Skip to content

Commit

Permalink
merge w master i guess
Browse files Browse the repository at this point in the history
  • Loading branch information
steveri committed Sep 24, 2023
2 parents 14dfd29 + 5ecdb0a commit 4266709
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 222 deletions.
186 changes: 23 additions & 163 deletions .buildkite/bin/custom-checkout.sh
Original file line number Diff line number Diff line change
@@ -1,191 +1,51 @@
#!/bin/bash

# What this script does:
# - Update and initialize all aha repo submodules.
# - Check out aha branch BUILDKITE_COMMIT if build triggered from aha repo
# or AHA DEFAULT (no-heroku now, master later) if triggered from submod push/pull.
# - Check out aha branch BUILDKITE_COMMIT if build triggered from aha repo.
# - Else check out master if triggered from submod push/pull.
# - Update and initialize all submodules.
# - If triggered from submod, update submod to match commit hash of triggering repo.

# Setup
set +u # nounset? not on my watch!
set +x # debug OFF
PS4="_" # Prevents "+++" prefix during 3-deep "set -x" execution
PS4=">" # Prevents "+++" prefix during 3-deep "set -x" execution

echo "--- CHECKOUT FULL REPO, submodules and all"

# Checkout
echo "+++ custom-checkout.sh BEGIN"
echo I am `whoami`
echo "+++ BEGIN custom-checkout.sh"
echo I am in dir `pwd`
cd $BUILDKITE_BUILD_CHECKOUT_PATH # Just in case, I dunno, whatevs.


##############################################################################
# FIXME yeah okay this could be offloaded as a separate script maybe something
# like "source annotate-w-pr-links.sh"
echo "+++ BEGIN TRIGGERED-FROM LINKS"

# If pull request, show where request came from.
if [ "$BUILDKITE_PULL_REQUEST_REPO" ]; then
# BUILDKITE_PULL_REQUEST_REPO="https://github.com/StanfordAHA/lake.git"
# BUILDKITE_PULL_REQUEST="166"
# BUILDKITE_COMMIT=7c5e88021a01fef1a04ea56b570563cae2050b1f

# E.g. repo="https://github.com/StanfordAHA/lake"
repo=`echo "$BUILDKITE_PULL_REQUEST_REPO" | sed 's/.git$//'`
r=`echo "$repo" | sed 's/http.*github.com.//'`

# E.g. url_cm="https://github.com/StanfordAHA/lake/commit/7c5...0b1f"
first7=`expr "$BUILDKITE_COMMIT" : '\(.......\)'` # 7c5e880
url_cm=${repo}/commit/${BUILDKITE_COMMIT}
mdlink_cm="[${first7}](${url_cm})"

# E.g. url_pr="https://github.com/StanfordAHA/lake/pull/166"
url_pr=${repo}/pull/${BUILDKITE_PULL_REQUEST}
mdlink_pr="[Pull Request #${BUILDKITE_PULL_REQUEST}](${url_pr})"

# E.g. "Triggered from StanfordAHA/canal ca602ef (Pull Request #58)"
cat <<EOF | buildkite-agent annotate --style "info" --context foo3
### Triggered from ${r} ${mdlink_cm} (${mdlink_pr})
EOF
fi

echo "--- END TRIGGERED-FROM LINKS"
# Checkout master or BUILDKITE_COMMIT

# FIXME don't need this after heroku is gone! FIXME
# Heroku sets BUILDKITE_COMMIT to sha of aha master branch.
# We want to rewrite that to be the sha of submod repo that
# originally triggered the build.

if [ "$FLOW_HEAD_SHA" ]; then
echo "Found heroku, rewriting BUILDKITE_COMMIT";
BUILDKITE_COMMIT=$FLOW_HEAD_SHA;
fi

# If trigger came from a submod repo, we will do "pr" regressions.
# Otherwise, trigger came from aha repo push/pull and we just do "daily" regressions.
# We use commdir to pass information to other steps.
# THIS ASSUMES THAT ALL STEPS RUN ON SAME HOST MACHINE and thus see the same commdir!

echo "--- Determine whether to do daily or pr regressions"
if git checkout -qf $BUILDKITE_COMMIT; then
echo "+++ UNSET DO_PR"
echo "BUILDKITE_COMMIT found in aha repo, we will do daily regressions."
cd $BUILDKITE_BUILD_CHECKOUT_PATH
# REQUEST_TYPE comes from set-trigfrom-and-reqtype.sh
if [ "$REQUEST_TYPE" == "SUBMOD_PR" ]; then
echo "Pull request from a submod repo: check out aha master branch"
git fetch -v --prune -- origin master
git checkout -qf master
else
echo "+++ SET DO_PR"
echo "BUILDKITE_COMMIT not found in aha repo, we will do pr regressions."
# FIXME could combine this with env-BNO temp file used by update-pr-repo.sh
commdir=/var/lib/buildkite-agent/builds/DELETEME; mkdir -p $commdir;
echo true > $commdir/DO_PR-${BUILDKITE_BUILD_NUMBER}
echo "Push or PR from aha repo: check out requested aha branch $BUILDKITE_COMMIT"
git fetch -v --prune -- origin $BUILDKITE_COMMIT
git checkout -qf $BUILDKITE_COMMIT
fi

echo "--- Check out appropriate AHA branch: $BUILDKITE_COMMIT, $DEV_BRANCH, or master"
cd $BUILDKITE_BUILD_CHECKOUT_PATH
# FIXME can delete DEV_BRANCH part once dev branch merges to master FIXME
DEV_BRANCH=remotes/origin/no-heroku
echo 'git checkout -qf $BUILDKITE_COMMIT || git checkout -qf $DEV_BRANCH || git checkout -qf master'; set -x
git checkout -qf $BUILDKITE_COMMIT || git checkout -qf $DEV_BRANCH || git checkout -qf master

echo "--- PREP AHA REPO and all its submodules"; set -x
pwd
# E.g. CHECKOUT_PATH=/var/lib/buildkite-agent/builds/r7cad-docker-1/stanford-aha/aha-flow
cd $BUILDKITE_BUILD_CHECKOUT_PATH # Actually I think we're already there but whatevs
echo "--- Initialize all submodules YES THIS TAKES AWHILE"

set -x
git submodule update --checkout # This is probably unnecessary but whatevs
git remote set-url origin https://github.com/hofstee/aha
git submodule foreach --recursive "git clean -ffxdq"
git clean -ffxdq
set +x

# FIXME can of course delete this after heroku is gone FIXME
# Heroku always sets message to "PR from <repo>" with BUILDKITE_COMMIT="HEAD"
# so as to checkout aha master. Heroku also sends desired submod commit <repo>
# hash as env var FLOW_HEAD_SHA. In this new regime, we set BUILDKITE_COMMIT
# as the desired submod commit, and auto-discover the repo that goes with the commit.

echo "--- See if we need to update a submodule"
unset PR_FROM_SUBMOD

# PR_FROM_SUBMOD means build was triggered by foreign (non-aha) repo,
# i.e. one of the submods. The submod sends its commit hash BUT DOES
# NOT TELL US WHAT REPO IT IS. :(

# We detect this by attempting to fetch BUILDKITE_COMMIT from aha repo.
# Success means this is an aha-triggered build. Failure means we need to
# find what repo actually did trigger the commit.

echo git fetch -v --prune -- origin $BUILDKITE_COMMIT
if git fetch -v --prune -- origin $BUILDKITE_COMMIT; then

# Pretty sure we already did this, in pipeline.xml BDI step pre-checkout hook
# But what the heck, let's do it again, don't break what is working already.
git checkout -f $BUILDKITE_COMMIT
echo "Found aha commit '$BUILDKITE_COMMIT'; no need to update submodule"

else
echo '-------------------------------------------'
echo 'REQUESTED COMMIT DOES NOT EXIST in aha repo'
echo '(This must be a pull request from one of the submods)'
echo 'Will checkout aha default branch'
PR_FROM_SUBMOD=true

# FIXME don't need if-then-else below after dev merges to master.
# Use dev branch as default until it gets merged and deleted.
AHA_DEFAULT_BRANCH=no-heroku
echo "Meanwhile, will use default branch '$AHA_DEFAULT_BRANCH' for aha repo"
if git fetch -v --prune -- origin $AHA_DEFAULT_BRANCH; then
echo "Fetching aha branch '$AHA_DEFAULT_BRANCH'"
git checkout -f $AHA_DEFAULT_BRANCH
else
echo "Aha branch '$AHA_DEFAULT_BRANCH' does not exist"
echo "Fetching aha master branch"
git fetch -v --prune -- origin master
git checkout -f master
fi
fi

set -x
git submodule sync --recursive
echo "--- git submodule update --init --recursive --force"
git submodule update --init --recursive --force
echo '--- git submodule foreach --recursive "git reset --hard"'
git submodule foreach --recursive "git reset --hard"
set +x

update_repo=`git config --get remote.origin.url`
# Update submod

# To find out what repo triggered the commit, we iterate through
# all the submodules and find which one can successfully checkout
# the desired BUILKITE_COMMIT.

# NOTE this is not necessary for pull requests, which embed the
# repo information as BUILDKITE_PULL_REQUEST. Push requests have
# no such mechanism however :(
# In general we don't process push requests from submodules, but
# this was useful for development etc.

if [ "$PR_FROM_SUBMOD" ]; then
echo "--- Handle PR"
echo "+++ Looking for submod commit $BUILDKITE_COMMIT"
unset FOUND_SUBMOD
submods=`git submodule status | awk '{print $2}'` # canal lake hwtypes...
for submod in $submods; do
echo "- " Looking in submod $submod
# --- THIS IS WHERE THE CHECKOUT HAPPENS ---
(set -x; cd $submod; git fetch origin && git checkout $BUILDKITE_COMMIT) && FOUND_SUBMOD=true || echo "NOT " $submod
[ "$FOUND_SUBMOD" ] && echo "- FOUND valid commit in submod" $submod
[ "$FOUND_SUBMOD" ] && break
done

if [ "$FOUND_SUBMOD" ]; then
echo "--- Updated submodule '$submod' w commit '$BUILDKITE_COMMIT'"
update_repo=`cd $submod; git config --get remote.origin.url`
else
echo "ERROR could not find requesting submod"; exit 13
fi
set +x
else
echo "--- NOT A PULL REQUEST"
# Note PR_REPO_TAIL comes from set-trigfrom-and-reqtype.sh
if [ "$REQUEST_TYPE" == "SUBMOD_PR" ]; then
echo "--- Update submodule '$PR_REPO_TAIL' w commit '$BUILDKITE_COMMIT'"
(set -x; cd $PR_REPO_TAIL; git fetch origin && git checkout $BUILDKITE_COMMIT)
fi

echo "--- custom-checkout.sh END"
echo "--- END custom-checkout.sh"
57 changes: 57 additions & 0 deletions .buildkite/bin/set-trigfrom-and-reqtype.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/bin/bash

# What does this script do?
# 1. If build was triggered by a pull request, annotate build with
# links pointing to pull request on github.
# 2. Set REQUEST_TYPE to one of "AHA_PUSH", "AHA_PR", or "SUBMOD_PR"
# 3. Set PR_REPO_TAIL to submod associated with the PR, e.g. "canal"
# 4. Add REQUEST_TYPE env temp file for use by later steps

echo "+++ set-trigfrom-and-reqtype.sh BEGIN"
cd $BUILDKITE_BUILD_CHECKOUT_PATH # Just in case, I dunno, whatevs.

echo "+++ BEGIN TRIGGERED-FROM LINKS"

# If pull request, show where request came from.
# And set e.g. PR_REPO_TAIL="lake"
PR_REPO_TAIL=
if [ "$BUILDKITE_PULL_REQUEST_REPO" ]; then
# BUILDKITE_PULL_REQUEST_REPO="https://github.com/StanfordAHA/lake.git"
# BUILDKITE_PULL_REQUEST="166"
# BUILDKITE_COMMIT=7c5e88021a01fef1a04ea56b570563cae2050b1f

# E.g. repo="https://github.com/StanfordAHA/lake"
repo=`echo "$BUILDKITE_PULL_REQUEST_REPO" | sed 's/.git$//'`
r=`echo "$repo" | sed 's/http.*github.com.//'` # "StanfordAHA/lake"
PR_REPO_TAIL=`echo "$repo" | sed "s,http.*github.com/.*/,,"` # "lake"
echo "Found PR from submod $PR_REPO_TAIL"

# E.g. url_cm="https://github.com/StanfordAHA/lake/commit/7c5...0b1f"
first7=`expr "$BUILDKITE_COMMIT" : '\(.......\)'` # 7c5e880
url_cm=${repo}/commit/${BUILDKITE_COMMIT}
mdlink_cm="[${first7}](${url_cm})"

# E.g. url_pr="https://github.com/StanfordAHA/lake/pull/166"
url_pr=${repo}/pull/${BUILDKITE_PULL_REQUEST}
mdlink_pr="[Pull Request #${BUILDKITE_PULL_REQUEST}](${url_pr})"

# E.g. "Triggered from StanfordAHA/canal ca602ef (Pull Request #58)"
cat <<EOF | buildkite-agent annotate --style "info" --context foo3
### Triggered from ${r} ${mdlink_cm} (${mdlink_pr})
EOF
fi
echo "--- END TRIGGERED-FROM LINKS"

case "$PR_REPO_TAIL" in
"aha") REQUEST_TYPE="AHA_PR" ;;
"") REQUEST_TYPE="AHA_PUSH" ;;
*) REQUEST_TYPE="SUBMOD_PR" ;;
esac
echo "--- FOUND REQUEST_TYPE '$REQUEST_TYPE'"

# Squirrel away the info for later use
temp=/var/lib/buildkite-agent/builds/DELETEME; mkdir -p $temp
env=$temp/env-$BUILDKITE_BUILD_NUMBER
echo REQUEST_TYPE=${REQUEST_TYPE} >> $env

echo "+++ set-trigfrom-and-reqtype.sh END"
7 changes: 1 addition & 6 deletions .buildkite/bin/update-pr-repo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,7 @@ cd $BUILDKITE_BUILD_CHECKOUT_PATH # Just in case, I dunno, whatevs.

# Also need to rediscover pull number BUILDKITE_PULL_REQUEST

# FIXME delete this clause after heroku is gone forever
if [ "$FLOW_HEAD_SHA" ]; then
echo "- Heroku build, leave it alone for now"
echo "- Nothing to do, returning to main script."

elif [ "$BUILDKITE_PULL_REQUEST_REPO" ]; then
if [ "$BUILDKITE_PULL_REQUEST_REPO" ]; then
echo "- BUILDKITE_PULL_REQUEST_REPO already set, to '$BUILDKITE_PULL_REQUEST_REPO'"
echo "- Nothing to do, returning to main script."

Expand Down
14 changes: 0 additions & 14 deletions .buildkite/hooks/post-checkout

This file was deleted.

31 changes: 0 additions & 31 deletions .buildkite/hooks/pre-exit

This file was deleted.

48 changes: 40 additions & 8 deletions .buildkite/pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,47 @@ steps:
# Req from submod repo: check out aha master branch, update submod w BUILDKITE_COMMIT
pre-checkout: |
echo "+++ BDI PRE CHECKOUT HOOK"
source $BUILDKITE_BUILD_CHECKOUT_PATH/.buildkite/bin/update-pr-repo.sh
set +u # nounset? not on my watch!
if [ "$$FLOW_HEAD_SHA" ]; then
echo "+++ ERROR Not processing heroku requests anymore"; exit 13; fi
# Clone the aha repo; starting in root dir '/' I think
echo I am in dir `pwd`
aha_clone=$BUILDKITE_BUILD_CHECKOUT_PATH;
set -x
/bin/rm -rf $$aha_clone; mkdir -p $$aha_clone
git clone https://github.com/StanfordAHA/aha $$aha_clone; cd $$aha_clone;
git remote set-url origin https://github.com/StanfordAHA/aha # Why?
git clean -ffxdq
set +x
# Use this hack to (temporarily) use dev branches during development
USING_MASTER=; DEV=aha-no-heroku-cleanup
if [ "$$DEV" ]; then
git fetch -v --prune -- origin $$DEV || USING_MASTER=true
git checkout $$DEV || USING_MASTER=true
if ! [ "$$USING_MASTER" ]; then
msg="### WARNING Using dev branch '$${DEV}' instead of master"
buildkite-agent annotate --style "warning" --context dev "$$msg"
fi
fi
bin=$BUILDKITE_BUILD_CHECKOUT_PATH/.buildkite/bin
# Make sure env var BUILDKITE_PULL_REQUEST_REPO is set correctly
source $$bin/update-pr-repo.sh
# If build was triggered by a pull request, annotate build with
# links pointing to pull request on github.
# Set REQUEST_TYPE to one of "AHA_PUSH", "AHA_PR", or "SUBMOD_PR"
# Set PR_REPO_TAIL to submod associated with the PR, e.g. "canal"
# Add REQUEST_TYPE env temp file for use by later steps
source $$bin/set-trigfrom-and-reqtype.sh
~/bin/status-update --force pending # Send "pending" status to github PR page
source $BUILDKITE_BUILD_CHECKOUT_PATH/.buildkite/bin/custom-checkout.sh
# Checkout and update correct aha branch and submodules
source $$bin/custom-checkout.sh
# Send regression test pass-fail info to github pull request page
pre-exit: |
Expand All @@ -41,12 +79,6 @@ steps:
- echo "--- Creating garnet Image"
- docker build . -t "garnet:aha-flow-build-${BUILDKITE_BUILD_NUMBER}"

# Docker image successfully built. Remove
# .TEST file to avoid reporting failure to
# aha-flow-app. This is a heroku thing I guess.
# FIXME remove heroku, remove this, along with pre-exit and post-checkout hooks FIXME
- rm -rf temp/.TEST

- echo "--- Pruning Docker Images"
- yes | docker image prune -a --filter "until=6h" --filter=label='description=garnet' || true

Expand Down
Loading

0 comments on commit 4266709

Please sign in to comment.