GitLab CI/CD配置使用教程

因为公司需要我们使用GitLab来提交代码,使用GitLab CI/CD
。之前我是没有接触过这个东西的,昨天搞了一天,也是踩了一天的坑,现在记录一下

这个是GitLab CI/CD 的官方文档:https://docs.gitlab.com/ee/ci/README.html

1、GitLab-runner安装

(1)linux安装

第一步首先是安装GitLab-runner了,因为我们的GitLab是公司搭建起来,给我们提交作业的,不用我们自己创建。一开始我以为要在部署GitLab服务器上面才能注册GitLab-runner。然而并不是。我们可以在自己的服务器或者虚拟机上注册一个,他会自动挂载。

安装GitLab runner地址:https://docs.gitlab.com/runner/install/ 上面安装到不同操作系统的教程,我的是debian虚拟机。

# Replace ${arch} with any of the supported architectures, e.g. amd64, arm, arm64
# A full list of architectures can be found here https://gitlab-runner-downloads.s3.amazonaws.com/latest/index.html
curl -LJO "https://gitlab-runner-downloads.s3.amazonaws.com/latest/deb/gitlab-runner_${arch}.deb"

这个是官网的示例代码,我们要去到 https://gitlab-runner-downloads.s3.amazonaws.com/latest/index.html 这里面找到我们对应的操作系统版本,我的是这个

在这里插入图片描述

下载下来之后就是安装了,安装也很简单,对应自己下载的版本就好了

dpkg -i gitlab-runner_<arch>.deb

输入gitlab-runner出现如下信息,即安装完成

在这里插入图片描述

(2)mac安装

官方教程:https://docs.gitlab.com/runner/install/osx.html

brew install gitlab-runner

brew services start gitlab-runner

2、GitLab-runner注册

我们打开gitlab网站,找到

在这里插入图片描述

找到runner选项

在这里插入图片描述

然后我们在虚拟机输入命令sudo gitlab-runner register(一定要运行在root权限下)按照提示输入对应的信息,URL和token在gitlab -> settings -> CD/CD -> runner找,我这里使用的是shell方式注册一个,绝大部分可以使用docker方式,但是docker方式需要提供一个镜像,也就是你的gitlab-ci.yml所写的内容使用到了什么镜像

在这里插入图片描述

这时候我们去看一下设置的页面,下面应该会有一个runner
在这里插入图片描述

但是现在去看,是一个黑色的三角,提示:New runner. Has not connected yet.

这个时候去服务器上输入sudo gitlab-runner verify,此时黑色三角变成绿色的圆形,表明runner可以使用了

在这里插入图片描述

gitlab-runner 可以在很多模式下运行,有parallels, shell, docker+machine, kubernetes, custom, docker-ssh, ssh, virtualbox, docker-ssh+machine, docker

executor shell

以宿主机作为Runner的所有jobs的执行器。

Runner将会从远程仓库pull你的工程,工程的目录为:<working-directory>/builds/<short-token>/<concurrent-id>/<namespace>/<project-name>

如果你使用了cache,那么cache将会存在<working-directory>/cache/<namespace>/<project-name>

所以如果我们在.gitlan-ci.yaml写上部署的语句docker-compose up的时候,该项目会真实的在宿主机上面运行,从而做到了自动CICD

3、配置gitlab-ci.yml

我是按照官方文档的配置gitlab-ci.yml 文件内容写的,其实就是输出了一些东西(没有用到一些镜像的东西,所以我这里使用shell,如果你使用了golang的一些命令,那就要使用docker构建,提供golang相关镜像)

保存文件名为:gitlab-ci.yml

stages:
  - build
  - test
  - deploy

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一下,CI就会自动运行

然后我们可以去看一下CI/CD -> Pipelines里面的运行情况

在这里插入图片描述
在这里插入图片描述

4、踩坑

(1)runner is alive but pipeline is pending

但是通过以上的步骤,CI会一直处于pending状态,点进去会发现

在这里插入图片描述

其实这里就说明了,runner一直没有启动,任务都没有进去,卡住了

我们从settings -> CI/CD -> runner里面看,也是绿灯,但是就是一直卡住了,我研究了一下午这个问题,最后发现可能是runner那边出现了假启动的状态,问了导师,导师让我run一下试试,看看runner有没有真的跑起来

sudo gitlab-runner listsudo gitlab-runner run

在这里插入图片描述

在运行命令的一瞬间,就开始跑CI任务了,说明真的是runner没有起来

在这里插入图片描述

完结撒花!

  • 2
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值