Skip to content

Commit

Permalink
Fix pull request workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
codetheweb committed Aug 18, 2024
1 parent 4841c07 commit a2d489c
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 24 deletions.
78 changes: 78 additions & 0 deletions .github/workflows/pr-release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
name: Release snapshot of PR
on:
workflow_run:
workflows: ["PR Workflow"]
types:
- completed

jobs:
release-and-comment:
name: Release snapshot and comment in PR
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
attestations: write
id-token: write
steps:
- name: Download images
uses: actions/download-artifact@v4
with:
path: /tmp/images
pattern: image-*
merge-multiple: true
run-id: ${{ github.event.workflow_run.id }}
github-token: ${{ secrets.WORKFLOW_PAT }}

- name: Load image
run: |
docker load --input /tmp/images/image-amd64.tar
docker load --input /tmp/images/image-arm64.tar
- name: Download PR number
uses: actions/download-artifact@v4
with:
path: /tmp/pull_request_number
pattern: pull_request_number
run-id: ${{ github.event.workflow_run.id }}
github-token: ${{ secrets.WORKFLOW_PAT }}

- name: Read the pull_request_number.txt file
id: pull_request_number_reader
uses: juliangruber/[email protected]
with:
path: ./pull_request_number/pull_request_number.txt

- name: Set up Buildx
uses: docker/setup-buildx-action@v1

- name: Login to DockerHub
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Push
working-directory: /tmp/digests
run: docker buildx imagetools create -t 'ghcr.io/museofficial/muse:pr-${{ github.event.number }}' -t 'ghcr.io/museofficial/muse:${{ github.event.pull_request.head.sha }}' 'ghcr.io/museofficial/muse:${{ github.event.pull_request.head.sha }}-arm64' 'ghcr.io/museofficial/muse:${{ github.event.pull_request.head.sha }}-amd64'

- name: Create comment
uses: marocchino/sticky-pull-request-comment@v2
with:
header: "pr-release"
number: ${{ steps.pull_request_number_reader.outputs.content }}
message: |
#### :package: A new release has been made for this pull request.
To play around with this PR, pull `ghcr.io/museofficial/muse:pr-${{ github.event.number }}` or `ghcr.io/museofficial/muse:${{ github.event.pull_request.head.sha }}`.
Images are available for x86_64 and ARM64.
> Latest commit: ${{ github.event.pull_request.head.sha }}
99 changes: 77 additions & 22 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
name: PR Workflow

on: pull_request_target
on: pull_request

env:
REGISTRY_IMAGE: ghcr.io/museofficial/muse

jobs:
release-snapshot:
name: Release snapshot
build:
name: Build snapshot
strategy:
matrix:
runner-platform:
- ubuntu-latest
- macos-14 # ARM
- buildjet-4vcpu-ubuntu-2204-arm
include:
- runner-platform: ubuntu-latest
build-arch: linux/amd64
tagged-platform: amd64
- runner-platform: macos-14
- runner-platform: buildjet-4vcpu-ubuntu-2204-arm
build-arch: linux/arm64
tagged-platform: arm64
runs-on: ${{ matrix.runner-platform }}
Expand All @@ -24,8 +27,20 @@ jobs:
attestations: write
id-token: write
steps:
- name: Prepare
run: |
platform=${{ matrix.build-arch }}
echo "PLATFORM_PAIR=${platform//\//-}" >> $GITHUB_ENV
- name: Docker meta
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY_IMAGE }}
tags: type=ref,event=pr

- name: Set up Buildx
uses: docker/setup-buildx-action@v1
uses: docker/setup-buildx-action@v3

- name: Login to GitHub Container Registry
uses: docker/login-action@v3
Expand All @@ -38,31 +53,64 @@ jobs:
uses: josStorer/get-current-time@v2
id: current-time

- name: Build and push
id: docker_build
- name: Build
id: build
uses: docker/build-push-action@v6
with:
context: .
push: true
tags: ghcr.io/museofficial/muse:${{ github.event.pull_request.head.sha }}-${{ matrix.tagged-platform }}
outputs: type=docker,dest=/tmp/image.tar
tags: ${{ steps.meta.outputs.tags }}
platforms: ${{ matrix.build-arch }}
build-args: |
COMMIT_HASH=${{ github.sha }}
BUILD_DATE=${{ steps.current-time.outputs.time }}
combine-and-comment:
name: Combine platform tags and leave comment
- name: Export digest
run: |
mkdir -p /tmp/digests
digest="${{ steps.build.outputs.digest }}"
touch "/tmp/digests/${digest#sha256:}"
- name: Upload digest
uses: actions/upload-artifact@v4
with:
name: image-${{ env.PLATFORM_PAIR }}
path: /tmp/image.tar
if-no-files-found: error
retention-days: 1

- name: Save PR number in artifact
shell: bash
env:
PR_NUMBER: ${{ github.event.number }}
run: echo $PR_NUMBER > /tmp/pull_request_number.txt
- name: Upload PR number
uses: actions/upload-artifact@v4
with:
name: pull_request_number
path: /tmp/pull_request_number.txt


merge:
runs-on: ubuntu-latest
needs: release-snapshot
needs:
- build
steps:
- name: Set up Buildx
uses: docker/setup-buildx-action@v1
- name: Download digests
uses: actions/download-artifact@v4
with:
path: /tmp/digests
pattern: digests-*
merge-multiple: true

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Login to DockerHub
uses: docker/login-action@v1
- name: Docker meta
id: meta
uses: docker/metadata-action@v5
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
images: ${{ env.REGISTRY_IMAGE }}
tags: type=ref,event=pr

- name: Login to GitHub Container Registry
uses: docker/login-action@v3
Expand All @@ -71,8 +119,15 @@ jobs:
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Combine tags
run: docker buildx imagetools create -t 'ghcr.io/museofficial/muse:pr-${{ github.event.number }}' -t 'ghcr.io/museofficial/muse:${{ github.event.pull_request.head.sha }}' 'ghcr.io/museofficial/muse:${{ github.event.pull_request.head.sha }}-arm64' 'ghcr.io/museofficial/muse:${{ github.event.pull_request.head.sha }}-amd64'
- name: Create manifest list and push
working-directory: /tmp/digests
run: |
docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \
$(printf '${{ env.REGISTRY_IMAGE }}@sha256:%s ' *)
- name: Inspect image
run: |
docker buildx imagetools inspect ${{ env.REGISTRY_IMAGE }}:${{ steps.meta.outputs.version }}
- name: Create comment
uses: marocchino/sticky-pull-request-comment@v2
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ jobs:
matrix:
runner-platform:
- ubuntu-latest
- macos-14 # ARM
- buildjet-4vcpu-ubuntu-2204-arm
include:
- runner-platform: ubuntu-latest
build-arch: linux/amd64
tagged-platform: amd64
- runner-platform: macos-14
- runner-platform: buildjet-4vcpu-ubuntu-2204-arm
build-arch: linux/arm64
tagged-platform: arm64
runs-on: ${{ matrix.runner-platform }}
Expand Down

0 comments on commit a2d489c

Please sign in to comment.