gitlab-runner安装注册

目录

 

gitlab-runner的安装

gitlab-runner的类型

gitlab-runner的注册

CICD持续集成和部署

案列分享

FAQ

问题1:

问题2:

问题3:


gitlab-runner的安装

curl --output /usr/local/bin/gitlab-runner "https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-darwin-amd64"

chmod +x /usr/local/bin/gitlab-runner

gitlab-runner的类型

如下所示gitlab-runner分为shared、group、specific等几个类型

gitlab-runner的注册

下面我们注册一个shared类型的runner. 通过管理员登录 gitlab ---- Admin area----Overview ---- Runner   查看需要注册的 URL与 Token(令牌)

#交互式
# gitlab-runner register
Runtime platform                                    arch=amd64 os=linux pid=28542 revision=8fa89735 version=13.6.0
Running in system-mode.                            
                                                   
Enter the GitLab instance URL (for example, https://gitlab.com/):
http://172.16.xx.xx:84/
Enter the registration token:
CHLiFzF-BxVkAauJEim7
Enter a description for the runner:
[db003-hf-xxx]: develop   
Enter tags for the runner (comma-separated):
develop
Registering runner... succeeded                     runner=CHLiFzF-
   
Enter an executor: docker-ssh, parallels, shell, ssh, docker+machine, docker-ssh+machine, custom, virtualbox, kubernetes, docker:
shell 
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded! 


#非交互式
gitlab-runner register --non-interactive --executor 'shell' --url 'http://172.16.xx.xx:84//' --registration-token 'CHLiFzF-BxVkAauJEim7'
Runtime platform                                    arch=amd64 os=linux pid=28016 revision=8fa89735 version=13.6.0
Running in system-mode.                            
                                                   
Registering runner... succeeded                     runner=CHLiFzF-
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded! 

 注册完可以在页面中看到一个shared类型的runner.

上面注册的时候,我们选择了shell类型的执行器,下面是几个常见执行器的功能对比表

 

CICD持续集成和部署

创建一个项目,然后修改代码,推送到master分支后,自动完成流水线工作

在项目的根下,创建.gitlab-ci.yml文件,内容如下:

stages:
  - build
  - deploy

build:
  stage: build
  tags:
    - deploy
  only:
    - master
  script:
    - echo "mvn clean"
    - echo "mvn install"

deploy:
  stage: deploy
  tags:
    - deploy
  only:
    - master
  script:
    - echo "hello deploy"

 当我们编辑任意文件,提交到master分支后,流水线开始工作,成功后,如下所示。

由上图可以看出,流水线工作已经开启,对于具体的任务,需要在.gitlab-ci.yml文件中编写。

案列分享

gitlab-ci.yml文件

include:
  - '/templates/.before-script.yml'
    
stages:
  - build
  - deploy_sha
  - deploy_tag
  
    
build:
  stage: build
  tags:
    - deploy
  only:
    - master
  script: 
    - docker build -t 172.16.xx.xx:85/testproject/$SERVICE_NAME:$CI_COMMIT_SHA .

deploy_sha:
  stage: deploy_sha
  tags:
    - deploy
  only:
    - master
  script: |
    set -e -o pipefail
    docker login -u admin -p 123456  172.16.xx.xx:85 
    docker push 172.16.xx.xx:85/testproject/$SERVICE_NAME:$CI_COMMIT_SHA


deploy_tag:
  stage: deploy_tag
  tags:
    - deploy
  only:
    - tags
  script: |
    set -e -o pipefail
    docker login -u admin -p 123456  172.16.xx.xx:85 
    docker tag 172.16.xx.xx:85/testproject/$SERVICE_NAME:$CI_COMMIT_SHA  172.16.154.14:85/testproject/$SERVICE_NAME:$CI_COMMIT_TAG
    docker push 172.16.xx.xx:85/testproject/$SERVICE_NAME:$CI_COMMIT_TAG
   

触发构建机制,然后可以看到两个job,以及镜像已经推送到harbor私有仓库中

FAQ

问题1:

升级git操作,可自行百度。

问题2:

解决方案,给gitlab-runner用户添加权限。

问题3:

需要给docker添加访问harbor的权限

docker login -u admin -p 123456  172.16.xx.xx:85     #harbor的登陆名和密码

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值