GitLab CI - CD
GitLab CI - CD
GitLab CI - CD
● What is CI/CD?
● GitLab CI/CD Overview
● GitLab CI/CD Setup
● GitLab CI/CD Runners
● Q&A
What is GitLab?
CI/CD encourages collaboration across all departments and makes code creation and management
easy, as well as provides the following specific benefits.
04 03
CD Ensures Every Change is Releasable CI Allows Teams to Develop Faster
This lowers the risk of each release- allows More confidence among the developers allows for
releases to be “boring” less bottlenecking
GitLab Recommended Process
Manage Plan Create Verify Package Secure Release Configure Monitor Protect
Epics
Review App
Milestones
Push Fixes
3 4
1 2
GitLab Pipeline Graph
image:
registry.gitlab.com/gitlab-examples/kubernete
s-deploy
stages:
- build
- deploy
variables:
KUBE_DOMAIN: example.com
build:
stage: build
script:
- command build
only:
- main
deploy:
stage: deploy
script:
- command deploy
environment:
name: production
url: http://production.example.com
variables:
DISABLE_POSTGRES: "yes"
only:
- main
GitLab CI/CD pipeline configuration reference
Stages seperate jobs into logical sections while Jobs perform the
actual tasks
Jobs and Scripts
build-code:
Each Stage Can Have stage: build
Multiple Jobs script: build-it.sh
build-other-code:
Jobs Run In Parallel stage: build
script: src/other/code/build-it.sh
Scripts Can Be
Defined Several script: command build
Different Ways
script:
Script Examples - npm install
- npm build
script:
- scripts/build_script.sh
Basic Parameters
test:
script:
- apt-get update -qy
- bundle install --path /cache
- bundle exec rake test
staging:
stage: deploy
script:
- gem install dpl
- dpl --provider=heroku --app=ruby-test-staging --api-key=$HEROKU_KEY
only:
- main
production:
stage: deploy
script:
- gem install dpl
- dpl --provider=heroku --app=ruby-prod --api-key=$HEROKU_PROD_KEY
only:
- tags
Image
.gitlab-ci.yml
build so far image: registry.example.com/k8-deploy:latest
Services & Variables
variables:
services:
- POSTGRES_DB:
- postgres
i ab l es rails-sample-1_test
ce s Var
Servi - POSTGRES_USER: root
- POSTGRES_PASSWORD: ”xyzzy”
image: registry.example.com/k8-deploy:latest
services:
.gitlab-ci.yml - postgres
build so far variables:
- POSTGRES_DB: rails-sample-1_test
What Our .gitlab-ci.yml looks like so far...
image: registry.example.com/k8-deploy:latest
services:
- postgres
variables:
- POSTGRES_DB: rails-sample-1_test
stages:
- build
- test
- deploy
deploy-code:
stage: deploy
script:
- command deploy
Environments
The environment keyword defines where the app is deployed and is defined by 3
parts.
environment:
name: prod
url: http://$CI_PROJECT_NAME.$KUBE_DOMAIN
when: manual
URL
Only & Except- Restricting When a Job is Executed
pseudo-deploy:
stage: deploy
script:
- command deploy_review
only:
- branches
except: Only Except
- main
environment: The name of branch Branches NOT to
name: review to execute on (in execute on with
url: http://$CI_PROJECT_NAME-review.$KUBE_DOMAIN this case all exception to the
branches) main Branch
The rules syntax is an improved, more powerful solution for defining when jobs should
run or not. Consider using rules instead of only/except to get the most out of your pipelines.
Rules - Restricting When a Job is Executed
pseudo-deploy:
stage: deploy
script:
- command deploy_review
rules:
- if: '$CI_COMMIT_REF_NAME == "main"'
when: never
- when: always
environment:
name: review
url: http://$CI_PROJECT_NAME-review.$KUBE_DOMAIN
before_script & after_script
after_script
is used to define the command that will be run
after each job, including failed ones.
Cache & Artifacts
Cache is used to pass information between jobs & stages by storing project
dependencies
cache:
paths:
- binary/
- .config
artifacts:
when: on_success
paths:
- bin/target
What Our .gitlab-ci.yml looks like so far...
image: registry.example.com/k8-deploy:latest
services:
- postgres
variables:
build-it:
- POSTGRES_DB: rails-sample-1_test
cache: stage: build
paths: script:
- binary/ - command build
stages: only:
- build - main
- test
- deploy artifacts:
deploy-code: when: on_success
stage: deploy paths:
script: - bin/target
- command deploy
environment:
name: production
url: http://$CI_PROJECT_NAME.$KUBE_DOMAIN
when: manual
only:
- main
Tags
○ If a Runner has more tags than required, it can still run that particular
job; including if the job requires no tags at all
job-name:
tags:
- ruby
- test
What Our .gitlab-ci.yml looks like so far...
image: registry.example.com/k8-deploy:latest
services:
- postgres
variables:
- POSTGRES_DB: rails-sample-1_test build-it:
cache: stage: build
paths: script:
- binary/ - command build
stages: only:
- build - main
- test tags:
- deploy - osx
deploy-code: - ios
stage: deploy artifacts:
script: when: on_success
- command deploy paths:
environment: - bin/target
name: production
url: http://$CI_PROJECT_NAME.$KUBE_DOMAIN
when: manual
only:
- main
GitLab CI/CD Runners
Configuration File + Runner
● .gitlab-ci.yml file
○ Instructions for GitLab CI/CD jobs.
○ Lives in the root of the repository
● GitLab Runner
○ Lightweight agent that runs CI/CD jobs. I need to run
the job Alright let’s
“build” before build this
“test” code then I
can test it!
● The GitLab runner can be installed on any platform where you build Go
binaries.
○ Linux, macOS, Windows, FreeBSD, Cloud Provider, Bare Metal, Your work station and
Docker
● Created by an Administrator
A Runner Can Be….
Description Description
VS
Tagged Untagged
Only used to run jobs tagged with Used to run jobs with no tags
same tag
Protected vs. Non-Protected
Protected Non-Protected
Characteristics Characteristics
Shell
Directly run commands as if writing them into terminal
(bash or sh) or command prompt (cmd) or powershell ��
��
Docker Machine
Docker
“Main” machine scales up runners with
*any* executor on demand Execute inside of a docker image
Typical in cloud deployments
�� Most common!
��
Kubernetes
Runs as a pod in a K8s cluster
Can also feature auto-scaling
Executors: Less Common
1. VirtualBox
Base VM for runner
“Main” creates a new VM for each needed runner
2. Parallels
Hint: Parallels is a nice platform on top of
VirtualBox
3. SSH
Similar to shell, but not as many features (bash only, no caching)
Does allow you to SSH and execute commands on a machine you
might not want to install runner on
Q&A
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: