使用gitlab实现CI/CD流程分为两步:
进入gitlab项目,点击settings,选择CI/CD, 在右边的页面中,点开Runners,进入到如下页面。
点击图中的1Show Runner installation instruction
,里面有安装步骤。
(1)使用命令dpkg --print-architecture
查看服务器架构,根据不同的环境和架构下载安装包。
# Download the binary for your system
sudo curl -L --output /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64
(2)下载完后,给gitlab-runner执行权限
# Give it permissions to execute
sudo chmod +x /usr/local/bin/gitlab-runner
(3)建一个gitlab-runner用户,之后使用CI/CD时,都是在这个用户下进行
# Create a GitLab CI user
sudo useradd --comment 'GitLab Runner' --create-home gitlab-runner --shell /bin/bash
(4)创建好了用户之后,安装gitlab-runner
# Install
sudo gitlab-runner install --user=gitlab-runner --working-directory=/home/gitlab-runner
(5)安装成功以后,启动gitlab-runner服务
# run as service
sudo gitlab-runner start
在自己的runner服务器上,输入如下命令,开始注册runner
# sudo 必须加,保证和前面一致,否则runner不可用
sudo gitlab-runner register
/etc/gitlab-runner/config.toml
文件中查看runner的配置在需要进行测试的分支的根目录创建.gitlab-ci.yml文件,.gitlab-ci.yml文件主要包括以下两部分内容,由YAML语言编写:
具体的编写规则详见GitLab CI流水线配置文件.gitlab-ci.yml详解
一个简单的例子:
build-job:
stage: build
script:
- echo "Hello, $GITLAB_USER_LOGIN!"
test-job1:
stage: test
script:
- echo "This job tests something"
test-job2:
stage: test
script:
- echo "This job tests something, but takes more time than test-job1."
- echo "After the echo commands complete, it runs the sleep command for 20 seconds"
- echo "which simulates a test that runs 20 seconds longer than test-job1"
- sleep 20
deploy-prod:
stage: deploy
script:
- echo "This job deploys something from the $CI_COMMIT_BRANCH branch."
编写或修改完后,点击commit
,测试pipeline就会自动开始。可视化查看。
包含三个阶段Stages(build,test,deploy),不同的stage之间时串行执行。相同的stage之间时并行执行(需要修改cong.toml
中的concurrent
,如下图)stages个数和名称可以自己定义。
(1) 注册runner后,runner不可用,呈现灰色
可能权限没有处理好,注册时候的命令应该是sudo gitlab-runner start
(2)runner可用,但是在运行时出现stuck
标签
https://stackoverflow.com/questions/53370840/this-job-is-stuck-because-the-project-doesnt-have-any-runners-online-assigned
日
(3)如果安装有什么问题,可以卸载重装,卸载的时候出现userdel: user gitlab-runner is currently used by process 1000
,加上-f
强制删除
sudo gitlab-runner uninstall
sudo rm -rf /usr/local/bin/gitlab-runner
sudo userdel -f gitlab-runner
sudo rm -rf /home/gitlab-runner/
gitlab runner安装与使用