Context
After creating a general post-receive
for a GitLab server, I noticed it gets triggered directly after a new commit is detected in any repository. However, I would like the post-receive
script to do something with the build status of the GitLab Runner CI on the commit that triggered the post-receive
script.
Approach
Based on this question and answer, I wrote a post-receive
script that gets the commit and repository, and I tried to get the build status from that commit from within the GitLab docker:
#!/bin/bash
read oldrev newrev refname
echo "Previous Commit: $oldrev"
echo "New/latest Commit: $newrev"
echo "Repository name: $refname"
# Get build status of $newrev
curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/17/$refname/commits/$newrev/statuses
However, that API call does not work from within the Docker environment (which is from where the post-receive
script runs).
Docker GitLab Build Status File locations
I also found the build status badges inside the Docker, they are located in: /opt/gitlab/embedded/service/gitlab-rails/public/assets/
. However I do not (yet) know how to decode their filenames. For example, the build status badge accompanying Job #3
, of commit: 9514d16aafc1d741ba6a9ff47718d632fa8d435b
has filename: icons-6d7d4be41eac996c72b30eac2f28399ac8c6eda840a6fe8762fc1b84b30d5a2d.svg
. Basically I do not know to which commit/repository that build status badge belongs.
On the other hand, I have found the location of the job logs in the hashed path of the repository:
/var/opt/gitlab/gitlab-rails/shared/artifacts/d4/73/d4735e3a265e16eee03f59718b9b5d03019c07d8b6c51f90da3a666eec13ab35/2021_10_09/1/1/job.log
/var/opt/gitlab/gitlab-rails/shared/artifacts/d4/73/d4735e3a265e16eee03f59718b9b5d03019c07d8b6c51f90da3a666eec13ab35/2021_10_14/3/3/job.log
Which each in turn contain their respective commit and branch as:
Checking out 9514d16a as master...
So in principle I could scan the repository path and accompanying job logs until I found the job.log
that contains the commit of the post-receive
script (for e.g. 5 minutes, to account for the delay between the commit and the starting of the GitLab Runner CI), and then search for the build status output in that job.log
(e.g. Job succeeded
) (for e.g. 60 minutes to allow for long jobs). However, that seems like a convoluted work-around.
Question
Hence, I was wondering, *Is there a better/faster/robuster method to get the GitLab Runner CI build status of the commit that triggered the general post-receive
script of a GitLab server, inside that triggered instance/run of the post-receive
script?