Home > Enterprise >  Retrieve all commit messages between two pipeline executions in Gitlab CICD
Retrieve all commit messages between two pipeline executions in Gitlab CICD

Time:09-19

I have a Gitlab CICD pipeline that builds and deploys a Java application.

When the pipeline runs I need to get all the new Git commit messages between the last execution of the pipeline and the current one.

E.g:

The previous pipeline execution last commit was:

commit 0f22ff72089axxx
   My old commit

Since then there have been two more commits:

commit 234ff72089axxx
   My new commit 1

commit 5454422089axxx
   My new commit 2

When the pipeline executes again I need to get

My new commit 1
My new commit 2

How can this be done?

CodePudding user response:

You can use hitman's API to get info on a specific pipeline

Check whether you should take sha or before_sha when you are inspecting the pipelines state while it is running.

CodePudding user response:

You can use the Pipelines API to fetch the list of pipelines ran for your branch:

GET /projects/:id/pipelines?ref=:branch&scope=finished
  • :id is your project ID found in Settings > General.
  • :branch is your branch name which can be retrieved from the predefined CI_COMMIT_REF_NAME variable in CI.
  • scope=finished just excludes pending or running pipelines.

This will return a list of objects that looks like this:

[
    {
        "id": 4717,
        "iid": 1563,
        "project_id": 87,
        "sha": "6d019ba714992aca9ee07c24664f04e4d05aac44",
        "ref": "my-branch",
        "status": "success",
        "source": "push",
        "created_at": "2022-09-05T23:24:40.613-07:00",
        "updated_at": "2022-09-05T23:26:31.249-07:00",
        "web_url": "REDACTED"
    },
    ...
]

This is sorted by pipeline ID by default, so the first item should be the most recent completed pipeline. You'll need to get the sha from that item for use in the next step.

Then you can use git log to get the commit messages for a range of commits:

git log --pretty=format:%s --no-merges <previous-sha>..<current-sha>
  • --pretty=format:%s causes git log to only show the first line of commit messages.
  • --no-merges excluded merge commits, which seems reasonable to me but you can remove.
  • <previous-sha> is the sha we retrieved from the API response.
  • <current-sha> is the value of the predefined CI_COMMIT_SHA variable in CI.

This will output something similar to this, with the commits ordered newest to oldest:

$ git log --pretty=format:%s --no-merges <previous-sha>..<current-sha>
Commit message of commit currently being built
The previous commit message
...

You can add the --reverse flag to git log to reverse the ordering of the commits for oldest to newest.

  • Related