Skip to content
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

Fix PR publish workflow #129

Merged
merged 3 commits into from
Aug 26, 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
27 changes: 3 additions & 24 deletions .github/workflows/pr-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,14 @@ on:
- opened
- synchronize
- reopened
pull_request_target:
types: [labeled]

jobs:
build-and-push:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Login to GitHub Container Registry
if: github.event_name == 'pull_request_target' && contains(github.event.pull_request.labels.*.name, 'push-to-ghcr')
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GHCR_PAT }}

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

Expand All @@ -35,19 +25,8 @@ jobs:
tags: type=raw,value=pr-${{ github.event.number }}
flavor: latest=false

- name: Docker - Build and Push
- name: Docker - Build
uses: docker/build-push-action@v5
with:
push: ${{ github.event_name == 'pull_request_target' && contains(github.event.pull_request.labels.*.name, 'push-to-ghcr') }}
push: false
tags: ${{ steps.meta.outputs.tags }}

- name: Comment on Pull Request with Container Image URL
if: success() && github.event_name == 'pull_request_target' && contains(github.event.pull_request.labels.*.name, 'push-to-ghcr')
uses: marocchino/sticky-pull-request-comment@v2
with:
token: ${{ secrets.GITHUB_TOKEN }}
header: Built and pushed Docker image for PR
message: |
The Docker image for this pull request has been built and pushed to GHCR.

Image URL: `ghcr.io/${{ github.repository }}:pr-${{ github.event.number }}`
72 changes: 72 additions & 0 deletions .github/workflows/pr-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
name: Pull Request Publish
on:
issue_comment:
types:
- created

jobs:
fetch-pr-details:
if: ${{ github.event.comment.body == '/publish' && github.event.issue.pull_request }}
runs-on: ubuntu-latest
outputs:
ref: ${{ steps.get-ref.outputs.ref }}
steps:
- name: Get PR Ref
id: get-ref
uses: actions/github-script@v6
with:
script: |
const prNumber = context.issue.number;
const response = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: prNumber
});
const ref = response.data.head.ref;
return ref;
result-encoding: string

build-and-push:
needs: fetch-pr-details
if: ${{ github.event.comment.body == '/publish' && github.event.issue.pull_request }}
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
ref: ${{ needs.fetch-pr-details.outputs.ref }}

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

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

- name: Docker - Metadata
id: meta
uses: docker/metadata-action@v5
with:
images: |
ghcr.io/${{ github.repository }}
tags: type=raw,value=pr-${{ github.event.issue.number }}
flavor: latest=false

- name: Docker - Build and Push
uses: docker/build-push-action@v5
with:
push: true
tags: ${{ steps.meta.outputs.tags }}

- name: Comment on Pull Request with Container Image URL
if: success()
uses: marocchino/sticky-pull-request-comment@v2
with:
header: Built and pushed Docker image for PR
message: |
The Docker image for this pull request has been built and pushed to GHCR.

Image URL: `ghcr.io/${{ github.repository }}:pr-${{ github.event.issue.number }}`
Loading