4

I am using a gitlab for my project where I am defining an environment variable inside "Settings->CI/CD->Environment variables" and initializing value of it as "0". Now as part of CI pipeline, I want to modify the value of this environment variable and want to increase by 1. I am trying to do this in my gulp file:

gulp.task('incrementBuildId', function()
{
    process.env.BUILD_ID = buildId + 1;
});  

However the value of environment variable isn't getting changed. Am I doing anything wrong here? Is there any other way to have a global environment variable and keep changing it's value?

7
  • can you share your .gitlab-ci.yml, it should be in the root folder of your project. Commented Mar 6, 2019 at 6:07
  • I am not sure how would that help. I am not doing anything inside my .gitlab-ci.yml file apart from calling incrementBuildId as part of one of my stage. Commented Mar 6, 2019 at 6:28
  • The intention was to know how are you triggering the gulp task (npm), if your gulp-task is a phase inside gitlab-ci.yml then you could easily increment it. Commented Mar 6, 2019 at 6:42
  • Also, you should know, CI Environment variables can only be modified through the UI, you may increment it and use it in a pipeline trigger but when the next pipeline will trigger it will take the initial value. :) It's a good read - here docs.gitlab.com/ee/ci/variables Commented Mar 6, 2019 at 6:44
  • 1
    Ahh I see, so is there anything else I can do to set value globally from CI pipeline? So when next time pipeline gets triggered, the new value should get fetched. Commented Mar 6, 2019 at 6:50

1 Answer 1

2

As of now, there is no way to modify gitlab environment variable to persist. However, you could look for any vault or api-server to do the same.

Option 1: (Recommended)

For your case, if I'm not wrong you want to set the build ID as last build ID +1 for that purpose gitlab allows a pipeline to commit to a specific branch, so you could have the pipeline read a file which contains the last build ID and then set the current build ID as last build ID+1 then commit it to branch and repeat the process for every build.

CI_COMMIT_SHA: is unique per commit CI_PIPELINE_ID: is Unique per pipeline CI_JOB_ID: is unique per Pipeline

So, You could use - CI_PIPELINE_ID + CI_JOB_ID - CI_COMMIT_SHA + CI_JOB_ID

This will produce unique values even if you run the same pipeline (CI) again and again.

Option 2:

However, I would recommend you give the build ID as the pipeline ID (known as CI_PIPELINE_ID) which is unique. example,

build_id = v1.0.${CI_PIPELINE_ID} #during build phase
#if current pipeline id = 3000, then build_id will be v1.0.3000

You may find the variables available with gitlab here. Some variables are unique like CI_JOB_ID, CI_COMMIT_SHA, CI_PIPELINE_ID etc., (for gitlab v9.0+).

Sign up to request clarification or add additional context in comments.

8 Comments

I think 2nd option will not be much useful for me as I don't have control on ID which is getting generated. I didn't get much how would 1st option work, did you mean to have a file in my project which specifies the value of BUILD_ID and I can keep changing that value as part of specific stage?
Let's say that there is a file in your project containing the last build id now in your pipeline you can read that file and increment it and commit it back to the branch such that next build will read the current incremented build id.
Hmm sounds like viable. I will give it a try
Some time has passed since this question was asked, but I'm facing a similar situation. The problem is, let's say that I use Option 1 and I increment a value of a file when the pipeline runs, then I commit+push this file during the pipeline. My pipeline fires, say, when I commit on the branch master. This causes my pipeline-commit (the one committed+pushed in the pipeline) to trigger a second pipeline, starting an infinite loop. One ends with this situation. How can one avoid it?
Should one push to a different branch from master?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.