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

Modified on Wed, 21 Jun 2023 at 03:01 PM

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.


If you have further questions or need assistance with implementing the when: tag section or optimizing your pipelines, feel free to reach out to Cloud-Runner. We specialize in helping developers maximize the efficiency and performance of their CI/CD workflows. Visit cloud-runner.com to learn more about our services and how we can support your pipeline automation needs.

Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select atleast one of the reasons

Feedback sent

We appreciate your effort and will try to fix the article