forked from Invertex/UDHBot
-
Notifications
You must be signed in to change notification settings - Fork 9
ci(workflows): split build-and-deploy into reusable build and deploy … #376
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| name: Build | ||
|
|
||
| on: | ||
| workflow_call: | ||
| inputs: | ||
| checkout_ref: | ||
| description: "Git ref to checkout and build from" | ||
| required: true | ||
| type: string | ||
| image_name: | ||
| description: "GHCR image name (without registry prefix)" | ||
| required: true | ||
| type: string | ||
| outputs: | ||
| short_sha: | ||
| description: "Short SHA of the built commit" | ||
| value: ${{ jobs.build.outputs.short_sha }} | ||
| image_ref: | ||
| description: "Full image reference with tag (ghcr.io/owner/name:sha)" | ||
| value: ${{ jobs.build.outputs.image_ref }} | ||
|
|
||
| permissions: | ||
| contents: read | ||
| packages: write | ||
|
|
||
| jobs: | ||
| build: | ||
| name: Build & Push Image | ||
| runs-on: ubuntu-latest | ||
| outputs: | ||
| short_sha: ${{ steps.sha.outputs.short }} | ||
| image_ref: ghcr.io/${{ steps.sha.outputs.owner }}/${{ steps.sha.outputs.image }}:${{ steps.sha.outputs.short }} | ||
| steps: | ||
| - name: Checkout source | ||
| uses: actions/checkout@v4 | ||
| with: | ||
| ref: ${{ inputs.checkout_ref }} | ||
|
|
||
| - name: Set up Docker Buildx | ||
| uses: docker/setup-buildx-action@v3 | ||
|
|
||
| - name: Login to GHCR | ||
| uses: docker/login-action@v3 | ||
| with: | ||
| registry: ghcr.io | ||
| username: ${{ github.actor }} | ||
| password: ${{ secrets.GITHUB_TOKEN }} | ||
|
|
||
| - name: Extract short SHA and image prefix | ||
| id: sha | ||
| run: | | ||
| echo "short=$(git rev-parse --short=7 HEAD)" >> "$GITHUB_OUTPUT" | ||
| echo "owner=$(echo '${{ github.repository_owner }}' | tr '[:upper:]' '[:lower:]')" >> "$GITHUB_OUTPUT" | ||
| echo "image=$(echo '${{ inputs.image_name }}' | tr '[:upper:]' '[:lower:]')" >> "$GITHUB_OUTPUT" | ||
|
|
||
| - name: Build and push | ||
| uses: docker/build-push-action@v6 | ||
| with: | ||
| context: . | ||
| push: true | ||
| tags: | | ||
| ghcr.io/${{ steps.sha.outputs.owner }}/${{ steps.sha.outputs.image }}:${{ steps.sha.outputs.short }} | ||
| ghcr.io/${{ steps.sha.outputs.owner }}/${{ steps.sha.outputs.image }}:latest | ||
| cache-from: type=gha | ||
|
Pierre-Demessence marked this conversation as resolved.
|
||
| cache-to: type=gha,mode=max | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| name: PR Build on Comment | ||
|
|
||
| on: | ||
| issue_comment: | ||
| types: [created] | ||
|
|
||
| permissions: | ||
| contents: read | ||
| packages: write | ||
| pull-requests: write | ||
| issues: write | ||
|
|
||
| jobs: | ||
| check: | ||
| name: Validate trigger | ||
| if: | | ||
| github.event.issue.pull_request && | ||
| contains(github.event.comment.body, '/build') && | ||
| github.event.comment.author_association != 'NONE' && | ||
| github.event.comment.author_association != 'FIRST_TIMER' && | ||
| github.event.comment.author_association != 'FIRST_TIME_CONTRIBUTOR' | ||
|
Pierre-Demessence marked this conversation as resolved.
|
||
| runs-on: ubuntu-latest | ||
| outputs: | ||
| head_sha: ${{ steps.pr.outputs.head_sha }} | ||
| pr_number: ${{ steps.pr.outputs.pr_number }} | ||
| steps: | ||
| - name: Get PR details | ||
| id: pr | ||
| uses: actions/github-script@v7 | ||
| with: | ||
| script: | | ||
| const { data: pr } = await github.rest.pulls.get({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| pull_number: context.issue.number, | ||
| }); | ||
| if (pr.state !== 'open') { | ||
| core.setFailed(`PR #${context.issue.number} is not open`); | ||
| return; | ||
| } | ||
| core.setOutput('head_sha', pr.head.sha); | ||
| core.setOutput('pr_number', context.issue.number); | ||
|
|
||
| - name: React to comment | ||
| uses: actions/github-script@v7 | ||
| with: | ||
| script: | | ||
| await github.rest.reactions.createForIssueComment({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| comment_id: context.payload.comment.id, | ||
| content: 'rocket', | ||
| }); | ||
|
|
||
| build: | ||
| needs: check | ||
| uses: ./.github/workflows/build.yml | ||
| with: | ||
| checkout_ref: ${{ needs.check.outputs.head_sha }} | ||
| image_name: udc-bot-dev | ||
| permissions: | ||
| contents: read | ||
| packages: write | ||
|
|
||
| notify: | ||
| needs: [check, build] | ||
| if: needs.check.result != 'skipped' && always() | ||
| name: Post result | ||
| runs-on: ubuntu-latest | ||
| permissions: | ||
| pull-requests: write | ||
| issues: write | ||
| steps: | ||
| - name: Post success comment | ||
| if: needs.build.result == 'success' | ||
| uses: actions/github-script@v7 | ||
| env: | ||
| PR_NUMBER: ${{ needs.check.outputs.pr_number }} | ||
| IMAGE_REF: ${{ needs.build.outputs.image_ref }} | ||
| SHORT_SHA: ${{ needs.build.outputs.short_sha }} | ||
| with: | ||
| script: | | ||
| const { PR_NUMBER, IMAGE_REF, SHORT_SHA } = process.env; | ||
| await github.rest.issues.createComment({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| issue_number: Number(PR_NUMBER), | ||
| body: [ | ||
| '🐳 **Build complete!**', | ||
| '', | ||
| `Image: \`${IMAGE_REF}\``, | ||
| '', | ||
| 'To deploy with manifest changes:', | ||
| `1. Update \`k8s/dev/bot.yaml\` image tag to \`${SHORT_SHA}\` on your branch`, | ||
| '2. Switch ArgoCD dev target revision to your branch', | ||
| ].join('\n'), | ||
| }); | ||
|
|
||
| - name: Post failure comment | ||
| if: needs.build.result == 'failure' | ||
| uses: actions/github-script@v7 | ||
| env: | ||
| PR_NUMBER: ${{ needs.check.outputs.pr_number }} | ||
| with: | ||
| script: | | ||
| const { PR_NUMBER } = process.env; | ||
| await github.rest.issues.createComment({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| issue_number: Number(PR_NUMBER), | ||
| body: `❌ Build failed. Check the [workflow run](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}) for details.`, | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.