Skip to content

[HPI-1059] Add support for PR base images. #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ name: Build and publish

on:
pull_request:
types:
- closed
branches:
- main
release:
Expand All @@ -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
Expand Down
40 changes: 40 additions & 0 deletions .github/workflows/pr-image.yaml
Original file line number Diff line number Diff line change
@@ -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$'
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
19 changes: 19 additions & 0 deletions scripts/pr-image.sh
Original file line number Diff line number Diff line change
@@ -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) <image-name> <branch-tag>"
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"