GitLab CI/CD 之 pipeline--trigger传参案例

本篇目录链接

    • 项目说明:
    • 流程思路:
      • 说明:
    • CI file:
      • http-ci file:
      • nginx-ci file:
      • 结果展示:

项目说明:

基于通过http项目去触发nginx项目。

流程思路:

在http ci中定义VERSION变量,然后通过pipelinetriggeer传参,在以变量的方式传入到nginx项目中;
通过修改nginx ci中的镜像tag版本,定义为version变量

说明:

此处我是将预定义变量、ci文件中定义变量、传参结合一起做的。其中

  • variables下的REF_NAME、VERSION为文件中定义变量
  • CITOKEN为预定义变量
  • -F variables[version]=${VERSION} 为传参(通过调用文件中定义的变量)

CI file:

http-ci file:

image: docker:19.03.13
variables:
  REF_NAME: main
  VERSION: v2
stages:
  - Test
  - Build
  - Deploy

test:
  stage: Test
  tags:
    - test
  script:
    - docker info

build-http:
  stage: Build
  tags:
    - test
  script:
    - docker build -t 192.168.137.14:5000/http:v1 -f Dockerfile .
    - docker push 192.168.137.14:5000/http:v1

deploy-http:
  stage: Deploy
  image: centos:7
  tags:
    - test
  script:
    - "curl -X POST -F token=${CITOKEN} -F ref=${REF_NAME} -F variables[version]=${VERSION} http://192.168.137.14/api/v4/projects/2/trigger/pipeline"
    - echo "Compile complete."
    - sleep 1

nginx-ci file:

image: docker:19.03.13
stages:          # List of stages for jobs, and their order of execution
  - build
  - test
  - deploy

build-job:       # This job runs in the build stage, which runs first.
  stage: build
  tags:
    - test
  script:
    - which docker
lint-test-job:   # This job also runs in the test stage.
  stage: test    # It can run at the same time as unit-test-job (in parallel).
  tags:
    - test
  script:
    - docker version

deploy-job:      # This job runs in the deploy stage.
  stage: deploy  # It only runs when *both* jobs in the test stage complete successfully.
  tags:
    - test
  script:
    - docker build -t 192.168.137.14:5000/nginx:${version} -f Dockerfile .
    - docker push 192.168.137.14:5000/nginx:${version}

结果展示:

http流水线:
GitLab CI/CD 之 pipeline--trigger传参案例_第1张图片
GitLab CI/CD 之 pipeline--trigger传参案例_第2张图片
nginx流水线:
GitLab CI/CD 之 pipeline--trigger传参案例_第3张图片
GitLab CI/CD 之 pipeline--trigger传参案例_第4张图片

你可能感兴趣的:(gitops,gitlab)