diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 013413d..3105940 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -2,6 +2,8 @@ name: Build and publish on: pull_request: + types: + - closed branches: - main release: @@ -15,13 +17,13 @@ jobs: contents: read packages: write steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Build the image run: make build - name: Log in to ghcr - uses: docker/login-action@v2 + uses: docker/login-action@v3 if: github.event_name == 'release' with: registry: ghcr.io diff --git a/.github/workflows/pr-image.yaml b/.github/workflows/pr-image.yaml new file mode 100644 index 0000000..ccb6a8f --- /dev/null +++ b/.github/workflows/pr-image.yaml @@ -0,0 +1,40 @@ +name: PR build and push + +on: + pull_request: + types: [opened, synchronize, reopened] + +jobs: + build: + name: Push pre-release image from PR + runs-on: ubuntu-latest + permissions: + contents: read + packages: write + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Log in to ghcr + uses: docker/login-action@v3 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Build the image + run: make build + + - name: Push PR image to the registry + run: make pr-image + env: + TAG: ${{ github.head_ref }} + + - name: Cleanup older pre-release images + uses: actions/delete-package-versions@v5 + with: + package-name: 'coder-dev' + package-type: 'container' + min-versions-to-keep: 10 + ignore-versions: '^\d+\.\d+\.\d+-base$' diff --git a/Makefile b/Makefile index 31b8a73..7505adf 100644 --- a/Makefile +++ b/Makefile @@ -12,5 +12,9 @@ publish: @test -n "${TAG}" || ( echo "TAG environment variable must be set" && return 1 ) ./scripts/publish.sh $(IMAGE_NAME) ${TAG} +pr-image: + @test -n "${TAG}" || ( echo "TAG environment variable must be set" && return 1 ) + ./scripts/pr-image.sh $(IMAGE_NAME) ${TAG} + help: @sed -ne '/@sed/!s/## //p' $(MAKEFILE_LIST) diff --git a/scripts/pr-image.sh b/scripts/pr-image.sh new file mode 100755 index 0000000..0db48fb --- /dev/null +++ b/scripts/pr-image.sh @@ -0,0 +1,19 @@ +#!/usr/bin/env bash + +# Push images built off a PR to Github Container Registry + +set -e + +if [ "$#" -ne 2 ]; then + >&2 echo "Usage: $(basename $0) " + exit 1 +fi + +IMAGE_NAME="$1" +BRANCH=$(echo "${2}" | sed 's/\//-/g') + +echo "🏷️ Setting tags for image '${IMAGE_NAME}:${BRANCH}-prbase" +docker tag "${IMAGE_NAME}:base" "${IMAGE_NAME}:${BRANCH}-prbase" + +echo "📤 Pushing '${IMAGE_NAME}:${BRANCH}-prbase'" +docker push "${IMAGE_NAME}:${BRANCH}-prbase"