Skip to content

Commit 1cc7c72

Browse files
committed
Add support for PR base images.
1 parent 9ea8944 commit 1cc7c72

File tree

4 files changed

+57
-0
lines changed

4 files changed

+57
-0
lines changed

.github/workflows/build.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ name: Build and publish
22

33
on:
44
pull_request:
5+
types:
6+
- closed
57
branches:
68
- main
79
release:

.github/workflows/pr-image.yaml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: Build and push image from PR
2+
3+
on:
4+
pull_request:
5+
types: [opened, synchronize, reopened]
6+
7+
jobs:
8+
build:
9+
name: Build and push image from PR
10+
runs-on: ubuntu-latest
11+
permissions:
12+
contents: read
13+
packages: write
14+
15+
steps:
16+
- name: Checkout code
17+
uses: actions/checkout@v3
18+
19+
- name: Log in to ghcr
20+
uses: docker/login-action@v2
21+
with:
22+
registry: ghcr.io
23+
username: ${{ github.actor }}
24+
password: ${{ secrets.GITHUB_TOKEN }}
25+
26+
- name: Build the image
27+
run: make build
28+
29+
- name: Push PR image to the registry
30+
run: make pr-image
31+
env:
32+
TAG: ${{ github.head_ref }}

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,9 @@ publish:
1212
@test -n "${TAG}" || ( echo "TAG environment variable must be set" && return 1 )
1313
./scripts/publish.sh $(IMAGE_NAME) ${TAG}
1414

15+
pr-image:
16+
@test -n "${TAG}" || ( echo "TAG environment variable must be set" && return 1 )
17+
./scripts/pr-image.sh $(IMAGE_NAME) ${TAG}
18+
1519
help:
1620
@sed -ne '/@sed/!s/## //p' $(MAKEFILE_LIST)

scripts/pr-image.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env bash
2+
3+
# Push images built off a PR to Github Container Registry
4+
5+
set -e
6+
7+
if [ "$#" -ne 2 ]; then
8+
>&2 echo "Usage: $(basename $0) <image-name> <branch-tag>"
9+
exit 1
10+
fi
11+
12+
IMAGE_NAME="$1"
13+
BRANCH=$(echo "${2}" | sed 's/\//-/g')
14+
15+
echo "🏷️ Setting tags for image '${IMAGE_NAME}:${BRANCH}-prbase"
16+
docker tag "${IMAGE_NAME}:base" "${IMAGE_NAME}:${BRANCH}-prbase"
17+
18+
echo "📤 Pushing '${IMAGE_NAME}:${BRANCH}-prbase'"
19+
docker push "${IMAGE_NAME}:${BRANCH}-prbase"

0 commit comments

Comments
 (0)