[Infrastructure as Code (IaC)] Triggering GitLab CI/CD pipelines through the API
Triggering GitLab pipelines through the API
GitLab CI/CD is a tool for software development using the continuous methodologies:
GitLab Triggers can be used to force a pipeline rerun of a specific ref (branch or tag) with an API call.
Create a personal access token
Personal access tokens can be an alternative to OAuth2
and used to:
-
Authenticate with the GitLab API.
-
Authenticate with Git using HTTP Basic Authentication.
You can create as many personal access tokens as you like.
-
In the top-right corner, select your avatar.
-
Select Edit profile.
-
On the left sidebar, select Access Tokens.
-
Enter a name and optional expiry date for the token.
-
Select the desired scopes.
-
Select Create personal access token.
Adding a new trigger
Go to your Settings
> CI/CD
under Triggers
to add a new trigger. The Add trigger button creates a new token which you can then use to trigger a rerun of this particular project’s pipeline.
Every new trigger you create, gets assigned a different token which you can then use inside your scripts or .gitlab-ci.yml
. You also have a nice overview of the time the triggers were last used.
In the following examples, you can see the exact API call you need to make in order to rebuild a specific ref (branch or tag) with a trigger token.
All you need to do is replace the TOKEN and REF_NAME with the trigger token and the branch or tag name respectively.
Use cURL
Copy one of the tokens above, set your branch or tag name, and that reference will be rebuilt.
1 | curl -X POST \ |
Use .gitlab-ci.yml
In the .gitlab-ci.yml
of another project, include the following snippet. The project will be rebuilt at the end of the pipeline.
1 | trigger_build: |
Use webhook
Add the following webhook to another project for Push and Tag push events. The project will be rebuilt at the corresponding event.
1 | https://<Your GitLab URL>/api/v4/projects/<Your Project>/ref/REF_NAME/trigger/pipeline?token=TOKEN |
Pass job variables
Add variables[VARIABLE]=VALUE
to an API request. Variable values can be used to distinguish between triggered pipelines and normal pipelines. With cURL:
1 | curl -X POST \ |
With webhook:
1 | https://<Your GitLab URL>/api/v4/projects/<Your Project>/ref/REF_NAME/trigger/pipeline?token=TOKEN&variables[RUN_NIGHTLY_BUILD]=true |
See Triggering pipelines through the API | GitLab - https://docs.gitlab.com/ee/ci/triggers/ to learn more.
Add a CI/CD variable to a project
You can add CI/CD variables to a project’s settings. Only project members with the Maintainer role can add or update project CI/CD variables. To keep a CI/CD variable secret, put it in the project settings, not in the .gitlab-ci.yml file.
To add or update variables in the project settings:
-
- Go to your project’s Settings > CI/CD and expand the Variables section.
-
- Select the Add Variable button and fill in the details:
-
Key: Must be one line, with no spaces, using only letters, numbers, or _.
-
Value: No limitations.
-
Type: File or Variable.
-
Environment scope: (Optional) All, or specific environments.
-
Protect variable (Optional): If selected, the variable is only available in pipelines that run on protected branches or tags.
-
Mask variable (Optional): If selected, the variable’s Value is masked in job logs. The variable fails to save if the value does not meet the masking requirements.
There are a few Variables should be defiend.
-
TRIGGER_TOKEN
-
BRANCH
-
GITLAB_URL
-
PROJECT_ID
-
HEADER, for example:
HEADER="PRIVATE-TOKEN: <ACCESS_TOKEN>"
Usages
In the .gitlab-ci.yml
, use the following to triggering a pipeline and waiting for the pipeline status from pending
to success
.
1 | testing: |
See The .gitlab-ci.yml
file | GitLab- https://docs.gitlab.com/ee/ci/yaml/gitlab_ci_yaml.html to learn more.
FAQs
script config should be an array of strings or a string
PRIVATE-TOKEN: ${ACCESS_TOKEN}
within .gitlab.ci.yml
cause that syntax error.
1 | script: |
Use GitLab CI/CD environment variables | GitLab - https://docs.gitlab.com/ee/ci/variables/ to solve it.
Variable value is empty
If the variable is marked as protected
, please remember to also mark the corresponding branch as protected
, otherwise the obtained variable value is empty.
References
[1] Triggering pipelines through the API | GitLab - https://docs.gitlab.com/ee/ci/triggers/
[3] GitLab CI/CD environment variables | GitLab - https://docs.gitlab.com/ee/ci/variables/
[5] The .gitlab-ci.yml
file | GitLab- https://docs.gitlab.com/ee/ci/yaml/gitlab_ci_yaml.html
[6] Loops - Shell Scripting Tutorial - https://www.shellscript.sh/loops.html
[7] Test - Shell Scripting Tutorial - https://www.shellscript.sh/test.html
[8] GitLab CI/CD | GitLab - https://docs.gitlab.com/ee/ci/
[9] Iterate faster, innovate together | GitLab - https://about.gitlab.com/