【DevOps】Gitlab CI/CD

本地安装与注册 gitlab-runner


卸载 gitlab-runner

gitlab-runner stop
chkconfig gitlab-runner off
gitlab-runner uninstall

# 清理文件
rm -rf /etc/gitlab-runner
rm -rf /usr/local/bin/gitlab-runner
rm -rf /usr/bin/gitlab-runner
rm -rf /etc/sudoers.d/gitlab-runner

安装 gitlab-runner

https://docs.gitlab.com/runner/install/linux-repository.html

cd /usr/local

# 下载 repository 并执行
curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.rpm.sh | sudo bash

# 查看可安装版本
yum list | grep gitlab-runner

# 执行安装
sudo yum install gitlab-runner

# 查看安装版本
gitlab-runner -v
sudo gitlab-runner install --user=root --working-directory=/home/gitlab-runner

sudo gitlab-runner start

注册 gitlab-runner

sudo gitlab-runner register

# 输入gitlab 地址 和 注册 token,可以在 Menu-> Admin -> Runner 的右侧找到

# 然后输入 description、tag

# 选择一个 executor
# Enter an executor: shell, ssh, docker-ssh+machine, kubernetes, custom, docker, docker-ssh, parallels, virtualbox, docker+machine:

shell

# Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!

打开gitlab 的 Menu => Admin => Runner ,可以看到刚刚配置的 runner

gitlab-runner list
gitlab-runner register 


gitlab-runner verify
gitlab-runner status
gitlab-runner stop 
gitlab-runner start 
gitlab-runner restart 
gitlab-runner uninstall

gitlab-runner unregister

gitlab-runner unregister --name test-runner

#注销所有
gitlab-runner unregister --all-runners

#使用令牌注销
gitlab-runner unregister --url http://gitlab.example.com/ --token t0k3n

Docker 安装与注册 gitlab-runner


参考 https://docs.gitlab.com/runner/install/docker.html

1、安装

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

2、注册

https://blog.csdn.net/little_kelvin/article/details/106297963

有 share runner、specific runner、group runner 三种类型;

share runner:用管理员账户登录,并在 Menu => admin => 概览 => Runner 中 获取到的 token注册;

docker run --rm -it -v /srv/gitlab-runner/config:/etc/gitlab-runner gitlab/gitlab-runner register

# 1、输入gitlab 地址 
# 2、输入 注册 token
# 3、输入 description (gitlab-runner-shared)
# 4、输入 tag (gitlab-runner-shared)
# 5、选择一个 executor (shell)

3、查看

登录 gitlab => Menu => Admin => 概览-Runner

可以看到已经有一个 runner 注册成功,类型是 specific ,状态是 locked;

点进去这个 runner,可以进行一系列配置:

Paused:暂停这个runner的使用

受保护:仅在受保护分支出发(需要在 gitlab.yml 中的每个流程 通过 only 项指定分支)
运行未标记作业:如果不勾选,则只能运行在有标记的作业上,即 需要在 gitlab.yml 中的每个流程 通过 tag 项指定 runner

锁定到当前项目:被锁定到固定的项目,其他项目则无法使用(shared runner 该项不可编辑)

使用CICD 流程

1、进入一个项目 => CI/CD => 编辑器,编辑一个 Pipeline

stages:          # List of stages for jobs, and their order of execution
  - build
  - test
  - deploy

build-job:       # This job runs in the build stage, which runs first.
  stage: build
  script:
    - echo "Compiling the code..."
    - echo "Compile complete."


unit-test-job:   # This job runs in the test stage.
  stage: test    # It only starts when the job in the build stage completes successfully.
  script:
    - echo "Running unit tests... This will take about 10 seconds."
    - sleep 10
    - echo "Code coverage is 90%"


lint-test-job:   # This job also runs in the test stage.
  stage: test    # It can run at the same time as unit-test-job (in parallel).
  script:
    - echo "Linting code... This will take about 10 seconds."
    - sleep 10
    - echo "No lint issues found."


deploy-job:      # This job runs in the deploy stage.
  stage: deploy  # It only runs when *both* jobs in the test stage complete successfully.
  script:
    - echo "Deploying application..."
    - echo "Application successfully deployed."

2、输入提交信息和目标分支后,点击提交更改,之后会自动执行以下步骤:

1、Checking pipeline status
2、Pipeline #17 运行中 for e9858f66

点击 #17 会看到流水线上每个步骤的执行情况和执行结果;
点击 e9858f66 会看到提交结果;

.gitlab-ci.yml 详解


.gitlab-ci.yml 的结构与简单示例

一个 .gitlab-ci.yml 文件就是一个 pipeline,一个 pipeline 包含不同的 stage,每个 stage 包含具体的 job;

看一个最简单的示例:

这个 .gitlab-ci.yml 文件就是一个 pipeline,即一个部署的流水线工程;

开头的stages 定义了 这个 pipeline 所包含的阶段,一般包含 构建、测试、部署 三个阶段;

每个阶段的第一行都是该阶段的任务的名称,build-job test-job 分别是build 和 test 阶段的任务名称;

tag标签声明了要使用的 gitlab-runner,如果 要使用的是 shared 的 gitlab-runner ,则无需指定;

stages:
  - build
  - test
  - deploy

build-job:
  stage: build
  script:
    - echo "Compiling the code..."
    - echo "Compile complete."
  tags:
    - ez_gitlab-runner

test-job:  
  stage: test
  script:
    - echo "Running unit tests... This will take about 10 seconds."
    - sleep 10
    - echo "Code coverage is 90%"
  tags:
    - ez_gitlab-runner

deploy-job:      
  stage: deploy
  script:
    - echo "Deploying application..."
    - echo "Application successfully deployed."
  tags:
    - ez_gitlab-runner

gitlab-ci.yml 的关键词

参考:https://fizzz.blog.csdn.net/article/details/113575625

1、全局关键词

image,services,before_script,after_script,tags,cache,artifacts,retry,timeout,interruptible

2、任务关键词
script, after_script, allow_failure, artifacts, before_script, cache, coverage, dependencies, environment, except, extends, image, include, interruptible, only, pages, parallel, release, resource_group, retry, rules, services, stage, tags, timeout, trigger, variables, when

script:可以执行shell脚本,执行目录就是项目的根目录(也就是.gitlab-ci.yml的目录),可以单行或多行;

job_a:
  script: npm install

job_b:
  script:
    - npm install a
    - npm install b

stage:按 stage 定义的顺序执行,而不是编写顺序;
only:指定执行分支,可以一个或多个;
tags:指定 执行的runner 标签,可以一个或多个;

stages:
  - test
  - build
  - deploy

# - test
# - release
# - deploy
before_script:
  - IMAGE_TAG=0.0.1
  - IMAGE=192.168.1.163/ubuntutest:$IMAGE_TAG
  - echo "build eap version:" $IMAGE_TAG

# 测试阶段
test_image:
  stage: test
  script:
    - echo "开始测试"


# 构建阶段
build_image:
  stage: build
  #variables:
  #  DOCKER_HOST: tcp://192.168.1.111:2375
  image: 192.168.1.163/maven_for_docker:3.6.2
  only:
    - master
  script:
    - echo "build " $IMAGE
    - mvn clean package -Dmaven.test.skip=true
    - export #查看所有变量信息
    - echo "start to build ${IMAGE}"
    - docker build -t $IMAGE .
    - docker push $IMAGE

# mvn 与 docker构建操作放放在同一个容器中
# apt-get update && apt-get install -y libltdl7

# 在线部署阶段
deploy_production:
  stage: deploy
  image: 192.168.1.163/imooc/kubectl:1.13.6.1 # 镜像名称
  only:
    - master
  script:
    - echo "deploy "$IMAGE
    - echo "开始测试"
    - kubectl set image -n namespacename deploy/test-deployment test=${IMAGE} --record
stages:
  - build
  - deploy

build:
  stage: build
  tags:
    - deploy
  only:
    - master
  script:
    - echo "mvn clean"
    - echo "mvn install"

deploy:
  stage: deploy
  tags:
    - deploy
  only:
    - master
  script:
    - echo "hello deploy"

配合ssh、tmux 和 tmuxp 部署到多台机器


设置 gitlab-runner 所在服务器到其他服务器的免密登录

1、在gitlab-runner 所在的机器生成 密钥

# 查看gitlab-runner 的执行账户
ps aux|grep gitlab-runner

su gitlab-runner
ssh-keygen -t rsa
cd  /home/gitlab-runner/.ssh
ls

2、复制到其他机器的 authorized_keys 中

复制到本机的 authorized_keys

cat /home/gitlab-runner/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys

使用 ssh-copy-id 命令复制到其他机器的 authorized_keys:

ssh-copy-id命令可以把本地主机的公钥复制到远程主机的authorized_keys文件上,也会给远程主机的用户主目录(home)和.ssh 以及 .ssh/authorized_keys 设置合适的权限。

.ssh 700权限,.ssh/authorized_key 600权限;

ssh-copy-id -i ~/.ssh/id_rsa.pub -p port remote_ip

3、在 gitlab-runner 所在机器 直接执行 ssh ip 即可ssh连接到其他机器,exit 即可退出;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值