Ubuntu18注册gitlab-runner并激活CI/CD
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
# Linux x86 sudo wget -O /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-386
# Linux arm sudo wget -O /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-arm
设置执行权限
sudo chmod +x /usr/local/bin/gitlab-runner
创建 GitLab CI 用户
useradd --comment 'gitLab-runner' --create-home gitlab-runner --shell /bin/bash
运行服务
gitlab-runner install --user=gitlab-runner --working-directory=/home/gitlab-runner gitlab-runner start
gitlab-runner注册
打开 gitlab 项目 -> 设置 -> CI / CD -> Runners 设置,获取令牌
运行以下命令开始注册:
sudo gitlab-runner register
输入GitLab实例URL:
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com ) https://xxx.xxx
输入 注册令牌 来注册 Runner
Please enter the gitlab-ci token for this runner xxx
输入 Runner 说明
Please enter the gitlab-ci description for this runner [hostame] gitlab-runner
输入 Runner 的 tags
Please enter the gitlab-ci tags for this runner (comma separated): tag
输入 Runner 执行方式
Please enter the executor: ssh, docker+machine, docker-ssh+machine, kubernetes, docker, parallels, virtualbox, docker-ssh, shell: shell
验证
runner列表
runner配置修改
.gitlab-ci.yml
要实现CI/CD,首先要在项目根路径下创建.gitlab-ci.yml文件,我的目录结构如下:
.gitlab-ci.yml文件内容:
# CI/CD管道分三步,这里可以根据需求自行配置 stages: - build - test - deploy build: stage: build # 测试构建 script: - gradle clean build -x test test: stage: test # 这里可以写一些测试相关的脚本 script: - echo SKIPPING TEST deploy: stage: deploy script: # 修改部署脚本文件类型为可执行文件 - chmod +x .gitlab-ci/deploy.sh # 执行脚本 - sh .gitlab-ci/deploy.sh only: # 只有master分支执行这个步骤 - /^master.*$/ tags: # 对应注册runner时候的tag名 - tag
deploy.sh文件内容:
#!/usr/bin/env bash echo "Start deploying!" gradle build # 把文件从本地传到服务器上去 scp /home/gitlab-runner/builds/n637Af4S/0/root/project-test/build/libs/project-test-0.0.1-SNAPSHOT.jar root@服务器ip:/usr/local/project_test/project-test-0.0.1-SNAPSHOT.jar # 执行服务器上的部署脚本文件 ssh root@服务器ip "sh /usr/local/project_test/deploy.sh" echo "Deploy jar success!"
执行deploy.sh文件需要两个前提条件,一是需要gitlat服务器可以免密登录待部署服务器,而是要在待部署服务器上也创建一个deploy.sh文件。成功执行待部署服务器上的deploy.sh文件需要文件夹创建格式和我这里相同。
待部署服务器上的deploy.sh文件:
#!/bin/bash #defined export JAVA_HOME=/usr/local/java/jdk1.8.0_231 PID=`ps -ef | grep java | grep project-test | awk '{print $2}'` if [ -z $PID_EXIST];then echo the process $PID is not exist else echo the process $PID exist kill -9 $PID fi echo "remover jar file" rm -rf /jar/project-test* echo "copy jar to dir" cp project-test-0.0.1-SNAPSHOT.jar /usr/local/project_test/jar/project-test.jar echo "start jar" nohup java -jar /usr/local/project_test/jar/project-test.jar >/dev/null 2>&1 & echo "started"
Q:提交代码后没有触发,一直停留在pending
A:报错:This job is stuck, because you don't have any active runners that can run this job.
原因:注册gitlab runner 的时候,有一步提示:Can run untagged jobs: [false/true],默认值为false。
这句话的意思是:是否在没有标记tag的job上运行,如果选择默认值false,那没有标记tag的代码提交是不会触发gitlab runner的,如果做测试,最好填true。
Q: bash: 行 91: git: 未找到命令
A:gitlab服务器需要安装git
Q: bash: 行 90: gradle: 未找到命令
A:gitlab服务器需要安装gradle