Skip to content

Commit

Permalink
[docker] build on branch names
Browse files Browse the repository at this point in the history
  • Loading branch information
rustielin committed Oct 3, 2024
1 parent 04da4fd commit dbd773c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
11 changes: 10 additions & 1 deletion .github/workflows/build-images.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ on:
push:
branches:
- main
- rustielin/branch-prefix-build # XXX: for canary, remove before merging

# cancel redundant builds
concurrency:
Expand Down Expand Up @@ -33,4 +34,12 @@ jobs:
with:
GCP_SERVICE_ACCOUNT_EMAIL: ${{ secrets.GCP_SERVICE_ACCOUNT_EMAIL }}
GCP_WORKLOAD_IDENTITY_PROVIDER: ${{ secrets.GCP_WORKLOAD_IDENTITY_PROVIDER }}
- run: ./scripts/build-and-push-images.sh ${{ matrix.example }}
- name: Extract branch name
shell: bash
run: echo "branch=${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}" >> $GITHUB_OUTPUT
id: extract_branch
- name: Build and push images
run: ./scripts/build-and-push-images.sh ${{ matrix.example }}
env:
GIT_BRANCH: ${{ steps.extract_branch.outputs.branch }}

17 changes: 16 additions & 1 deletion scripts/build-and-push-images.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,29 @@ else
CREDENTIAL_MOUNT="$HOME/.config/gcloud:/root/.config/gcloud:ro"
fi

# Normalize GIT_BRANCH if it's set
if [ -n "${GIT_BRANCH}" ]; then
export NORMALIZED_GIT_BRANCH=$(printf "${GIT_BRANCH}" | sed -e 's/[^a-zA-Z0-9]/-/g')
fi

for example in $EXAMPLES_TO_BUILD; do
# Set DESTINATIONS based on GIT_SHA since that is always set
DESTINATIONS="--destination ${TARGET_REGISTRY}/${example}:${GIT_SHA}"

# If GIT_BRANCH is set and not empty, add it as an additional tag
if [ -n "${NORMALIZED_GIT_BRANCH}" ]; then
DESTINATIONS="${DESTINATIONS} --destination ${TARGET_REGISTRY}/${example}:${NORMALIZED_GIT_BRANCH}"
DESTINATIONS="${DESTINATIONS} --destination ${TARGET_REGISTRY}/${example}:${NORMALIZED_GIT_BRANCH}_${GIT_SHA}"
fi

# build and push the image
docker run \
--rm \
-v $CREDENTIAL_MOUNT \
-v $(pwd)/$example:/workspace \
gcr.io/kaniko-project/executor:latest \
--dockerfile /workspace/Dockerfile \
--destination "$TARGET_REGISTRY/$example:$GIT_SHA" \
$DESTINATIONS \
--context dir:///workspace/ \
--cache=true
done

0 comments on commit dbd773c

Please sign in to comment.