欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

gitlab CI/CD环境搭建

程序员文章站 2022-04-30 08:39:32
...

1.安装gitlab-runner

# Linux x86-64
sudo wget -O /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/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

以上是Linux64位环境下的安装,其他的环境参考 https://docs.gitlab.com/runner/install/linux-manually.html

 

2.注册gitlab-runner

sudo gitlab-runner register

他会提示你写gitlab的地址和token,这地址可以在gitlab的网页上的settings->CI/CD Pipelines 找到如图:

gitlab CI/CD环境搭建

另外,配置好的runner可能需要开启-Run untagged jobs,同样在上图所示的页面中有一个Runners activated for this project列表,点击下面runner右侧的修改按钮,在页面中把 Run untagged jobs勾选即可

gitlab CI/CD环境搭建

其他具体参考:https://docs.gitlab.com/runner/register/index.html

 

3.在项目根目录中添加 .gitlab-ci.yml 如下:

# 提交dev分支时触发
dev job:
  only:
    - dev
  script: 
    - echo 'dev' >> ci.log

# 提交test分支时触发
test job:
  only:
    - test
  script: 
    - ssh aaa@qq.com -i /home/gitlab-runner/.ssh/ci_key << ssh2
    - date >> ci.log
    - cd /home/ubuntu/first_project/
    - git checkout test
    - git pull origin test
    - ssh2

# 提交master分支时触发
master job:
  only:
    - master
  script: 
    - ssh aaa@qq.com -i /home/gitlab-runner/.ssh/ci_key << ssh2
    - date >> ci.log
    - cd /home/ubuntu/first_project/
    - git checkout master
    - git pull origin master
    - ssh2
    

然后,本地提交代码后runner就会执行.yml里相关脚本

全文参考:https://docs.gitlab.com/ee/ci/quick_start/README.html

yml教程:https://docs.gitlab.com/ee/ci/yaml/README.html