.gitlab-ci.yml Part Eight – Basics Of Include Extends Pages
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In the last blog post, we have discussed .gitlab-ci.yml features like interruptible, resource_group and trigger.
https://cloudaffaire.com/gitlab-ci-yml-part-seven-basics-of-interruptible-resource-group-trigger/
In this blog post, we will discuss .gitlab-ci.yml features like include, extends and pages.
include:
Using the include keyword, you can allow the inclusion of external YAML files. include requires the external YAML file to have the extensions .yml or .yaml, otherwise the external file will not be included. The files defined in include are deep merged with those in .gitlab-ci.yml. and always evaluated first and merged with the content of .gitlab-ci.yml, regardless of the position of the include keyword.
- include:local: include:local includes a file from the same repository as .gitlab-ci.yml. It’s referenced using full paths relative to the root directory (/).
- include:file: To include files from another private project under the same GitLab instance, use include:file. This file is referenced using full paths relative to the root directory (/).
- include:template: include:template can be used to include .gitlab-ci.yml templates that are shipped with GitLab.
- include:remote: include:remote can be used to include a file from a different location, using HTTP/HTTPS, referenced by using the full URL. The remote file must be publicly accessible through a simple GET request as authentication schemas in the remote URL is not supported.
Note: include also supports nested includes, which allow you to compose a set of includes. A total of 100 includes is allowed. Duplicate includes are considered a configuration error.
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 |
############################################################ ## .gitlab-ci.yml Part Eight : include | extends | pages ## ############################################################ ## 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 ## ------- ## include ## ------- ## Create the pipeline configuration file cd cloudaffairecicd && vi .gitlab-ci.yml --------------------- job1: stage: build script: - echo "hello from job1" include: - local: '/template/.my-gitlab-ci-template.yml' --------------------- :wq ## Create a second GitLab pipeline definitiona file that will be included mkdir template && vi template/.my-gitlab-ci-template.yml --------------------- job2: stage: build script: - echo "hello from job2" --------------------- :wq ## Push the .gitlab-ci.yml file into GitLab git add . git commit -m "jobs with include" git push |
extends:
extends defines entry names that a job that uses extends is going to inherit from. It is an alternative to using YAML anchors and is a little more flexible and readable. extends supports multi-level inheritance, however it is not recommended to use more than three levels. The maximum nesting level that is supported is 10.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
## ------- ## extends ## ------- ## Edit the pipeline configuration file cat /dev/null > .gitlab-ci.yml && vi .gitlab-ci.yml --------------------- job1: script: echo "hello from job1" stage: test job2: extends: job1 script: echo "hello from job2" --------------------- :wq ## Push the .gitlab-ci.yml file into GitLab git add . git commit -m "jobs with extends" git push |
pages:
pages is a special job that is used to upload static content to GitLab that can be used to serve your website. It has a special syntax, so the two requirements below must be met:
- Any static content must be placed under a public/ directory.
- artifacts with a path to the public/ directory must be defined.
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 |
## ----- ## pages ## ----- ## Create a public directory and index.html file mkdir public && vi public/index.html --------------------- Hello world, Debjeet here! --------------------- :wq ## Edit the pipeline configuration file cat /dev/null > .gitlab-ci.yml && vi .gitlab-ci.yml --------------------- pages: stage: deploy script: - mkdir .public - cp -r * .public - mv .public public artifacts: paths: - public only: - master --------------------- :wq ## Push the .gitlab-ci.yml file into GitLab git add . git commit -m "jobs with pages" git push # It may take up to 30 minutes before the site is available after the first deployment. # You can access your page using https:// ## 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/