summaryrefslogtreecommitdiff
path: root/.buildkite/hooks/pre-command
blob: 9af5d459b5900dd14cf84a12e06f5b7ab1395d05 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/bin/bash

set -eu

# Only execute in the verify pipeline
[[ "$BUILDKITE_PIPELINE_NAME" =~ verify$ ]] || exit 0

docker ps || true
free -m || true

# We've now seen cases where origin/main on the build hosts can get
# out of date. This causes us to build components unnecessarily.
# Fetching it here hopefully will prevent this situation.
echo "Fetching origin/main"
git fetch origin main

# DEBUGGING FOR RELENG
# Fetch the git tags to see if that addresses the weird smart build behavior for Habitat
git fetch --tags --force

# Rebase onto current main to ensure this PR is closer to what happens when it's merged.
# Only do this if it's actually a branch (i.e. a PR or a manually created build), not a
# post-merge CI run of main.
if [[ "$BUILDKITE_BRANCH" != "main" ]]; then
  git config user.email "you@example.com" # these are needed for the rebase attempt
  git config user.name "Your Name"
  main=$(git show-ref -s --abbrev origin/main)
  pr_head=$(git show-ref -s --abbrev HEAD)
  github="https://github.com/chef/chef/commit/"
  if git rebase origin/main >/dev/null; then
    buildkite-agent annotate --style success --context "rebase-pr-branch-${main}" \
      "Rebased onto main ([${main}](${github}${main}))."
  else
    git rebase --abort
    buildkite-agent annotate --style warning --context "rebase-pr-branch-${main}" \
      "Couldn't rebase onto main ([${main}](${github}${main})), building PR HEAD ([${pr_head}](${github}${pr_head}))."
  fi
fi