.gitlab-ci.yml Part Seven – Basics Of Interruptible Resource Group Trigger
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In the last blog post, we have discussed .gitlab-ci.yml features like cache, artifacts and dependencies.
https://cloudaffaire.com/gitlab-ci-yml-part-six-basics-of-cache-artifacts-dependencies/
In this blog post, we will discuss .gitlab-ci.yml features like interruptible, resource_group and trigger.
interruptible:
interruptible is used to indicate that a job should be canceled if made redundant by a newer pipeline run. Defaults to false. This value will only be used if the automatic cancellation of redundant pipelines feature is enabled. When enabled, a pipeline on the same branch will be canceled when it is made redundant by a newer pipeline run or either all jobs are set as interruptible, or any uninterruptible jobs have not started. Pending jobs are always considered interruptible.
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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
################################################################################### ## .gitlab-ci.yml Part Seven : cache | interruptible | resource_group | trigger ## ################################################################################### ## 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 ## ------------- ## interruptible ## ------------- ## Create the pipeline configuration file cd cloudaffairecicd && vi .gitlab-ci.yml --------------------- stages: - stage1 - stage2 - stage3 job1: stage: stage1 script: - echo "Can be canceled" - sleep 600 interruptible: true tags: - docker job2: stage: stage2 script: - echo "Can not be canceled if a new pipeline runs and job1 is still running" - echo "Can be cancelled if a new pipeline runs and job1 is completed" tags: - docker job3: stage: stage3 script: - echo "if job2 can not be canceled, this step also can not be cancelled, though interruptible set to true" interruptible: true tags: - docker --------------------- :wq ## Push the .gitlab-ci.yml file into GitLab git add . git commit -m "jobs with interruptible" git push # create a new pipeline immidiately and run ## Edit the pipeline configuration file cat /dev/null > .gitlab-ci.yml && vi .gitlab-ci.yml --------------------- stages: - stage1 - stage2 - stage3 job1: stage: stage1 script: - echo "hello from job1" tags: - docker job2: stage: stage2 script: - echo "hello from job2" tags: - docker job3: stage: stage3 script: - echo "hello from job3" tags: - docker --------------------- :wq ## Push the .gitlab-ci.yml file into GitLab git add . git commit -m "next pipeline execution" git push # observe, the pipeline one gets cancelled as we pushed the new pipeline # if you want to view the 2nd case, reduce the sleep value and trigger # the 2nd pipeline after 1st pipeline job1 executed |
resource_group:
Sometimes running multiples jobs or pipelines at the same time in an environment can lead to errors during the deployment. To avoid these errors, the resource_group attribute can be used to ensure that the Runner will not run certain jobs simultaneously. When the resource_group key is defined for a job in .gitlab-ci.yml, job executions are mutually exclusive across different pipelines for the same project. If multiple jobs belonging to the same resource group are enqueued simultaneously, only one of the jobs will be picked by the Runner, and the other jobs will wait until the resource_group is free.
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 |
## -------------- ## resource_group ## -------------- ## Edit the pipeline configuration file cat /dev/null > .gitlab-ci.yml && vi .gitlab-ci.yml --------------------- job1: script: - echo "hello from job1" - sleep 300 tags: - docker resource_group: prod --------------------- :wq ## Push the .gitlab-ci.yml file into GitLab git add . git commit -m "jobs with resource_group" git push # create a new pipeline immidiately and run ## Edit the pipeline configuration file cat /dev/null > .gitlab-ci.yml && vi .gitlab-ci.yml --------------------- job1: script: - echo "this job will not get triggered until the job1 from previous pipeline finishes" tags: - docker resource_group: prod --------------------- :wq ## Push the .gitlab-ci.yml file into GitLab git add . git commit -m "next pipeline execution" git push |
trigger:
trigger allows you to define downstream pipeline trigger. When a job created from trigger definition is started by GitLab, a downstream pipeline gets created.
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 |
## ------- ## trigger ## ------- ## Edit the pipeline configuration file cat /dev/null > .gitlab-ci.yml && vi .gitlab-ci.yml --------------------- job1: tags: - docker trigger: include: mydir/mynewpipeline.yml strategy: depend --------------------- :wq ## create a new pipeline configuration file that will be triggered by above pipeline mkdir mydir && vi mydir/mynewpipeline.yml --------------------- job2: script: - echo "this pipeline is triggered by above pipeline" tags: - docker --------------------- :wq ## Push the .gitlab-ci.yml file into GitLab git add . git commit -m "jobs with trigger" git push ## 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/