Reputation: 492
I have 3 stages:
test
build
deploy
My goal is to detect in my gitlab_ci.yaml
file if the new branch I created were able to check if it's its first time to run on the deploy
stage. Like:
.deploy:
stage: deploy
script:
# - if ${CI_FIRST_TIME} then
- echo "please, it is my ${CI_FIRST_TIME}"
# - else
- echo "not my first time"
I'm trying to formulate something in my head but it always comes to a dead end. Would like to see if there's any clever idea to execute this.
Upvotes: 3
Views: 1518
Reputation: 81
It looks you could use the CI_COMMIT_BEFORE_SHA
environment variable (https://docs.gitlab.com/ee/ci/variables/predefined_variables.html#predefined-variables-reference) as that represents:
The previous latest commit present on a branch or tag. Is always 0000000000000000000000000000000000000000 in merge request pipelines and for the first commit in pipelines for branches or tags.
So something like (psuedo code):
.deploy:
stage: deploy
script:
# - if $CI_COMMIT_BEFORE_SHA == "0000000000000000000000000000000000000000" then
- echo "please, it is my ${CI_FIRST_TIME}"
# - else
- echo "not my first time"
Upvotes: 8
Reputation: 7649
There is no built-in way to do this currently, but you could implement something to track it on your own.
Let's say you have a mysql table 'branch_pipelines' that has the following fields: 'id', 'branch_name', 'pipeline_id', 'commit_sha'..., and 'branch_name' and 'commit_sha' together are a unique index. Then in my pipeline, I could use the mysql:8
(or whichever) image, connect to a database that stores this info, and check if the current branch is present in the table.
If not, this is the first time. If yes, you can check the commits to see how many times, and see if this commit is present to know if this is a retried job vs. a new push. Then, if this is the first pipeline for this commit, add it to the table.
The predefined variables used:
$CI_COMMIT_REF_NAME
: branch or tag name$CI_COMMIT_SHA
: the commit sha (can also use $CI_COMMIT_SHORT_SHA
for the shortened sha)The list of all predefined variables is here: https://docs.gitlab.com/ee/ci/variables/predefined_variables.html
Upvotes: 1