Skip to main content

How do I use the when: tag section in a pipeline as code?

To use the when: tag section in a GitLab pipeline as code, you can specify a job or a step to run only when the pipeline is triggered by a tag. This allows you to control the execution of jobs based on the presence of a tag in the pipeline.

Here's an example of how you can use the when: tag section:

deploy_production:
stage: deploy
script:
- echo "Deploying to production environment..."
only:
refs:
- tags
when: tag

In the above example:

The deploy_production job is defined in the deploy stage of the pipeline.

The script section includes the commands for deploying to the production environment.

The only section specifies that the job should be triggered for all tags in the repository.

The when: tag section indicates that the job should run only when the pipeline is triggered by a tag.

When this pipeline is executed, the deploy_production job will run only if the pipeline is triggered by a tag. If the pipeline is triggered by a branch or any other event, the job will be skipped.

Using the when: tag section allows you to control the deployment of jobs specifically for tag events, ensuring that certain actions, like deploying to production, are performed only when a tag is present in the pipeline.