Slide 1

Slide 1 text

Ondrej Sika [email protected] @ondrejsika Linux Days 2018, Prague, 6. 10. 2018 Gitlab CI + Docker

Slide 2

Slide 2 text

https://sika.link/linuxdays2018

Slide 3

Slide 3 text

Goals - Build application - Run tests - Deploy to staging env.

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

What is CI?

Slide 6

Slide 6 text

What is CI? In software engineering, continuous integration is the practice of merging all developer working copies to a shared mainline several times a day.

Slide 7

Slide 7 text

Usage of CI Automatization of - build process - testing - deployment - dev - staging - production - code quality - Linting - Formating

Slide 8

Slide 8 text

Gitlab CI

Slide 9

Slide 9 text

Gitlab CI Architecture

Slide 10

Slide 10 text

Gitlab CI Runner GitLab Runner is the tool that is used to run your jobs and send the results back to GitLab.

Slide 11

Slide 11 text

Gitlab CI Runner

Slide 12

Slide 12 text

Gitlab CI Runner Run on: - Linux - Docker - Windows How to install & configure: - https://docs.gitlab.com/runner/install/ - https://docs.gitlab.com/runner/register/ - https://github.com/ondrejsika/gitlab-ci-runner

Slide 13

Slide 13 text

Install Gitlab Runner - Linux sudo wget -O /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/b inaries/gitlab-runner-linux-amd64 sudo chmod +x /usr/local/bin/gitlab-runner sudo useradd --comment 'GitLab Runner' --create-home gitlab-runner --shell /bin/bash sudo gitlab-runner install --user=gitlab-runner --working-directory=/home/gitlab-runner sudo gitlab-runner start

Slide 14

Slide 14 text

Register Gitlab Runner - Linux sudo gitlab-runner register # or gitlab-runner register --non-interactive \ --url $GITLABCI_URL \ --registration-token $GITLABCI_TOKEN

Slide 15

Slide 15 text

Install Gitlab Runner - Docker docker run -d \ --name gitlab-runner \ --restart always \ -v /var/run/docker.sock:/var/run/docker.sock \ -v /builds:/builds \ gitlab/gitlab-runner:latest

Slide 16

Slide 16 text

Register Gitlab Runner - Docker docker exec -ti gitlab-runner gitlab-runner register \ --non-interactive \ --url $GITLABCI_URL \ --registration-token $GITLABCI_TOKEN \ --name $(hostname) \ --executor docker \ --docker-image docker:git \ --docker-volumes '/var/run/docker.sock:/var/run/docker.sock' \ --docker-volumes '/builds:/builds'

Slide 17

Slide 17 text

https://github.com/ondrejsika/gitlab-ci-runner

Slide 18

Slide 18 text

Done, check out your Gitlab

Slide 19

Slide 19 text

First Job

Slide 20

Slide 20 text

First job git clone [email protected]:test/test.git cd test vim .gitlab-ci.yml git add . git commit -m "Add CI script" git push origin master # .gitlab-ci.yml job: script: echo Hello World!

Slide 21

Slide 21 text

.gitlab-ci.yml

Slide 22

Slide 22 text

https://docs.gitlab.com/ce/ci/

Slide 23

Slide 23 text

Jobs - script - when - stages - only & except - before_job & after_job - retry

Slide 24

Slide 24 text

Script test1_job: script: echo 'Run test1 ...' test2_job: script: - echo Run 'test2.1 ...' - echo Run 'test2.2 ...' - echo Run 'test2.3 ...'

Slide 25

Slide 25 text

Stages stages: - build - test - deploy build_job: stage: build script: echo 'Building ...' test1_job: stage: test script: echo Run test1 ...' test2_job: stage: test script: echo Run test2 ...'

Slide 26

Slide 26 text

When cleanup_build_job: script: echo Cleanup build when failed ... when: on_failure test_job: script: echo Run test ... deploy_job: script: echo Deploy ... when: manual cleanup_job: script: echo Full cleanup ... when: always

Slide 27

Slide 27 text

Only & Except job: # use regexp only: - /^issue-.*$/ # use special keyword except: - branches

Slide 28

Slide 28 text

Variables - Secret variables are defined in Gitlab - Some variables set CI runtime - Public variables are defined in .gitlab-ci.yml

Slide 29

Slide 29 text

Variables CI CI_PROJECT_NAME, CI_PROJECT_PATH_SLUG CI_COMMIT_REF_NAME, CI_COMMIT_REF_SLUG CI_COMMIT_SHA, CI_COMMIT_TAG CI_PIPELINE_ID, CI_JOB_ID CI_REGISTRY, CI_REGISTRY_USER, CI_REGISTRY_PASSWORD ... https://docs.gitlab.com/ce/ci/variables/README.html

Slide 30

Slide 30 text

Variables variables: IMAGE_TAG: myapp:$CI_PIPELINE_ID job: script: docker build -t $IMAGE_TAG .

Slide 31

Slide 31 text

Docker - Fully supported - Easiest way how to create build environment - Easiest way how to run and distribute your software

Slide 32

Slide 32 text

Docker Environment image: ondrejsika/ci job: image: ondrejsika/ci-go script: go build server.go

Slide 33

Slide 33 text

Docker job: script: - docker build -t $IMAGE . - docker push $IMAGE

Slide 34

Slide 34 text

Environments Environment is used to define that a job deploys to a specific environment. If environment is specified and no environment under that name exists, a new one will be created automatically.

Slide 35

Slide 35 text

Environment deploy: script: echo 'Deploy!' environment: name: $CI_COMMIT_REF_SLUG url: https://$CI_COMMIT_REF_SLUG.example.com

Slide 36

Slide 36 text

Deployments - Automatic - Manual

Slide 37

Slide 37 text

Auto vs Manual Deployments auto_deploy_job: script: echo Auto Deploy! environment: name: deployment-$CI_PIPELINE_ID manual_deploy_job: when: manual script: echo Manual Deploy! environment: name: deployment-$CI_PIPELINE_ID

Slide 38

Slide 38 text

Stop Deployment deploy_job: stage: deploy script: echo Deploy! environment: name: deployment-$CI_PIPELINE_ID on_stop: stop_deploy_job stop_deploy_job: stage: deploy script: echo Stop! when: manual environment: name: deployment-$CI_PIPELINE_ID action: stop

Slide 39

Slide 39 text

Try it!

Slide 40

Slide 40 text

https://github.com/ondrejsika/linuxdays2018

Slide 41

Slide 41 text

https://gitlab-demo.xsika.cz/demo/linuxdays2018

Slide 42

Slide 42 text

Resources - https://about.gitlab.com/features/gitlab-ci-cd/ - https://docs.gitlab.com/ce/ci/ - https://docs.gitlab.com/ce/ci/yaml/ - https://docs.gitlab.com/ce/ci/quick_start/ - https://ondrej-sika.cz/blog/2018/gitlab-ci-docker-linuxdays/ - https://github.com/ondrejsika/ondrejsika-ci-docker - https://github.com/ondrejsika/traefik-ssl - https://github.com/ondrejsika/gitlab-ci-runner

Slide 43

Slide 43 text

Thank you & Questions Ondrej Sika email: [email protected] web: https://ondrej-sika.cz twitter: @ondrejsika linkedin: /in/ondrejsika/ https://sika.link/linuxdays2018