Ebook_1_-_GitLab_Continuous_Integration
Ebook_1_-_GitLab_Continuous_Integration
id
GitLab Continuous
Integration (CI) Tutorial
Maximizing Software Development Efficiency with Essential
GitLab CI Mastery for DevOps Engineers
Enterprise Cloud System Integrator & Tech Professional Solution May 2024
GitLab Continuous Integration (CI) Tutorial btech.id
Table of Contents
About Boer Technology 2
© 2024 Boer Technology. All rights reserved. All trademarks are the property of their respective owners.
GitLab Continuous Integration (CI) Tutorial btech.id
Our Services
Consulting
Our Certified Experts provide Excellent Professional Service in the form of
discussion to produce a documented plan to give the best solution for your
needs.
Implementation
Btech provides a development service for independent infrastructure or platforms
tailored to clients' needs. The implementation can be paired with training
activities and optional maintenance services.
Maintenance Support
Maintenance support provides maintenance for prevention and ticket-based
repair with a flexible Service Level Agreement. This service can be bundled with
the implementation activities if required by the clients.
Managed Service
Availability of new reliable resources takes time. Btech will help to assist the
overall technical operations in Managed Service. Btech also supports unlimited
preventive and corrective tickets for this service.
© 2024 Boer Technology. All rights reserved. All trademarks are the property of their respective owners. 2
GitLab Continuous Integration (CI) Tutorial btech.id
2. Register runner
1|gitlab-runner register
4. Result
References
●
Install GitLab Runner
© 2024 Boer Technology. All rights reserved. All trademarks are the property of their respective owners. 3
GitLab Continuous Integration (CI) Tutorial btech.id
© 2024 Boer Technology. All rights reserved. All trademarks are the property of their respective owners. 4
GitLab Continuous Integration (CI) Tutorial btech.id
References
●
Integrating SonarQube into GitLab CI
© 2024 Boer Technology. All rights reserved. All trademarks are the property of their respective owners. 5
GitLab Continuous Integration (CI) Tutorial btech.id
© 2024 Boer Technology. All rights reserved. All trademarks are the property of their respective owners. 6
GitLab Continuous Integration (CI) Tutorial btech.id
© 2024 Boer Technology. All rights reserved. All trademarks are the property of their respective owners. 7
GitLab Continuous Integration (CI) Tutorial btech.id
© 2024 Boer Technology. All rights reserved. All trademarks are the property of their respective owners. 8
GitLab Continuous Integration (CI) Tutorial btech.id
All Script
1|stages:
2| - sonarqube
3| - build
4| - push
5| - trivy
6| - argocd
7|
8|variables:
9| IMAGE: "<<image-name>>"
10| APP_NAME: "calculator"
11| REPO_BRANCH: "main"
12| REPO_URL: "<<repo-url>>"
13|
14|# Sonarqube
15|Sonarqube Check:
16| stage: sonarqube
17| image:
18| name: sonarsource/sonar-scanner-cli:latest
19| entrypoint: [""]
20| variables:
21| # Defines the location of the analysis task cache
22| SONAR_USER_HOME: "${CI_PROJECT_DIR}/.sonar"
23| # Shallow cloning needs to be disabled.
24| # See https://docs.sonarqube.org/latest/analysis/gitlab-cicd/.
25| GIT_DEPTH: 0
26| cache:
27| key: "${CI_JOB_NAME}"
28| paths:
29| - .sonar/cache
30| script:
31| - sonar-scanner -X
32| rules:
33| # SonarQube CommunityEdition only supports analyzing a single branch.
34| # So only run on main.
35| - if: '$CI_COMMIT_BRANCH == "main"'
36| when: on_success
37| - when: never
38|
39|# Build Image
40|Build:
41| stage: build
42| only:
43| - main
44| before_script:
45| - docker login $HARBOR_URL -u $HARBOR_USERNAME -p $HARBOR_PASSWORD
46| script:
47| - docker build -t $IMAGE:$CI_COMMIT_SHORT_SHA .
48| - docker push $IMAGE:$CI_COMMIT_SHORT_SHA
49|
© 2024 Boer Technology. All rights reserved. All trademarks are the property of their respective owners. 9
GitLab Continuous Integration (CI) Tutorial btech.id
© 2024 Boer Technology. All rights reserved. All trademarks are the property of their respective owners. 10
GitLab Continuous Integration (CI) Tutorial btech.id
Result
© 2024 Boer Technology. All rights reserved. All trademarks are the property of their respective owners. 11
GitLab Continuous Integration (CI) Tutorial btech.id
Provisioning Stack
If you want to provisioning all devsecops stack, you can refer this article.
© 2024 Boer Technology. All rights reserved. All trademarks are the property of their respective owners. 12
GitLab Continuous Integration (CI) Tutorial btech.id
39|# Sonarqube
40|#Sonarqube Check:
41|# stage: sonarqube
42|# image:
43|# name: sonarsource/sonar-scanner-cli:latest
44|# entrypoint: [""]
45|# variables:
46|# SONAR_USER_HOME: "${CI_PROJECT_DIR}/.sonar"
47|# GIT_DEPTH: 0
48|# cache:
49|# key: "${CI_JOB_NAME}"
50|# paths:
51|# - .sonar/cache
52|# script:
53|# - sonar-scanner -X
54|# rules:
55|# - if: '$CI_COMMIT_BRANCH == "staging"'
56|# when: on_success
57|# - when: never
58|
59|# Build Image
60|Build Staging:
61| stage: build
62| only:
63| - staging
64| script:
65| - echo frontend Staging version = $STAGING
66| - docker build -t $IMAGE:$STAGING .
67| - docker push $IMAGE:$STAGING
68|
69|# Tag the "release" branch as "latest"
70|Build Release:
71| stage: push
72| only:
73| - release
74| script:
75| - docker pull $IMAGE:$STAGING
76| - docker tag $IMAGE:$STAGING $IMAGE:$RELEASE
77| - docker tag $IMAGE:$STAGING $IMAGE:latest
78| - docker push $IMAGE:$RELEASE
79| - docker push $IMAGE:latest
80|
81|# Trivy Scan
82|#Trivy Scan:
83|# stage: trivy
84|# script:
85|# - trivy --version
86|# - TRIVY_INSECURE=true trivy image --exit-code 0 --no-progress -f json -o trivy-scan-report.json $IMAGE
87|# # Prints full report
88|# - TRIVY_INSECURE=true trivy image --exit-code 0 --no-progress $IMAGE
89|
90|# Push Chart
91|Push Chart Staging:
92| stage: push-chart
93| only:
94| - staging
95| script:
96| - sed -i "s+{{CHART_VERSION}}+$HELM_CHART_STAGING_VERSION+g" ./frontend/Chart.yaml
97| - sed -i "s+{{IMAGE_VERSION}}+$HELM_STAGING_VERSION+g" ./frontend/Chart.yaml
98| - helm repo add chartmuseum $HELM_REPO --force-update
99| - helm cm-push frontend chartmuseum --force
100| - helm repo update
101|
© 2024 Boer Technology. All rights reserved. All trademarks are the property of their respective owners. 13
GitLab Continuous Integration (CI) Tutorial btech.id
102|
Push Chart:
103| stage: push-chart
104| only:
105| - release
106| script:
107| - sed -i "s+{{CHART_VERSION}}+$HELM_CHART_RELEASE_VERSION+g" ./frontend/Chart.yaml
108| - sed -i "s+{{IMAGE_VERSION}}+$HELM_APP_VERSION+g" ./frontend/Chart.yaml
109| - helm repo add chartmuseum $HELM_REPO --force-update
110| - helm cm-push $APP_NAME chartmuseum --force
111| - helm repo update
112|
113|
# Approval Staging
114|
Approval Staging:
115| stage: approval
116| only:
117| - staging
118| environment:
119| name: approval-staging
120| url: $REPO_URL
121| script:
122| - curl -s -X POST https://api.telegram.org/bot$TELE_TOKEN/sendMessage -d chat_id=$TELE_CHAT_ID -d
parse_mode=markdown -d text="Dear Team *CusEx*, Please Approve Deployment of * $APP_NAME * on Branch *Staging*. Please
goes to $REPO_URL_2/-/environments to *Accept.*"
123|
124|
# Approval Release
125|
Approval Release:
126| stage: approval
127| only:
128| - release
129| environment:
130| name: approval-release
131| url: $REPO_URL
132| script:
133| - curl -s -X POST https://api.telegram.org/bot$TELE_TOKEN/sendMessage -d chat_id=$TELE_CHAT_ID -d
parse_mode=markdown -d text="Dear Team *CusEx*, Please Approve Deployment of * $APP_NAME * on Branch *Release*. Please
goes to $REPO_URL_2/-/environments to *Accept.*"
134|
135|
# ArgoCD Deploy
136|
Deploy Staging to ArgoCD:
137| stage: argocd
138| rules:
139| - if: '$CI_COMMIT_BRANCH == "staging"'
140| when: manual
141| environment:
142| name: staging
143| url: https://cx-boutique-stg.btech.id
144| script:
145| - argocd login --insecure $ARGOCD_SERVER --username $ARGOCD_CREDS_USR --password $ARGOCD_CREDS_PSW
146| - argocd app create $ARGOCD_APP_STAGING --project $ARGOCD_STAGING_PROJECT --repo $HELM_REPO --helm-chart $APP_NAME
--revision $HELM_CHART_STAGING_VERSION --dest-namespace boutique-staging --dest-server https://kubernetes.default.svc --
annotations notifications.argoproj.io/subscribe.on-sync-succeeded.teams=TeamsBTech --upsert
147| - ARGOCD_SERVER=$ARGOCD_SERVER argocd --grpc-web app sync $ARGOCD_APP_STAGING --force
148| - ARGOCD_SERVER=$ARGOCD_SERVER argocd --grpc-web app wait $ARGOCD_APP_STAGING --timeout 600
149|
150|
# ArgoCD Deploy
151|
Deploy Release to ArgoCD:
152| stage: argocd
153| rules:
154| - if: '$CI_COMMIT_BRANCH == "release"'
155| when: manual
156| environment:
157| name: release
158| url: https://cx-boutique.btech.id
159| script:
160| #- argocd login --insecure $ARGOCD_SERVER --username $ARGOCD_CREDS_USR --password $ARGOCD_CREDS_PSW
161| - argocd app create $ARGOCD_APP_RELEASE --project $ARGOCD_RELEASE_PROJECT --repo $HELM_REPO --helm-chart $APP_NAME
--revision $HELM_CHART_RELEASE_VERSION --dest-namespace boutique --dest-server https://kubernetes.default.svc --
annotations notifications.argoproj.io/subscribe.on-sync-succeeded.teams=TeamsBTech --upsert
162| - ARGOCD_SERVER=$ARGOCD_SERVER argocd --grpc-web app sync $ARGOCD_APP_RELEASE --force
163| - ARGOCD_SERVER=$ARGOCD_SERVER argocd --grpc-web app wait $ARGOCD_APP_RELEASE --timeout 600
© 2024 Boer Technology. All rights reserved. All trademarks are the property of their respective owners. 14
GitLab Continuous Integration (CI) Tutorial btech.id
Project
Gitlab-CI-Pipelines-Exporter
Execution
1|git clone https://github.com/mvisonneau/gitlab-ci-pipelines-exporter.git
2|cd gitlab-ci-pipelines-exporter/examples/quickstart
© 2024 Boer Technology. All rights reserved. All trademarks are the property of their respective owners. 15
GitLab Continuous Integration (CI) Tutorial btech.id
Deploy
1|docker-compose up -d
gitlab-ci-pipelines-exporter : http://localhost:8080/metrics
prometheus : http://localhost:9090
© 2024 Boer Technology. All rights reserved. All trademarks are the property of their respective owners. 16
GitLab Continuous Integration (CI) Tutorial btech.id
http://192.168.2.26:9090/new/graph
You can then validate that you get the expected values for your projects metrics, eg
gitlab_ci_pipeline_status:
http://192.168.2.26:9090/new/graph? g0.expr=gitlab_ci_pipeline_status&g0.tab=1&g0.stacked=0&g0.range_input=1h
© 2024 Boer Technology. All rights reserved. All trademarks are the property of their respective owners. 17
GitLab Continuous Integration (CI) Tutorial btech.id
●
Pipelines dashboard
http://192.168.2.26:3000/d/gitlab_ci_pipelines
© 2024 Boer Technology. All rights reserved. All trademarks are the property of their respective owners. 18
GitLab Continuous Integration (CI) Tutorial btech.id
●
Jobs dashboard
http://192.168.2.26:3000/d/gitlab_ci_jobs
●
Environments / deployments dashboard
http://192.168.2.26:3000/d/gitlab_ci_environment_deployments
References
●
Gitlab CI Pipeline Exporter
© 2024 Boer Technology. All rights reserved. All trademarks are the property of their respective owners. 19
GitLab Continuous Integration (CI) Tutorial btech.id
Example
© 2024 Boer Technology. All rights reserved. All trademarks are the property of their respective owners. 20
GitLab Continuous Integration (CI) Tutorial btech.id
Flow (+-)
© 2024 Boer Technology. All rights reserved. All trademarks are the property of their respective owners. 21
GitLab Continuous Integration (CI) Tutorial btech.id
Requirements
A. Repo : https://your-gitlab.com/testing.git
B. GitLab Token and set to CI/CD Variables (Protected) grant the api and the write_repository scopes to the token.
© 2024 Boer Technology. All rights reserved. All trademarks are the property of their respective owners. 22
GitLab Continuous Integration (CI) Tutorial btech.id
Commitizen is a tool designed for teams. Its main purpose is to define a standard way of
committing rules and communicating it (using the cli provided by commitizen). The reasoning
behind it is that it is easier to read, and enforces writing descriptive commits. Besides that,
having a convention on your commits makes it possible to parse them and use them for
something else, like generating automatically the version or a changelog.
1. when a new commit happens (either via git push or a merge) GitLab starts a new
pipeline.
2. when the pipeline runs the job that starts semantic-release, the process goes
through each release steps for each plugin, in the order they are configured.
3. the latest release is determined by inspecting the tags and messages of commits
4. the analyzeCommits (the only required step) determines if a new release has to be
created and, if so, which type.
5. each plugin performs its core tasks, like generating release notes, changelogs
or creating tags. Change logs are interpreted by default based on the Angular
commit message conventions.
6. the release is then published and notified by plugins.
© 2024 Boer Technology. All rights reserved. All trademarks are the property of their respective owners. 23
GitLab Continuous Integration (CI) Tutorial btech.id
Create .releaserc.yml
.releaserc.yml
1|plugins:
2|- "@semantic-release/commit-analyzer"
3|- "@semantic-release/release-notes-generator"
4|- - "@semantic-release/exec"
5| - verifyReleaseCmd: "echo ${nextRelease.version} > VERSION.txt"
6|- - "@semantic-release/changelog"
7| - changelogFile: CHANGELOG.md
8|- - "@semantic-release/gitlab"
9| - gitlabUrl: https://your-gitlab.com/
10|- - "@semantic-release/git"
11| - assets:
12| - CHANGELOG.md
13| - VERSION.txt
14| message: |-
15| chore(release): ${nextRelease.version} [skip ci]
16|
17| ${nextRelease.notes}
18|
19|branches:
20|- "main"
21|- "+([0-9])?(.{+([0-9]),x}).x"
22|- name: "rc"
23| prerelease: "rc"
© 2024 Boer Technology. All rights reserved. All trademarks are the property of their respective owners. 24
GitLab Continuous Integration (CI) Tutorial btech.id
.gitlab-ci.yml
1|stages:
2| - fetch-version
3| - build
4| - push
5| - release
6|
7|variables:
8| IMAGE: "your-private.registry.com/calculator/calculator"
9|
10|
before_script:
11| - docker login $HARBOR_URL -u $HARBOR_USERNAME -p $HARBOR_PASSWORD
12|
13|
fetch-semantic-version:
14| # Requires Node >= 10.13 version
15| image: node:13
16| stage: fetch-version
17| only:
18| refs:
19| - main
20| - rc
21| - /^(([0-9]+)\.)?([0-9]+)\.x/ # This matches maintenance branches
22| - /^([0-9]+)\.([0-9]+)\.([0-9]+)(?:-([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?(?:\+[0-9A-Za-z-]+)?$/ # This matches pre-releases
23| script:
24| - npm install @semantic-release/gitlab @semantic-release/exec @semantic-release/changelog @semantic-release/commit-analyzer
@semantic-release/release-notes-generator @semantic-release/npm @semantic-release/git
25| - npx semantic-release --generate-notes false --dry-run
26| artifacts:
27| paths:
28| - VERSION.txt
29|
30|
Build:
31| stage: build
32| only:
33| - rc
34| before_script:
35| - export TAGS=$(cat VERSION.txt)
36| script:
37| - docker build -t $IMAGE:$TAGS .
38| - docker push $IMAGE:$TAGS
39|
40|
## Tag the "main" branch as "latest"
41|
Push latest:
42| stage: push
43| only:
44| - main
45| before_script:
46| - export TAGS=$(cat VERSION.txt)
47| script:
48| - docker build -t $IMAGE:$TAGS .
49| - docker tag $IMAGE:$TAGS $IMAGE:latest
50| - docker push $IMAGE:$TAGS
51| - docker push $IMAGE:latest
52|
53|
release:
54| image: node:13
55| stage: release
56| only:
57| refs:
58| - main
59| - rc
60| # This matches maintenance branches
61| - /^(([0-9]+)\.)?([0-9]+)\.x/
62| # This matches pre-releases
63| - /^([0-9]+)\.([0-9]+)\.([0-9]+)(?:-([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?(?:\+[0-9A-Za-z-]+)?$/
64| script:
65| - npm install @semantic-release/gitlab @semantic-release/exec @semantic-release/changelog @semantic-release/commit-analyzer
@semantic-release/release-notes-generator @semantic-release/npm @semantic-release/git
66| - npx semantic-release
67| artifacts:
68| paths:
69| - CHANGELOG.md
© 2024 Boer Technology. All rights reserved. All trademarks are the property of their respective owners. 25
GitLab Continuous Integration (CI) Tutorial btech.id
Review
It looks like a lot of stuff. What we do here:
1. define a new stage in the pipeline named fetch-version. This stage runs at the
beginning of the pipeline to fetch or generate the version number and make it available
2. define a new job fetch-semantic-version that runs at the fetch-version stage only
on the branches managed by semantic-release. This time we use the options --generate-
notes false --dry-run to avoid applying any change. However this still generates the
VERSION.txt file because of the semantic-release configuration we have in place
3. produce the VERSION.txt file in both jobs (of course you can use any other name) and
publish it as an artifact because it needs to be available across different build jobs
4. define another job build that build a docker image using the value from VERSION.txt
regardless of how and when it was generated
5. define a new job 'release' that runs semantic-release and publish ChangeLog to our repo
If a commit doesn’t match any rule in releaseRules it will be evaluated against the default
release rules.
© 2024 Boer Technology. All rights reserved. All trademarks are the property of their respective owners. 26
GitLab Continuous Integration (CI) Tutorial btech.id
No rules matching
If a commit doesn`t match any rules in releaseRules or in default release rules then no
release type will be associated with the commit.
Edit app/templates/calculator.html
1|...
2| <h3 class="text-muted">Simple Scientific Calculator (v1.2.1)</h3>
3|...
Note: we use fix to add patch version. if Breaking Change set to Yes, it will Correlates
with MAJOR in SemVer
1|git push origin rc
© 2024 Boer Technology. All rights reserved. All trademarks are the property of their respective owners. 27
GitLab Continuous Integration (CI) Tutorial btech.id
Results
© 2024 Boer Technology. All rights reserved. All trademarks are the property of their respective owners. 28
GitLab Continuous Integration (CI) Tutorial btech.id
Merge to main
Pull on rc branch first
1|git pull
© 2024 Boer Technology. All rights reserved. All trademarks are the property of their respective owners. 29
GitLab Continuous Integration (CI) Tutorial btech.id
© 2024 Boer Technology. All rights reserved. All trademarks are the property of their respective owners. 30
GitLab Continuous Integration (CI) Tutorial btech.id
Results
●
Rc Version Url : http://192.168.3.141:5000/
●
Main Version Url : http://192.168.3.140:5000/
CHANGELOG.md
References
●
https://semver.org/
●
https://github.com/semantic-release/semantic-release
●
https://www.youtube.com/watch?v=QZdY4XYbqLI&list=LL&index=14
●
https://levelup.gitconnected.com/semantic-versioning-and-release-automation-on-gitlab-9ba16af0c21
●
https://medium.com/nerd-for-tech/auto-bump-apps-versions-and-releases-using-gitlab-pipeline-e32f1d7fa3ee
●
https://repository.prace-ri.eu/git/help/ci/examples/semantic-release.md
●
https://faun.pub/git-flow-and-semantic-release-with-gitlab-be54b2c64818
●
https://stackoverflow.com/questions/70790587/semantic-release-not-accepting-gitlab-token-on-gitlab-private-
repository
●
https://itnext.io/automate-your-releases-versioning-and-release-notes-with-semantic-release-d5575b73d986
© 2024 Boer Technology. All rights reserved. All trademarks are the property of their respective owners. 31
btech.id
GitLab Continuous
Integration (CI) Tutorial
Maximizing Software Development Efficiency with
Essential GitLab CI Mastery for DevOps Engineers
btech.id
Contact Us
Boer Technology
Komplek Ruko Pandu Raya No. 14, Jl. Achmad
Adnawijaya, Bogor, West Java, Indonesia 16152