|
| 1 | +name: Build and Push Dev Preview Image |
| 2 | +on: |
| 3 | + # github treats issue comments and PR comments as the same. |
| 4 | + # Further down there's a bit that restricts the job itself to a PR |
| 5 | + # comment with a specific body. Credit to https://grem1.in/post/gha-comment-trigger/ for this script |
| 6 | + issue_comment: |
| 7 | + types: |
| 8 | + - created |
| 9 | + |
| 10 | +jobs: |
| 11 | + build-and-push-dev-container: |
| 12 | + runs-on: ubuntu-22.04 |
| 13 | + name: Builds and pushes the docker image for the open source NGINX version to github packages |
| 14 | + if: ${{ github.event.issue.pull_request && github.event.comment.body == '/build_dev' }} |
| 15 | + steps: |
| 16 | + - name: Put a reaction to the comment |
| 17 | + run: gh api graphql --silent --raw-field query="mutation AddReaction {addReaction(input:{subjectId:\"$NODE_ID\",content:EYES}){reaction{content}subject{id}}}" |
| 18 | + env: |
| 19 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 20 | + NODE_ID: ${{ github.event.comment.node_id }} |
| 21 | + |
| 22 | + - name: Check if PR is open |
| 23 | + run: | |
| 24 | + STATE=$(gh pr view $PR_NUMBER --repo ${{ github.repository }} --json state --jq .state) |
| 25 | + if [ "$STATE" != "OPEN" ]; then |
| 26 | + echo "Cannot build for closed PRs" |
| 27 | + ( |
| 28 | + echo "**${{ github.workflow }}**" |
| 29 | + echo "Cannot build Kuby for a closed PR. Use the `latest` version (built for the `master` branch) or create a new PR." |
| 30 | + ) | \ |
| 31 | + gh pr comment "${PR_NUMBER}" --repo ${{ github.repository }} -F - |
| 32 | + gh api graphql --silent --raw-field query="mutation AddReaction {addReaction(input:{subjectId:\"$NODE_ID\",content:THUMBS_DOWN}){reaction{content}subject{id}}}" |
| 33 | + gh api graphql --silent --raw-field query="mutation RemoveReaction {removeReaction(input:{subjectId:\"$NODE_ID\",content:EYES}){reaction{content}subject{id}}}" |
| 34 | + exit 1 |
| 35 | + fi |
| 36 | + env: |
| 37 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 38 | + PR_NUMBER: ${{ github.event.issue.number }} |
| 39 | + NODE_ID: ${{ github.event.comment.node_id }} |
| 40 | + |
| 41 | + - name: Get PR HEAD Ref |
| 42 | + id: getRef |
| 43 | + run: echo "pr_ref=$(gh pr view $PR_NUMBER --repo ${{ github.repository }} --json headRefOid | jq -r '.headRefOid')" >> $GITHUB_OUTPUT |
| 44 | + env: |
| 45 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 46 | + PR_NUMBER: ${{ github.event.issue.number }} |
| 47 | + |
| 48 | + - name: Debug print out ref info |
| 49 | + run: echo $(gh pr view $PR_NUMBER --repo ${{ github.repository }}) |
| 50 | + env: |
| 51 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 52 | + |
| 53 | + - name: Checkout source code from Github |
| 54 | + uses: actions/checkout@v4 |
| 55 | + with: |
| 56 | + fetch-depth: 0 |
| 57 | + ref: ${{ steps.getRef.outputs.pr_ref }} |
| 58 | + |
| 59 | + - name: Get Branch Name |
| 60 | + id: getBranchName |
| 61 | + run: echo "branch_name=$(gh pr view $PR_NUMBER --repo ${{ github.repository }} --json headRefName | jq -r '.headRefName')" >> $GITHUB_OUTPUT |
| 62 | + env: |
| 63 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 64 | + PR_NUMBER: ${{ github.event.issue.number }} |
| 65 | + |
| 66 | + - name: Login to GitHub Container Registry |
| 67 | + uses: docker/login-action@v3 |
| 68 | + with: |
| 69 | + registry: ghcr.io |
| 70 | + username: ${{ github.repository_owner }} |
| 71 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 72 | + |
| 73 | + # Docker build setup |
| 74 | + - name: Set up QEMU |
| 75 | + uses: docker/setup-qemu-action@v3 |
| 76 | + - name: Set up Docker Buildx |
| 77 | + uses: docker/setup-buildx-action@v3 |
| 78 | + |
| 79 | + - name: Build and push image [oss] |
| 80 | + uses: docker/build-push-action@v5 |
| 81 | + with: |
| 82 | + file: Dockerfile.oss |
| 83 | + context: . |
| 84 | + push: true |
| 85 | + platforms: linux/amd64,linux/arm64 |
| 86 | + provenance: false |
| 87 | + tags: | |
| 88 | + ghcr.io/${{ github.repository }}/nginx-oss-s3-gateway-dev:${{ steps.getBranchName.outputs.branch_name }} |
| 89 | + |
| 90 | + - name: Final Comment |
| 91 | + run: | |
| 92 | + gh api graphql --silent --raw-field query="mutation AddReaction {addReaction(input:{subjectId:\"$NODE_ID\",content:THUMBS_UP}){reaction{content}subject{id}}}" |
| 93 | + gh api graphql --silent --raw-field query="mutation RemoveReaction {removeReaction(input:{subjectId:\"$NODE_ID\",content:EYES}){reaction{content}subject{id}}}" |
| 94 | + ( |
| 95 | + echo "**${{ github.workflow }}**" |
| 96 | + echo "The long task is done!" |
| 97 | + echo |
| 98 | + echo "You can find the workflow here:" |
| 99 | + echo "${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}" |
| 100 | + ) | \ |
| 101 | + gh pr comment "${PR_NUMBER}" --repo ${{ github.repository }} -F - |
| 102 | + env: |
| 103 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 104 | + PR_NUMBER: ${{ github.event.issue.number }} |
| 105 | + NODE_ID: ${{ github.event.comment.node_id }} |
| 106 | + |
| 107 | + notify-job: |
| 108 | + runs-on: ubuntu-22.04 |
| 109 | + needs: [build-and-push-dev-container] |
| 110 | + if: ${{ always() && contains(needs.*.result, 'failure') }} <-- check that status of the previous job |
| 111 | + steps: |
| 112 | + - name: Notify on Failure |
| 113 | + run: | |
| 114 | + gh api graphql --silent --raw-field query="mutation AddReaction {addReaction(input:{subjectId:\"$NODE_ID\",content:THUMBS_DOWN}){reaction{content}subject{id}}}" |
| 115 | + gh api graphql --silent --raw-field query="mutation RemoveReaction {removeReaction(input:{subjectId:\"$NODE_ID\",content:EYES}){reaction{content}subject{id}}}" |
| 116 | + ( |
| 117 | + echo "**${{ github.workflow }}**" |
| 118 | + echo "**Something went wrong!**" |
| 119 | + echo |
| 120 | + echo "**Details:** ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}" |
| 121 | + ) | \ |
| 122 | + gh pr comment "${PR_NUMBER}" --repo ${{ github.repository }} -F - |
| 123 | + env: |
| 124 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 125 | + PR_NUMBER: ${{ github.event.issue.number }} |
| 126 | + NODE_ID: ${{ github.event.comment.node_id }} |
0 commit comments