Trigger Buildkite update screenshots build #1141
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
| name: Trigger Buildkite update screenshots build | |
| on: | |
| issue_comment: | |
| types: [created] | |
| permissions: | |
| issues: write | |
| pull-requests: write | |
| contents: read | |
| jobs: | |
| check-and-trigger: | |
| if: | | |
| github.event.issue.pull_request && | |
| ( | |
| contains(github.event.comment.body, 'buildkite update screenshots') || | |
| contains(github.event.comment.body, 'buildkite update vrt') | |
| ) | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Check elastic org membership | |
| id: check-elastic-member | |
| uses: actions/github-script@v7 | |
| with: | |
| github-token: ${{ secrets.ADMIN_TOKEN_GH }} | |
| script: | | |
| const org = 'elastic'; | |
| const username = '${{ github.event.comment.user.login }}'; | |
| try { | |
| await github.request('GET /orgs/{org}/members/{username}', { | |
| org, | |
| username, | |
| headers: { Accept: 'application/vnd.github.v3+json' } | |
| }); | |
| core.setOutput('is_member', 'true'); | |
| core.info(`${username} is a member of ${org}`); | |
| } catch (err) { | |
| if (err.status === 404) { | |
| core.setOutput('is_member', 'false'); | |
| core.info(`${username} is NOT a member of ${org}`); | |
| } else { | |
| core.setFailed(`Failed to check membership: ${err}`); | |
| } | |
| } | |
| - name: Add 😕 reaction to comment | |
| if: steps.check-elastic-member.outputs.is_member == 'false' | |
| uses: peter-evans/create-or-update-comment@v5 | |
| with: | |
| comment-id: ${{ github.event.comment.id }} | |
| reactions: 'confused' | |
| - name: Fail if not Elastic org member | |
| if: steps.check-elastic-member.outputs.is_member == 'false' | |
| run: | | |
| echo "User ${{ github.event.comment.user.login }} is not a member of elastic organization" | |
| echo "Aborting Buildkite trigger" | |
| - name: Add 👍 reaction to comment | |
| if: steps.check-elastic-member.outputs.is_member == 'true' | |
| uses: peter-evans/create-or-update-comment@v5 | |
| with: | |
| comment-id: ${{ github.event.comment.id }} | |
| reactions: '+1' | |
| - name: Fetch PR head commit and branch | |
| id: get-pr-info | |
| if: steps.check-elastic-member.outputs.is_member == 'true' | |
| uses: actions/github-script@v7 | |
| with: | |
| github-token: ${{ secrets.ADMIN_TOKEN_GH }} | |
| script: | | |
| const owner = context.repo.owner; | |
| const repo = context.repo.repo; | |
| const prNumber = ${{ github.event.issue.number }}; | |
| try { | |
| const { data: pr } = await github.rest.pulls.get({ | |
| owner, | |
| repo, | |
| pull_number: prNumber | |
| }); | |
| core.setOutput('commit_sha', pr.head.sha); | |
| core.setOutput('branch', pr.head.ref); | |
| core.setOutput('prefixed_branch', pr.head.label); | |
| core.setOutput('base_branch', pr.base.ref); | |
| core.setOutput('can_modify', pr.maintainer_can_modify); | |
| core.setOutput('pr_repo', pr.head.repo?.git_url); | |
| core.info(`PR #${prNumber}: ${pr.head.sha} on branch ${pr.head.ref}`); | |
| } catch (error) { | |
| core.setFailed(`Failed to fetch PR info: ${error.message}`); | |
| } | |
| - name: Trigger Buildkite pipeline | |
| if: steps.check-elastic-member.outputs.is_member == 'true' | |
| env: | |
| BUILDKITE_API_TOKEN: ${{ secrets.BUILDKITE_API_TOKEN }} | |
| PR_NUMBER: ${{ github.event.issue.number }} | |
| TRIGGERED_BY: ${{ github.event.comment.user.login }} | |
| COMMIT_SHA: ${{ steps.get-pr-info.outputs.commit_sha }} | |
| BRANCH: ${{ steps.get-pr-info.outputs.branch }} | |
| BASE_BRANCH: ${{ steps.get-pr-info.outputs.base_branch }} | |
| PREFIXED_BRANCH: ${{ steps.get-pr-info.outputs.prefixed_branch }} | |
| PR_REPO: ${{ steps.get-pr-info.outputs.pr_repo }} | |
| CAN_MODIFY: ${{ steps.get-pr-info.outputs.can_modify }} | |
| run: | | |
| curl -s -X POST "https://api.buildkite.com/v2/organizations/elastic/pipelines/elastic-charts-build/builds" \ | |
| -H "Authorization: Bearer $BUILDKITE_API_TOKEN" \ | |
| -H "Content-Type: application/json" \ | |
| -d '{ | |
| "message": "Updating screenshots - triggered by @'"${TRIGGERED_BY}"'", | |
| "commit": "'"$COMMIT_SHA"'", | |
| "branch": "'"$PREFIXED_BRANCH"'", | |
| "pull_request_id": "'"$PR_NUMBER"'", | |
| "pull_request_base_branch": "'"$BASE_BRANCH"'", | |
| "pull_request_repository": "'"$PR_REPO"'", | |
| "ignore_pipeline_branch_filters": true, | |
| "author": { | |
| "name": "'"$TRIGGERED_BY"'", | |
| "email": "'"$TRIGGERED_BY"'@users.noreply.github.com" | |
| }, | |
| "env": { | |
| "ECH_STEP_PLAYWRIGHT_UPDATE_SCREENSHOTS": "true", | |
| "GITHUB_PR_MAINTAINER_CAN_MODIFY": "'"$CAN_MODIFY"'" | |
| }, | |
| "meta_data": { | |
| "github_pr_number": "'"$PR_NUMBER"'", | |
| "triggered_via": "github-comment", | |
| "triggered_by": "'"$TRIGGERED_BY"'" | |
| } | |
| }' | jq . |