.gitlab-ci.yml Part Two – Basics Of Only Except
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In the last blog post, we have discussed the basics of jobs and pipelines in GitLab CI/CD.
https://cloudaffaire.com/gitlab-ci-yml-part-one-basics-of-script-image-services-stages/
In this blog post, we will discuss how to control the pipeline job executing with only and except.
only and except:
only and except are two parameters that set a job policy to limit when jobs are created. only defines the conditions if true, the job will run. except defines the conditions if true, the job will not run. Using only or except or both, you can control when the job will be triggered in a pipeline.
Note: The rules syntax is now the preferred method of setting job policies. only and except are candidates for deprecation, and may be removed in the future.
only and except usage:
- only and except are inclusive. If both only and except are defined in a job specification, the ref is filtered by only and except.
- only and except allow the use of regular expressions.
- only and except allow to specify a repository path to filter jobs for forks.
only and except configurations:
- branches: When a Git reference of a pipeline is a branch.
- tags: When a Git reference of a pipeline is a tag.
- api: When pipeline has been triggered by a second pipelines API (not triggers API).
- external: When using CI services other than GitLab.
- pipelines: For multi-project triggers, created using the API with CI_JOB_TOKEN.
- pushes: Pipeline is triggered by a git push by the user.
- schedules: For scheduled pipelines.
- triggers: For pipelines created using a trigger token.
- web: For pipelines created using the Run pipeline button in GitLab UI.
- merge_requests: When a merge request is created or updated.
- external_pull_requests: When an external pull request on GitHub is created or updated.
- chat: For jobs created using a GitLab ChatOps command.
- refs: The refs strategy can take the same values as all the above values.
- kubernetes: The Kubernetes strategy accepts only the active keyword. Job is going to be created only when the Kubernetes service is active in the project
- variables: The variables keyword is used to define variables expressions.
- changes: Using the changes keyword with only or except makes it possible to define if a job should be created based on files modified by a Git push event.
Next, we are going to trigger some pipelies based on specific conditions to demonstarte the use cases of only and except. In the demo we have only used only for conditions for triggering the pipeline, but you can use except as well for not triggereing the pipeline.
.gitlab-ci.yml Part Two – Basics Of Only Except:
How To Trigger GitLab CI/CD Pipeline For Specific Branch:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
############################################# ## .gitlab-ci.yml Part Two: Only | Except ## ############################################# ## Prerequisites: GitLab Access Token ## https://cloudaffaire.com/how-to-create-a-gitlab-project-using-api/ ## One CentOs system with internet access ## git, curl and jq package installed ## ------------------------------------ ## Create A GitLab Project To Play With ## ------------------------------------ ## Create a project in GitLab ## GitLab Console => Projects => Your Projects => New Project => PROJECT_ID=$(curl --silent --header "PRIVATE-TOKEN: -XPOST "https://gitlab.com/api/v4/projects?name=cloudaffairecicd&visibility=private&initialize_with_readme=true" | jq '.id') ## Get the ssh clone url for your project ## GitLab Console => Projects => Your Projects => GIT_SSH_CLONE_URL=$(curl --silent --header "PRIVATE-TOKEN: -XGET "https://gitlab.com/api/v4/projects/$PROJECT_ID" | jq -r '.ssh_url_to_repo') ## Clone your GitLab project repository git clone $GIT_SSH_CLONE_URL ## -------------- ## only: branches ## -------------- ## Create the pipeline configuration file cd cloudaffairecicd && vi .gitlab-ci.yml --------------------- mygitlabjob_one: script: echo "pipeline not triggered as in master branch" only: - mybranch ---------------------- :wq ## Push the .gitlab-ci.yml file into GitLab git add . git commit -m "pipeline will trigger" git push # Pipeline will not get triggered as we are in master branch ## Create a branch named mybranch git checkout -b mybranch ## Edit the pipeline configuration file cat /dev/null > .gitlab-ci.yml && vi .gitlab-ci.yml --------------------- mygitlabjob_one: script: echo "pipeline is triggered as in mybranch branch" only: - mybranch ---------------------- :wq ## Push the .gitlab-ci.yml file into GitLab git add . git commit -m "pipeline will be triggered" git push -u origin mybranch # Pipeline will get triggered as we are in mybranch branch ## Delete the branch mybranch git checkout master git branch -D mybranch git push origin --delete mybranch |
How To Trigger The GitLab CI/CD Pipeline For Specific Tag:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
## ---------- ## only: tags ## ---------- ## Edit the pipeline configuration file cat /dev/null > .gitlab-ci.yml && vi .gitlab-ci.yml --------------------- mygitlabjob_one: script: echo "pipeline is not triggered as no tag exists" only: - mytag ---------------------- :wq ## Push the .gitlab-ci.yml file into GitLab git add . git commit -m "pipeline will not triggered" git push # Pipeline will not get triggered as mytag does not exist ## Create a tag and push git tag mytag git push origin mytag # Pipeline will get triggered as mytag exist ## Delete the tag git tag -d mytag git push origin :mytag |
How To Trigger The GitLab CI/CD Pipeline For Specific Commit Message:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
## --------------- ## only: variables ## --------------- ## Edit the pipeline configuration file cat /dev/null > .gitlab-ci.yml && vi .gitlab-ci.yml --------------------- mygitlabjob_one: script: echo "pipeline is not triggered as no specific commit message" only: variables: - $CI_COMMIT_MESSAGE =~ /mycommitmessage/ ---------------------- :wq ## Push the .gitlab-ci.yml file into GitLab git add . git commit -m "no specific commit message" git push # pipeline will not get triggered as we didn't gave the specific commit message ## Edit the pipeline configuration file cat /dev/null > .gitlab-ci.yml && vi .gitlab-ci.yml --------------------- mygitlabjob_one: script: echo "pipeline is triggered as specific commit message provided" only: variables: - $CI_COMMIT_MESSAGE =~ /mycommitmessage/ ---------------------- :wq ## Push the .gitlab-ci.yml file into GitLab git add . git commit -m "mycommitmessage" git push # pipeline will get triggered as we have given the specific commit message ## Delete your project curl --silent --header "PRIVATE-TOKEN: -XDELETE "https://gitlab.com/api/v4/projects/$PROJECT_ID" | jq '.' cd .. && rm -rf cloudaffairecicd |
Hope you enjoyed this article. In the next blog post, we will continue our discussion .gitlab-ci.yml.
To get more details on GitLab you can follow the below link.
https://docs.gitlab.com/ee/README.html
To Get more details on Git you can follow the below links.
https://cloudaffaire.com/category/devops/git/