ismail
June 13, 2024, 9:43am
1
Hi Team,
I am using this action to trigger a buildkite job:
name: Trigger Buildkite Build on PR Close
on:
pull_request:
types: [closed]
jobs:
trigger-buildkite-on-pr-close:
runs-on: ubuntu-latest
steps:
- name: Trigger Buildkite Build
env:
BUILDKITE_API_TOKEN: ${{ secrets.BUILDKITE_API_TOKEN }}
run: |
echo "Triggering Buildkite build for merged/closed pull request..."
curl -X POST "https://api.buildkite.com/v2/organizations/my-org/pipelines/my-job/builds" \
-H "Authorization: Bearer $BUILDKITE_API_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"commit": "${{ github.sha }}",
"branch": "${{ github.ref_name }}",
"message": "Build triggered by GitHub Action on PR close",
"author": {
"name": "${{ github.event.pull_request.user.login }}"
},
"creator": {
"name": "${{ github.event.pull_request.user.login }}"
},
"env": {
"PR_NUMBER": "${{ github.event.number }}"
}
}'
The problem is every time this runs, buildkite shows, the name of the person who created the token, rather than the user who triggered this flow.
This behavior is confusing and we want a way to show the actual user who triggered it. Can you please advise?
Thanks and Regards,
Ismail
Hello, @ismail ! We’ll need to take a closer look at the pipeline where you see this happening. Could you please email a link to it to support@buildkite.com ?
Many thanks in advance!
Karen
ismail
June 13, 2024, 10:50am
3
Shared email with details.
Thanks, @ismail ! We received it and are looking.
Best!
Karen
Hello @ismail ,
As my colleague explained via email, the outcome you’re getting is expected.
That being said, I’d like to share an alternative approach that you might find useful. It boils down to:
Pass the name of the GitHub user via env
when making the API call.
Add a step that prints the environment variable’s value via an annotation .
Your GitHub action would then look like this:
name: Trigger Buildkite Build on PR Close
on:
pull_request:
types: [closed]
jobs:
trigger-buildkite-on-pr-close:
runs-on: ubuntu-latest
steps:
- name: Trigger Buildkite Build
env:
BUILDKITE_API_TOKEN: ${{ secrets.BUILDKITE_API_TOKEN }}
run: |
echo "Triggering Buildkite build for merged/closed pull request..."
curl -X POST "https://api.buildkite.com/v2/organizations/my-org/pipelines/my-job/builds" \
-H "Authorization: Bearer $BUILDKITE_API_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"commit": "${{ github.sha }}",
"branch": "${{ github.ref_name }}",
"message": "Build triggered by GitHub Action on PR close",
"author": {
"name": "${{ github.event.pull_request.user.login }}"
},
"env": {
"PR_NUMBER": "${{ github.event.number }}",
"GH_USER": "${{ github.event.pull_request.user.login }}"
}
}'
And the additional step in your pipeline would be:
- label: Identity of the GitHub user who closed the PR
if: build.env("GH_USER") != null && build.env("GH_USER") != ""
command:
- buildkite-agent annotate "This build was triggered after $GH_USER closed the PR #$PR_NUMBER" --style 'info'
With this approach, the build would still show as being triggered by the bearer of the Buildkite API token. But the annotation would clearly mention the GitHub user whose action triggered the GitHub action that in turn triggered the Buildkite build.
I hope this helps.
/Yann
ismail
June 17, 2024, 4:48am
6
Hey Yann.
This looks promising. I will be using it.
Thanks and Regards,
Ismail
1 Like