.gitlab-ci.yml Part Three – Basics Of Rules
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In the last blog post, we have discussed how to control the pipeline job executing with only and except.
https://cloudaffaire.com/gitlab-ci-yml-part-two-basics-of-only-except/
In this blog post, we will discuss how to perform the same with new GitLab feature rules.
Rules:
Rules allow for a list of individual rule objects to be evaluated in order, until one matches and dynamically provides attributes to the job. Note that rules cannot be used in combination with only/except since it is intended to replace that functionality. If you attempt to do this the linter will return a key may not be used with rules error.
Available rule clauses include:
- if (similar to only:variables)
- changes (same as only:changes)
- exists
.gitlab-ci.yml Part Two – Job Conditions Using Only And Except
rules – if:
rules:if differs slightly from only:variables by accepting only a single expression string, rather than an array of them. Any set of expressions to be evaluated should be conjoined into a single expression using && or ||, and use the variable matching syntax. If none of the provided rules match, the job will be set to when:never, and not included in the pipeline. If rules:when is not included in the configuration at all, the behavior defaults to job:when, which continues to default to on_success.
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 |
###################################### ## .gitlab-ci.yml Part Three: Rules ## ####################################### ## 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 ## --------- ## rules: if ## --------- ## Create the pipeline configuration file cd cloudaffairecicd && vi .gitlab-ci.yml --------------------- mygitlabjob_one: script: echo "Hello World" rules: - if: '$CI_COMMIT_MESSAGE =~ /mycommitmessage/ && $CI_COMMIT_BRANCH == "master"' --------------------- :wq ## Push the .gitlab-ci.yml file into GitLab git add . git commit -m "mycommitmessage" git push # Pipeline gets triggered as we gave the specific commit message in master branch |
rules – changes:
rules: changes work exactly the same way as only: changes and except: changes, accepting an array of paths. Similarly, it will always return true if there is no Git push event.
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 |
## -------------- ## rules: changes ## -------------- ## Edit the pipeline configuration file cat /dev/null > .gitlab-ci.yml && vi .gitlab-ci.yml --------------------- mygitlabjob_one: script: echo "Hello World" rules: - changes: - myfile --------------------- :wq ## Push the .gitlab-ci.yml file into GitLab git add . git commit -m "pipeline will not triggered" git push ## Pipeline does not get triggered as myfile does not exist ## Create a file named myfile echo "this is my file" > myfile ## Push the .gitlab-ci.yml file into GitLab git add . git commit -m "pipeline will be triggered" git push ## Pipeline gets triggered as myfile exist |
rules – exist:
exists accepts an array of paths and will match if any of these paths exist as files in the repository. You can also use glob patterns to match multiple files in any directory within the repository.
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 |
## ------------- ## rules: exists ## ------------- ## Edit the pipeline configuration file cat /dev/null > .gitlab-ci.yml && vi .gitlab-ci.yml --------------------- mygitlabjob_one: script: echo "Hello World" rules: - exists: - mypath/mynewfile --------------------- :wq ## Push the .gitlab-ci.yml file into GitLab git add . git commit -m "pipeline will not triggered" git push ## Pipeline does not get triggered as mypath/mynewfile does not exist ## Create a file named myfile mkdir mypath && echo "this is my new file" > mypath/mynewfile ## Push the .gitlab-ci.yml file into GitLab git add . git commit -m "pipeline will be triggered" git push ## Pipeline gets triggered as mypath/mynewfile exist ## 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/