I’m using GitHub Branch Source Plugin plugin on Multibranch Pipeline. Integration with GitHub is done through GitHub App configured according to official Cloudbees documentation.
Just wondering, is there a way to detect in the pipeline the name/userid of the GitHub App which is currently executing GitHub API calls? I’m trying to do a one time comment into GitHub PR so I’m searching through all comments in the PR, and if at least one such comment from my GitHub App is detected the code bails out:
def comment_detected = false
for (comment in pullRequest.comments) {
if (comment.user == 'my-github-app[bot]') {
comment_detected = true
break;
}
}
if (!comment_detected) {
pullRequest.comment('PR preview website is deployed at: https://mywebsite.example.com/' + env.CHANGE_ID + '/home')
}
As you see currently I have GitHub App name hardcoded. I would rather do something like if (comment.user == github.context.app) {.
Is such thing possible?
Or maybe there is a better way to do a one time action on successful build without checking the destination for the data (comments) which must be posted?
As far as I know, the information about the GitHub App is not directly exposed to the pipeline.
However, you could maybe use the withCredentials step to inject the GitHub App ID into your pipeline from a Jenkins credential.
It’s not pretty, it’s still hardcoded somewhere, but I can’t think of anything better.
First, you would need to store the GitHub App ID as a secret text credential.
Then, you could use the withCredentials step to inject this credential into your pipeline.
Here’s how you could do it (untested):
withCredentials([string(credentialsId: 'github-app-id', variable: 'GITHUB_APP_ID')]) {
def comment_detected = false
for (comment in pullRequest.comments) {
if (comment.user == "${GITHUB_APP_ID}[bot]") {
comment_detected = true
break;
}
}
if (!comment_detected) {
pullRequest.comment('PR preview website is deployed at: https://mywebsite.example.com/' + env.CHANGE_ID + '/home')
}
}
In this example, replace ‘github-app-id’ with the ID of the Jenkins credential where you stored the GitHub App ID.
Of course, this approach requires you to manually update the Jenkins credential if the GitHub App ID changes.
As for your second question, the approach you’re using is (to me at least) a common way to ensure a one-time action in a Jenkins pipeline.
You can ensure that the comment is only posted once by checking for an existing comment before posting a new one.
If you want to avoid checking the destination for existing data, you would need to store the state of whether the comment has been posted somewhere else, such as in an external database or a file.
Unfortunately, this would likely add unneeded complexity to your pipeline.
I actually already have GitHub App ID in credentials, however this doesn’t translate into username via GitHub API. Or at least I didn’t find a way to do this. And GitHub Pipeline plugin exposes just username GitHub - jenkinsci/pipeline-github-plugin: Pipeline: GitHub .