Gitlab CI script: exclude branches

2 min read 06-10-2024
Gitlab CI script: exclude branches


Mastering GitLab CI: Excluding Branches for Efficient Pipelines

GitLab CI/CD pipelines are powerful tools for automating your development workflows. But sometimes, you might want to exclude certain branches from triggering specific jobs or the entire pipeline. This is where the only and except keywords come in handy.

Scenario: A Common Use Case

Imagine you have a GitLab CI script that automatically deploys your application to production whenever a new commit is pushed to the main branch. However, you want to prevent this from happening for commits pushed to the development branch, as these commits are still under development and might not be production-ready.

Here's a simplified example of the original GitLab CI script:

stages:
  - deploy

deploy:
  stage: deploy
  script:
    - echo "Deploying to production!"

This script would trigger the deploy job for all branches, including development.

Excluding Branches for a Clean Workflow

To exclude the development branch from triggering the deploy job, we can use the except keyword in the deploy job configuration:

stages:
  - deploy

deploy:
  stage: deploy
  script:
    - echo "Deploying to production!"
  except:
    - branches:
      - development

This modification ensures that the deploy job only runs for branches other than development.

Deeper Dive: only vs. except

While except explicitly excludes branches, the only keyword defines which branches should trigger a specific job.

Here's an example of using only to run the deploy job only for the main branch:

stages:
  - deploy

deploy:
  stage: deploy
  script:
    - echo "Deploying to production!"
  only:
    - branches:
      - main

Benefits of Using only and except

  • Improved Pipeline Control: You can easily customize your pipelines to run specific jobs for different branches, ensuring a streamlined workflow.
  • Reduced Errors: By excluding unwanted branches, you reduce the risk of accidentally deploying unstable code to production.
  • Clearer Configuration: only and except provide clear and concise syntax for defining pipeline behavior.

Best Practices for Branch Exclusion

  • Use Specific Branches: Avoid using wildcard characters like * or regular expressions, as this can lead to unintended behavior.
  • Test Thoroughly: After implementing branch exclusion, thoroughly test your pipelines to ensure they function as expected.
  • Document Changes: Clearly document any changes to your GitLab CI script, especially when using only or except keywords.

Beyond Branches: Advanced Exclusion Options

only and except can be used to target other pipeline triggers besides branches. You can exclude specific tags, pipelines, or even specific commit messages. Refer to the GitLab CI/CD documentation for a comprehensive list of supported options.

Conclusion

By leveraging only and except in your GitLab CI scripts, you can fine-tune your pipelines to work efficiently for various scenarios. This will lead to a more controlled and secure development process, ultimately benefiting your entire team.

References: