0

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?

a.t.
  • 2,002
  • 3
  • 26
  • 66
  • 1
    Git runs post-receive scripts immediately after receiving and accepting the commit. "Acceptance" is determined by: the pre-receive and update scripts (if present) allowed the commit, as did Git's own internal checks. There's no CI here, just a commit entering a repository. If the update script actually runs the CI to completion first, that would mean it's done when the post-receive script runs, but that would create other problems which would likely be insurmountable. So it seems likely that the CI code doesn't even *start* until around the same time the post-receive script runs. – torek Oct 14 '21 at 13:21
  • 1
    In any case, how you can wait for a particular build, or even identify any particular CI run from within a post-receive script, will depend entirely on GitLab. – torek Oct 14 '21 at 13:23
  • Thank you for the clarification, I initially was not completely clear on whether the `post-receive` was particular to GitLab or Git, its behaviour is defined by Git, and the CI's behaviour is defined by GitLab's runner. After processing your comments, I narrowed down the question to finding the build status of a particular commit from within the GitLab Docker. – a.t. Oct 14 '21 at 15:16

0 Answers0