Introduce GitlabCI
Abstract
This template which store short
yaml
to show how can work withGitlabCI
and note some specify for use case
More information about Gitlab CI/CD and Variables. Go check
General
- Gitlab Documentation - General Documentation about Gitlab
- GitLab Runner - Information about Gitlab Runner and configuration
- GitLab CI/CD examples - GitlabCI/CD Collections
- Gitlab Permissions and roles - About Role and Permissions of account in GitLab organization
Syntax
- Gitlab CI/CD Syntax - Introduce about Gitlab Syntax and usage
- Variables & Predefine variables - GitlabCI Variables and Predefine variables
- Scripts and job logs - Use for format script and log inside job
Features
- GitlabCI Services - Use service keyword GitlabCI
- Run your CI/CD jobs in Docker containers - Setup GitlabCI in docker+machine
- Use Docker to build Docker images - Run Dind to build image inside GitlabCI
- DRY development: A cheatsheet on reusability throughout GitLab - DRY Mindset
- Optimize GitLab CI/CD configuration files - Optimize and DRY Mindset
- CI/CD components - GitlabCI Components
- Downstream pipelines - Use Downstream pipelines for trigger
- Gitlab CI/CD Pass artifacts/variables between pipelines - Methodology for resue artifacts
- GitLab CI/CD artifacts reports types - Report type of Gitlab Artifact
- GitLab CI/CD Environments and deployments - Environments describe where code is deployed.
- GitLab container registry - You can use the integrated container registry to store container images for each GitLab project.
- How to modify a variable, used as name for docker image - Dynamic image to using for gitlabci
Use cases and scenarios
Case 1: Completely pipeline for Container Services
Info
React build app, test and build Image for container services
Info
This pipeline definition will cover for me and you with mostly cases when you work with GitlabCI
- Run Script
- Perform test
- Build code and upload artifact
- Build docker image and push to currently registry
Case 2: Build & push docker image to private registry
Info
Use private ECR of AWS with dind (Local Include)
Before run and execute the script, you need to expose some variables into CI/CD variables because of AWS authentication, and one more to ensure anything secrets will not expose
- AWS_ACCESS_KEY_ID
- AWS_SECRET_ACCESS_KEY
- AWS_ACCOUNT
Remember when you want to your variables have effected with your .gitlab-ci.yml
pipelines, you need to concern about use variables in mode protected
or not because if your branch is not protected
, you variables will not have export anything, that sick π
And then you can execute pipelines like usual, but for become special, I will use add on include
keyword to import somewhere template gitlab
, and try to use this one via !reference
You need to create template in your repo, for example /templates/ci/ecr-auth.gitlab-ci.yml
Info
With template will help us to authentication AWS Account, we will use that via
!refernce
tags inside your.gitlab-ci.yml
and attach with template define with name and which one to use inside template. For example, I have.ecr_auth
template withbefore_script
, base on those one you can use with!reference [.ecr_auth, before_script]
And to use !reference
in yaml file of editor, you need to configure inside settings of them for bypass the error
Info
YAML has a feature called βanchorsβ that you can use to duplicate content across your document. For example, read here: YAML anchors for scripts
Warning
But You canβt use YAML anchors across multiple files when using theΒ
include
Β keyword. Anchors are only valid in the file they were defined in. To reuse configuration from different YAML files, useΒ!reference
Β tagsΒ or theΒextends
Β keyword.
Case 3: Authentication Job with private ECR from remote repository
Info
Purpose: Create a general template, that help job can authenticate to private ECR from remote repo, following that job will pull and use private docker image for jobs instead of use the public version
Following this idea, Gitlab
offers for us one methodology to help us authentication private CR via CI/CD variables by DOCKER_AUTH_CONFIG
. Read more at documentation
Note
Remember, DOCKER_AUTH_CONFIG will only help you authenticate your service for pull image from docker registry, If you want to make interaction with private registry, please use
docker login
orcrane auth login
-
Create a CI/CD variable
DOCKER_AUTH_CONFIG
with the content of the Docker https://docs.gitlab.com/ee/ci/yaml/script.html#multiline-commands-not-preserved-by-folded-yaml-multiline-block-scalarconfiguration file as the value- For protected, general repository will use and wrap authentication, such as
AWS_ACCESS_KEY_ID
andAWS_SECRET_ACCESS_KEY
, we only authenticate privateECR
via only general repository. - Therefore, we will use
downstream
techniques ofGitlab
, with one help us trigger general pipeline from remote repository, and in the last that will submit authentication successfully as~/.docker/config.json
to remote repository
- For protected, general repository will use and wrap authentication, such as
-
You can now use any private image fromΒ private ECRΒ defined inΒ
image
Β orΒservices
Β in yourΒ.gitlab-ci.yml
Β file
Create Upstream Pipeline
Upstream pipeline where we configure to generate token of ECR or private dockerhub, you can do same with each others
- Follow the technique in the second part, you can reuse template for login
ECR
, and try to import that withinclude
and!reference
keyword - After run,
before script
, It mean yourdocker+machine
runner work and authenticate toprivate
ECR, now we addjq
for help us manipulate and analysisjson
file, which one format of/root/.docker/config.json
and help you retrieve the token when you usedocker login
command (NOTE: Base64 is actually work and you not need to do anything). - After analysis and retrieve the token, Gilab offer us the methodology to export that result into
.env
because file is tough for process,therefore with simple string we have easily ways to apply that in other repository. Read more at GitLab CI/CD artifacts reports types
Warning
Remember expose
rules
for any situation becauseneed
ortrigger
will base on that for condition which execute command in remote repository
- Lastly, we expose that via artifacts, and you will create downstream pipeline which one retrieve that value
But wait, you need do one more thing. Because of expiration of AWS ECR, you need have strategy to renew that. You donβt have much time to prepare what time we need to trigger pipeline again. That why we need schedule
to handle, and Gitlab provide it for us inside crontab
and very easily. Read more at Scheduled pipelines
For example, I create pipeline schedule
with strategy
- Rerun after every 55 minutes, practice with cron guru
- Select time zone, for example Singapore but it not important in situation
- Select branch, I choose
main
branch - You can add more custom
variable
when run pipeline - Click create and enjoy
Create Downstream pipeline
With downstream pipeline you have two option
- Use
trigger
to call remote repository, but that tough and to control - Use
need
to receive artifact from remote repository
Follow we will use need
to define downstream pipeline, for easily expose your token. You will need to practice with curl
command with GitlabAPI Project-level CI/CD variables API
-
With this
CI
, we will do twice stuff, authentication and check pull private image successfully or not -
Use
need
to retrieve the artifact indotenv
in Gitlab, because gitlab provide us many type of artifact anddotenv
is one of them. Read more at GitLab CI/CD artifacts reports types -
Use script to install some we need
curl
becausealpine:3
is lighweigh image and do not include anything inside. After that, we triggercurl
command to execute API of Gitlab to set stringDOCKER_AUTH_CONFIG
inside machine with created via artifact toCI/CD
variables -
Now your
recheck
job will pass with in-useprivate
image, how cool is this πππ
For optimize pipeline, and easily reuse by other repository
With idea cut off the effort for user who want to implement pipeline, you just change job from execute to hidden with .
form with become template for reuse by remote repositories, like these
And from remote, you can use both include
and extends
keywords for retrieve this template, super easy π