GitLab Runnerg CI

作者: root007 分类: Linux 发布时间: 2024-09-27 18:21

创建 GitLab Runner

必须先安装 Runner 才能注册 runner。如何安装 Runner?

sudo curl -L --output /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64
chmod  +x /usr/local/bin/gitlab-runner 
  1. 将用户设置为root,同时也可以改变working-directory(工作区,从Gitlab拉取的代码会存放的目录)
  2. gitlab-runner install –working-directory /home/gitlab-runner –user root
  3. 重启Gitlab-Runner
  4. gitlab-runner restart

注册runner的时候,可以给它添加一个标签。添加标签以后,就可以在CI/CD配置文件里,通过标签来调用特定的Runner,如下所示:

job: 
  tags: 
  - ci


上面的内容是一个.gitlab-ci.yaml里的job内容,调用带有ci标签的Runner。
变量
变量存储您可以在作业脚本中使用的信息,如密码和密钥。每个 project 最多可以定义 8000 个变量。 了解更多。

模板

https://gitlab.com/gitlab-org/gitlab-foss/tree/master/lib/gitlab/ci/templates

stages:
  - mvn_build
  - docker_build 
 
variables:
  IMAGE_NAME: base/sentinel:1.8.7-custom-$CI_JOB_ID 
  CONTAINER_REGISTRY: harbor.xxx.com
build_mvn:
  tags: 
  - ci   
  stage: mvn_build 
  script:
    - cd sentinel-dashboard/
    - mvn  install -f pom.xml   
  artifacts:
    paths:
      - ./sentinel-dashboard/target/sentinel-dashboard.jar 
  rules:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'      
docker-build:
  tags: 
  - ci   
  stage: docker_build
  variables:
    DOCKER_IMAGE_NAME: $CONTAINER_REGISTRY/$IMAGE_NAME
  script:
    - cp ./sentinel-dashboard/target/sentinel-dashboard.jar ./
    - docker build --pull -t "$DOCKER_IMAGE_NAME" -f deploy/Dockerfile .
    - docker push "$DOCKER_IMAGE_NAME"
  dependencies:
    - build_mvn   
cat  /etc/systemd/system/gitlab-runner.service 
[Unit]
Description=GitLab Runner
After=network.target
 
[Service]
Type=simple
User=root
ExecStart=/usr/local/bin/gitlab-runner run
Restart=always
 
[Install]
WantedBy=multi-user.target

sudo systemctl daemon-reload
sudo systemctl enable gitlab-runner
sudo systemctl start gitlab-runner

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注