GitLab CI/CD

Docker gitlab – runner installation

$docker run -d --name gitlab-runner --restart always \ -v /srv/gitlab-runner/config:/etc/gitlab-runner \ -v /var/run/docker.sock:/var/run/docker.sock \ gitlab/gitlab-runner:latest
Copy the code

You can expose a port when running Runner to debug directly on the GitLab pipeline:

$docker run -d --name gitlab-runner -p 8081:8081 --restart always \ -v /srv/gitlab-runner/config:/etc/gitlab-runner \ -v /var/run/docker.sock:/var/run/docker.sock \ gitlab/gitlab-runner:latest
Copy the code

After the operation need to modify the configuration files of the runner/SRV/gitlab – runner/config/config. Toml:

[session_server]
	listen_address = "[...] : 8081"
	advertise_address = "IP:8081"
	session_timeout = 1800
[runners.docker]
	volumes = ["/cache"."/usr/bin/docker:/usr/bin/docker"."/var/run/docker.sock:/var/run/docker.sock"]
	pull_policy = "if-not-present"
Copy the code

Enter the gitlab – runner

$ docker exec -it gitlab-runner bash
Copy the code

Check out the status of runners

$ gitlab-runner status
Copy the code

Restart the runners

$ gitlab-runner run
Copy the code

Registered gitlab – runner

$ docker exec -it gitlab-runner gitlab-runner register
Copy the code

After the command is executed, you need to enter prompts one by one

The URL and token can be found in gitLab project Settings

.gitlab-ci.yml

  • stages

    Global customization phase

    stages:
    	- stage1
    	- stage2
    	- stage3
    Copy the code
  • script

    A shell script

    job1:
    	stage: stage1
    	script: "echo 'this is job1'"
    	
    job2:
    	stage: stage2
    	script:
    		- echo "this is job2"
    		- echo "this is job2 too"
    Copy the code
  • stage

    The phase within the task must be selected from the global phase

    job1:
    	stage: stage1
    	script: "echo 'this is stage1'"
    Copy the code
  • retry

    Number of retries after a failure (0 does not retry and 2 retries at most)

    When: When to retry

    test:
    	script: rspec
    	retry: 0
    test:
    	script: rspec
    	retry:
    		max: 2
    		when:
    			- runner_system_failure
    			- syuck_or_timeout_failure
    Copy the code
  • image

    Specify a base Docker image as the base runtime environment, often used images have Node Java Python Docker

  • tags

    Tags keyword is used to specify Runner, the value range of tags is visible in the project Runner tags

  • only/except

    Limits the conditions under which the current task is executed

  • when

    The when keyword is the implementation of a job that can run in or despite a failure

  • cache

    It is to store some files and folders in the current working environment directory for recovery during the initialization of each task

  • variables

    There are three ways to set variables:

    • Define yourself in.gitlab-ci.yml
    • Use predefined variables in pipeline
    • Set Sets variables in CICD
  • interruptible

    < span style = “box-sizing: border-box! Important; word-break: inherit! Important; word-break: inherit! Important;

  • timeout

    Setting timeout

  • rousource_group

    The number of deployment tasks for a branch is limited to one, and other new pipelines enter the waiting state

An example of front-end automated deployment:

image: node:alpine

stages:
	- install
	- build
	- deploy

cache:
	key: modules-cache
	paths:
		- node_modules

job_install:
	stage: install
	tags: 
	 - dockercicd
	script:
		- npm install
	interruptible: true
	rouscource: prod
		
job_build:
	stage: build
	tags: 
	 - dockercicd
	script:
		- npm run build
	only:
		- release Execute only on the Release branch
	interruptible: true
	rouscource: prod
		
job_deplot:
	stage: deploy
	variables:
		WEB_NAME:"web-image"
	image: docker
	tags: 
	 - dockercicd
	script:
		- docker build -t $WEB_NAME . Create a Dockerfile file in the current project directory
		- if [ $(docker ps -aq --filter name=$WEB_NAME) ]; then docker rm -f $WEB_NAME; fi
		- docker run -d -p 8082: 80 $WEB_NAME
	when: manual Execute the task manually
	interruptible: true
	rouscource: prod
Copy the code
FROM node:lastest as builder
WORKDIR /app
COPY package.json .
RUN npm install --registry=http://registry.npm.taobao.org
COPY.
RUN npm run build

FROM nginx:lastest
COPY --from=builder /app/dist /usr/share/nginx/html
Copy the code

CI/CD Settings

Cancel the default email alert

Pipeline has no permission to trigger

Pipeline failed due to the user not being verified

This is a weird problem. It took a long time to find the problem on extranet because shared tags were enabled by default and required authentication.

Here I choose to close directly, which is also the page for obtaining the token.

Assembly line

  • Basic pipeline

  • DAG line

    stages:
    	- build
    	- test
    	- deploy
    
    image: alpine
    
    build_a:
    	stage: build
    	script:
    		- echo "this job builds something quickly"
    
    build_b:
    	stage: build
    	script:
    		- echo "this job builds something slowly"
    		
    test_a:
    	stage: test
    	need: [build_a]
    	script:
    		- echo "this test job will start as soon as build_a finished"
    		- echo "it will not wait for build_b,or other jobs in the build stage,to finished"
    		
    test_b:
    	stage: test
    	need: [build_b]
    	script:
    		- echo "this test job will start as soon as build_b finished"
    		- echo "it will not wait for build_b,or other jobs in the build stage,to finished"
    		
    deploy_a:
    	stage: deploy
    	need: [test_a]
    	script:
    		- echo "Since build_a and test_a run quickly,this deploy job can run much earlier"
    		- echo "it dose not need to wait for build_b or test_b"
    Copy the code
  • Parent and child assembly line

    stages:
    	- test
    	- triggers
    	- deploy
    	
    testjob:
    	stage: test
    	script: echo 'test job'
    	
    trigger_a:
    	stage: triggers
    	trigger:
    		include: a/.gitlab-ci.yml
    	rules:
    		- changes:
    			- a/*
    
    trigger_b:
    	stage: triggers
    	trigger:
    		include: b/.gitlab-ci.yml
    	rules:
    		- changes:
    			- b/*
    Copy the code
  • Multi-project pipeline

    stages:
    	- test
    	- triggers
    	- deploy
    	
    testjob:
    	stage: test
    	script: echo 'test job'
    	
    trigger_a:
    	stage: triggers
    	trigger:
    		include: a/.gitlab-ci.yml
    	rules:
    		- changes:
    			- a/*
    
    trigger_b:
    	stage: triggers
    	trigger:
    		include: b/.gitlab-ci.yml
    	rules:
    		- changes:
    			- b/*
    			
    trigger_c:
    	stage: deploy
    	trigger:
    		project: root/pain_html
    		branch: master
    		stategy: depend
    Copy the code
  • Merge request pipeline

    image: node:alpine
    
    stages:
    	- install
    	- build
    	- deploy
    
    cache:
    	key: modules-cache
    	paths:
    		- node_modules
    
    job_install:
    	stage: install
    	tags: 
    	 - dockercicd
    	script:
    		- npm install
    		
    job_build:
    	stage: build
    	tags: 
    	 - dockercicd
    	script:
    		- npm run build
    	only:
    		- merge_requests Execute only when merging
    Copy the code

Pipeline trigger

  • Push the code
  • Timing trigger
  • The url to trigger
  • Manual trigger

Vue project actual combat. Gitlab-ci.yml

stages: 
  - package
  - build
  - deploy
my_package:
  image: 192.168100.150./tools/node-git:10-alpine
  stage: package
  script:
    - npm install --registry=https://registry.npm.taobao.org
    - npm run build:prod
    - cp Dockerfile dist/Dockerfile
  cache:
    key: ${CI_PIPELINE_ID}
    paths: 
      - dist/
  only:
    - master
  tags:
   - runInDk
my_build:
  stage: build
  cache:
    key: ${CI_PIPELINE_ID}
    paths: 
      - dist/
  script:
    - cd dist
    - docker build -t 192.16888.4.:5000/${CI_PROJECT_NAME}:${CI_PIPELINE_ID} .
    - docker push 192.16888.4.:5000/${CI_PROJECT_NAME}:${CI_PIPELINE_ID}
  tags:
    - runInDk
my_deploy:
  stage: deploy
  script:
    - docker stop ${CI_PROJECT_NAME} && docker rm ${CI_PROJECT_NAME}
    - docker run -d -p 8080: 80 --restart=always --name=${CI_PROJECT_NAME} 192.16888.4.:5000/${CI_PROJECT_NAME}:${CI_PIPELINE_ID}
  tags:
    - runInDk
Copy the code