.gitlab-ci.yml Part Six – Basics Of Cache Artifacts Dependencies
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In the last blog post, we have discussed .gitlab-ci.yml features like environment, need, retry, timeout and parallel.
https://cloudaffaire.com/gitlab-ci-yml-part-five-basics-of-environment-need-retry-timeout-parallel/
In this blog post, we will discuss .gitlab-ci.yml features like cache, artifacts and dependencies.
cache:
cache is used to specify a list of files and directories which should be cached between jobs. You can only use paths that are within the local working copy. If cache is defined outside the scope of jobs, it means it is set globally and all jobs will use that definition. Cache supports below configuration parameters:
- cache:paths: Use the paths directive to choose which files or directories will be cached. Paths are relative to the project directory ($CI_PROJECT_DIR) and cannot directly link outside it.
- cache:key: The key directive allows you to define the affinity of caching between jobs, allowing to have a single cache for all jobs, cache per-job, cache per-branch or any other way that fits your workflow. This way, you can fine tune caching, allowing you to cache data between different jobs or even different branches.
- cache:key:files: The cache:key:files keyword extends the cache:key functionality by making it easier to reuse some caches, and rebuild them less often, which will speed up subsequent pipeline runs. When you include cache:key:files, you must also list the project files that will be used to generate the key, up to a maximum of two files.
- cache:key:prefix: The prefix parameter adds extra functionality to key:files by allowing the key to be composed of the given prefix combined with the SHA computed for cache:key:files.
- cache:untracked: Set untracked: true to cache all files that are untracked in your Git repository.
- cache:policy: The default behaviour of a caching job is to download the files at the start of execution, and to re-upload them at the end. This allows any changes made by the job to be persisted for future runs, and is known as the pull-push cache policy.
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 |
################################################################ ## .gitlab-ci.yml Part Six: cache | artifacts | dependencies ## ################################################################ ## 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 ## ---- ## cache ## ---- ## Create the pipeline configuration file cd cloudaffairecicd && vi .gitlab-ci.yml --------------------- stages: - one - two - three job1: stage: one script: - mkdir mydir - echo "hello from job1" > mydir/myfile1 tags: - docker cache: key: "$CI_COMMIT_REF_SLUG" paths: - mydir/myfile1 job2: stage: two script: - echo "hello from job2" >> mydir/myfile1 - cat mydir/myfile1 tags: - docker cache: key: "$CI_COMMIT_REF_SLUG" paths: - mydir/myfile1 job3: stage: three script: - cat mydir/myfile1 tags: - docker cache: key: "$CI_COMMIT_REF_SLUG" paths: - mydir/myfile1 --------------------- :wq ## Push the .gitlab-ci.yml file into GitLab git add . git commit -m "jobs with cache" git push |
artifacts:
artifacts is used to specify a list of files and directories which should be attached to the job when it succeeds, fails, or always. The artifacts will be sent to GitLab after the job finishes and will be available for download in the GitLab UI. Artifacts supports below configuration parameter:
- artifacts:path: Paths are relative to the project directory ($CI_PROJECT_DIR) and cannot directly link outside it.
- artifacts:expose_as: The expose_as keyword can be used to expose job artifacts in the merge request UI.
- artifacts:name: The name directive allows you to define the name of the created artifacts archive. That way, you can have a unique name for every archive which could be useful when you’d like to download the archive from GitLab.
- artifacts:untracked: artifacts:untracked is used to add all Git untracked files as artifacts (along to the paths defined in artifacts:paths).
- artifacts:when: artifacts:when is used to upload artifacts on job failure or despite the failure. artifacts:when can have on_success, on_failure and always as values.
- artifacts:expire_in: expire_in allows you to specify how long artifacts should live before they expire and are therefore deleted, counting from the time they are uploaded and stored on GitLab. f the expiry time is not defined, it defaults to the instance wide setting (30 days by default, forever on GitLab.com).
- artifacts:reports: The reports keyword is used for collecting test reports, code quality reports, and security reports from jobs. It also exposes these reports in GitLab’s UI (merge requests, pipeline views, and security dashboards).
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 |
## --------- ## artifacts ## --------- ## Edit the pipeline configuration file cat /dev/null > .gitlab-ci.yml && vi .gitlab-ci.yml --------------------- stages: - one - two - three job1: stage: one script: - echo "hello from job1" > myartifact.txt tags: - docker artifacts: paths: - myartifact.txt expire_in: 1 week job2: stage: two script: - echo "hello from job2" >> myartifact.txt tags: - docker artifacts: paths: - myartifact.txt expire_in: 1 week job3: stage: three script: - echo "hello from job3" >> myartifact.txt tags: - docker artifacts: paths: - myartifact.txt expire_in: 1 week --------------------- :wq ## Push the .gitlab-ci.yml file into GitLab git add . git commit -m "jobs with artifact" git push |
You can download artifacts from the pipeline.
cache vs artifacts:
Caches:
- Are disabled if not defined globally or per job (using cache:).
- Are available for all jobs in your .gitlab-ci.yml if enabled globally.
- Can be used in subsequent pipelines by the same job in which the cache was created (if not defined globally).
- Are stored where the Runner is installed and uploaded to S3 if distributed cache is enabled.
- If defined per job, are used:
- By the same job in a subsequent pipeline.
- By subsequent jobs in the same pipeline, if they have identical dependencies.
Artifacts:
- Are disabled if not defined per job (using artifacts:).
- Can only be enabled per job, not globally.
- Are created during a pipeline and can be used by the subsequent jobs of that currently active pipeline.
- Are always uploaded to GitLab (known as coordinator).
- Can have an expiration value for controlling disk usage (30 days by default).
dependencies:
By default, all artifacts from all previous stages are passed, but you can use the dependencies parameter to define a limited list of jobs (or no jobs) to fetch artifacts from. To use this feature, define dependencies in context of the job and pass a list of all previous jobs from which the artifacts should be downloaded. You can only define jobs from stages that are executed before the current one. An error will be shown if you define jobs from the current stage or next ones. Defining an empty array will skip downloading any artifacts for that job. The status of the previous job is not considered when using dependencies, so if it failed or it is a manual job that was not run, no error occurs.
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 |
## ------------ ## dependencies ## ------------ ## Edit the pipeline configuration file cat /dev/null > .gitlab-ci.yml && vi .gitlab-ci.yml --------------------- stages: - one - two - three - four job1: stage: one script: - echo "hello from job1" > myartifact.txt tags: - docker artifacts: paths: - myartifact.txt expire_in: 1 week job2: stage: two script: - echo "hello from job2" >> myartifact.txt tags: - docker artifacts: paths: - myartifact.txt expire_in: 1 week job3: stage: three script: - cat myartifact.txt - echo "hello from job2 is not available" tags: - docker dependencies: - job1 job4: stage: four script: - cat myartifact.txt - echo "hello from job1 and Job2 available" tags: - docker --------------------- :wq ## Push the .gitlab-ci.yml file into GitLab git add . git commit -m "jobs with artifact dependencies" 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/