Gitlab 自动化部署 ——GitLab Runner

安装Runner

两种方式,一种Omnibus,一种源码安装(gitlab自身推荐)

简介:

  1. gitlab-ci url 和 token在Admin Area->Overview->Runners中;
  2. 注册中需要填写tag,tag是与gitlab-ci.yml里配置的job的tag相对应的,不填写就是公共runner;
  3. 注册结果中可以删除locked字段并设置可以接受没打tag的job,这样就成了公共runner了(可以执行没有tag的job了)
  4. 还有一种yum install gitlab-ci-multi-runner的版本,百度参考另外的文章吧awa
  5. 如果因为版本不兼容想要升级GitLab版本的话参考一下这偏文

源码

# 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

# Give it permission to execute
sudo chmod +x /usr/local/bin/gitlab-runner

# Create a GitLab Runner user
sudo useradd --comment 'GitLab Runner' --create-home gitlab-runner --shell /bin/bash

# Install and run as a service
sudo gitlab-runner install --user=gitlab-runner --working-directory=/home/gitlab-runner
sudo gitlab-runner start

yum安装,先来个清华镜像

cat > /etc/yum.repos.d/gitlab-runner.repo << END
[gitlab-runner]
name=gitlab-runner
baseurl=https://mirrors.tuna.tsinghua.edu.cn/gitlab-runner/yum/el\$releasever-\$basearch/
repo_gpgcheck=0
gpgcheck=0
enabled=1
gpgkey=https://packages.gitlab.com/gpg.key
END

yum makecache && yum install gitlab-runner -y

注册 - 不管你是源码安装还是yum安装最后都还是需要注册的

注册需要一个叫做token的东西,可以在Admin Area->Overview->Runners中找到(实际上还要多点几个按钮)。

# 15.6之前使用register,15.6之后用deploy

sudo gitlab-runner register --url <仓库地址> --registration-token <runners页面给的token>
# 举个例子,我自己用的是
# sudo gitlab-runner register --url http://192.168.0.17/ --registration-token XM99nGBtLmGHwvxSFfyX

后面有一堆内容,需要自行输入(前缀带--的可以直接补充在命令行后面)

  1. --url GitLab的地址
  2. --registration-token 注册的token
  3. --description Runner的描述
  4. tags
    • tags就是与gitlab-ci.yml里配置的job的tag相对应的,不填写就是公共runner,多个tag中间用英文逗号分隔;
    • tag可以根据项目进行垂直切分,或者根据流程进行水平切分,具体看自己使用情况。
  5. maintenance note(可选)
    • 据说填写shell就行了
  6. --executor 使用什么执行Runner,个人就shell咯

最后记住这行Configuration (with the authentication token) was saved in "/etc/gitlab-runner/config.toml,修改这个文件可以参考这篇文章

  1. 结束,没啦,看啥看!

写一个简单的gitlab-ci.yml (Springboot)

stages:
  - install
  - test
  - package
  - deploy

cache:
  key: test-cache
  paths:
    - target

install-job:
  stage: install
  tags:
    - test
  script:
    - mvn clean install -f pom.xml

test-job:
  stage: test
  tags:
    - test
  script:
    - mvn test

package-job:
  stage: package
  tags:
    - test
  artifacts:
    paths:
      - ./target/*.jar
  script:
    - mvn clean package -DskipTest

deploy-job:
  stage: deploy
  tags:
    - test
  script:
    - echo $TEST_HOST_NAME@$TEST_HOST_IP
    - scp -r ./target/*.jar $TEST_HOST_NAME@$TEST_HOST_IP:/www/wwwroot/not_just_notebook/replace_version
    - $TEST_HOST_PASSWORD
    - ssh $TEST_HOST_NAME@$TEST_HOST_IP
    - $TEST_HOST_PASSWORD
    - cd /www/wwwroot/not_just_notebook
    - echo is done!
    #    - ./inpack.sh  # 砍端口并重新部署
    - exit

在具体仓库 -> 设置页 -> CI/CD中配置变量(当然,管理页也能配,但好像是全局的,我不小心配到全局了(如果分支是非保护分支需要在变量设置中关掉保护变量,否则可能拿不到参数),所以你看我的yml脚本的变量用TEST_HOST开头的,正常用):

  • NAME -> root
  • IP -> 192.168.0.19
  • PASSWORD -> 你们真的不会以为我会把密码贴出来的吧?!

一些问题(他们喜欢说Troubleshoot)

gitlab中出现的所有地址都是使用的gitlab.example.com,包括runner和pipline解析

这里我直接修改了/etc/gitlab/gitlab.rb,然后gitlab-ctl reconfigure,再看地址就正确了(一看就不是从我上一篇文章来的)。

大概提交了17次代码(同一天)

报错:

Fetching changes with git depth set to 20...
重新初始化现存的 Git 版本库于 /home/gitlab-runner/builds/_zuVsqN_/0/root/not_just_notebook/.git/
fatal: git fetch-pack: expected shallow list
fatal: The remote end hung up unexpectedly
ERROR: Job failed: exit status 1

更新Git版本

yum install http://opensource.wandisco.com/centos/7/git/x86_64/wandisco-git-release-7-2.noarch.rpm -y && yum install git -y

重新跑pipeline,通过。
说实话这个报错很迷,这种情况怎么也不应该因为版本报错啊,但是据说有些程序构建会用到git版本号做计数器??anyway~解决了就是好事儿~

一些未测试的想法

  • 如果--executor选择了docker,部署Java的话镜像就用maven或者gradle;前端的话考虑一下webpack脚手架之类的 ——因为嫌麻烦就直接装进了本机里(用shell并直接搭建环境)
  • neebs加速构建速度

参考文章

gitlab-runner+docker实现自动化部署流水线 - https://blog.csdn.net/qq_35314093/article/details/123891021
gitlab-runner各个参数的具体含义 - https://www.cnblogs.com/wu-wu/p/13269950.html
gitlab CI/CD 变量 - https://docs.gitlab.cn/14.9/jh/ci/variables/index.html
gitlab 中出现的所有地址都是使用的gitlab.example.com - https://www.likecs.com/show-305674981.html
gitlab 分支保护 - https://blog.csdn.net/qq_30273575/article/details/124858583
更新git - https://blog.csdn.net/heian_99/article/details/128129761
gradle构建期间将git版本号添加到项目版本中 - https://www.lmlphp.com/user/151062/article/item/2833535/

其它文章(做实验环境时使用)

wget安装高版本maven - http://www.manongjc.com/detail/29-rzgwlqtazpghaxo.html
yum查找安装包下载地址(用于配置全局文件) - https://blog.csdn.net/m0_67401270/article/details/126602887
换一个高版本的JDK~ - https://www.oracle.com/java/technologies/downloads/#jdk19-mac

java版本更换残留(说实话,我还在对我/usr/bin/里的JDK15心有余悸。。) - https://blog.csdn.net/weixin_65920414/article/details/127648764

posted @ 2022-12-13 11:54  Astroline  阅读(887)  评论(0编辑  收藏  举报