Skip to content

Commit

Permalink
chore: Dev container push workflow (#216)
Browse files Browse the repository at this point in the history
allows building a dev image for testing by commenting /build_dev on the PR.
Thanks to this post https://grem1.in/post/gha-comment-trigger/
  • Loading branch information
4141done committed Feb 26, 2024
1 parent 626d29d commit 0f89ee0
Showing 1 changed file with 126 additions and 0 deletions.
126 changes: 126 additions & 0 deletions .github/workflows/dev-preview.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
name: Build and Push Dev Preview Image
on:
# github treats issue comments and PR comments as the same.
# Further down there's a bit that restricts the job itself to a PR
# comment with a specific body. Credit to https://grem1.in/post/gha-comment-trigger/ for this script
issue_comment:
types:
- created

jobs:
build-and-push-dev-container:
runs-on: ubuntu-22.04
name: Builds and pushes the docker image for the open source NGINX version to github packages
if: ${{ github.event.issue.pull_request && github.event.comment.body == '/build_dev' }}
steps:
- name: Put a reaction to the comment
run: gh api graphql --silent --raw-field query="mutation AddReaction {addReaction(input:{subjectId:\"$NODE_ID\",content:EYES}){reaction{content}subject{id}}}"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NODE_ID: ${{ github.event.comment.node_id }}

- name: Check if PR is open
run: |
STATE=$(gh pr view $PR_NUMBER --repo ${{ github.repository }} --json state --jq .state)
if [ "$STATE" != "OPEN" ]; then
echo "Cannot build for closed PRs"
(
echo "**${{ github.workflow }}**"
echo "Cannot build Kuby for a closed PR. Use the `latest` version (built for the `master` branch) or create a new PR."
) | \
gh pr comment "${PR_NUMBER}" --repo ${{ github.repository }} -F -
gh api graphql --silent --raw-field query="mutation AddReaction {addReaction(input:{subjectId:\"$NODE_ID\",content:THUMBS_DOWN}){reaction{content}subject{id}}}"
gh api graphql --silent --raw-field query="mutation RemoveReaction {removeReaction(input:{subjectId:\"$NODE_ID\",content:EYES}){reaction{content}subject{id}}}"
exit 1
fi
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_NUMBER: ${{ github.event.issue.number }}
NODE_ID: ${{ github.event.comment.node_id }}

- name: Get PR HEAD Ref
id: getRef
run: echo "pr_ref=$(gh pr view $PR_NUMBER --repo ${{ github.repository }} --json headRefOid | jq -r '.headRefOid')" >> $GITHUB_OUTPUT
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_NUMBER: ${{ github.event.issue.number }}

- name: Debug print out ref info
run: echo $(gh pr view $PR_NUMBER --repo ${{ github.repository }})
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Checkout source code from Github
uses: actions/checkout@v4
with:
fetch-depth: 0
ref: ${{ steps.getRef.outputs.pr_ref }}

- name: Get Branch Name
id: getBranchName
run: echo "branch_name=$(gh pr view $PR_NUMBER --repo ${{ github.repository }} --json headRefName | jq -r '.headRefName')" >> $GITHUB_OUTPUT
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_NUMBER: ${{ github.event.issue.number }}

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

# Docker build setup
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Build and push image [oss]
uses: docker/build-push-action@v5
with:
file: Dockerfile.oss
context: .
push: true
platforms: linux/amd64,linux/arm64
provenance: false
tags: |
ghcr.io/${{ github.repository }}/nginx-oss-s3-gateway-dev:${{ steps.getBranchName.outputs.branch_name }}
- name: Final Comment
run: |
gh api graphql --silent --raw-field query="mutation AddReaction {addReaction(input:{subjectId:\"$NODE_ID\",content:THUMBS_UP}){reaction{content}subject{id}}}"
gh api graphql --silent --raw-field query="mutation RemoveReaction {removeReaction(input:{subjectId:\"$NODE_ID\",content:EYES}){reaction{content}subject{id}}}"
(
echo "**${{ github.workflow }}**"
echo "The long task is done!"
echo
echo "You can find the workflow here:"
echo "${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"
) | \
gh pr comment "${PR_NUMBER}" --repo ${{ github.repository }} -F -
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_NUMBER: ${{ github.event.issue.number }}
NODE_ID: ${{ github.event.comment.node_id }}

notify-job:
runs-on: ubuntu-22.04
needs: [build-and-push-dev-container]
if: ${{ always() && contains(needs.*.result, 'failure') }} <-- check that status of the previous job
steps:
- name: Notify on Failure
run: |
gh api graphql --silent --raw-field query="mutation AddReaction {addReaction(input:{subjectId:\"$NODE_ID\",content:THUMBS_DOWN}){reaction{content}subject{id}}}"
gh api graphql --silent --raw-field query="mutation RemoveReaction {removeReaction(input:{subjectId:\"$NODE_ID\",content:EYES}){reaction{content}subject{id}}}"
(
echo "**${{ github.workflow }}**"
echo "**Something went wrong!**"
echo
echo "**Details:** ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"
) | \
gh pr comment "${PR_NUMBER}" --repo ${{ github.repository }} -F -
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_NUMBER: ${{ github.event.issue.number }}
NODE_ID: ${{ github.event.comment.node_id }}

0 comments on commit 0f89ee0

Please sign in to comment.