无法在gitlab ci yaml文件中的作业之间共享全局变量值 [英] Can't share global variable value between jobs in gitlab ci yaml file

查看:345
本文介绍了无法在gitlab ci yaml文件中的作业之间共享全局变量值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用gitlab ci构建应用程序.

I'm trying to build an application using gitlab ci.

生成的文件的名称取决于时间,采用这种格式

The name of the generated file is depending on the time, in this format

DEV_APP_yyyyMMddhhmm

DEV_APP_yyyyMMddhhmm

(例如: DEV_APP_201810221340 ,对应于今天的日期2018/10/22 13h40).

(example: DEV_APP_201810221340, corresponding to the date of today 2018/10/22 13h40).

如何将该名称存储在.gitlab-ci.yml文件内的全局变量中?

How can I store this name in a global variable inside the .gitlab-ci.yml file?

这是我的.gitlab-ci.yml文件:

Here is my .gitlab-ci.yml file:

image: docker:latest
image: docker:latest
services:
- docker:dind

variables:
  DOCKER_DRIVER: overlay
  SPRING_PROFILES_ACTIVE: gitlab-ci
#   TIME: ""
#  BRANCH: ""
#  REC_BUILD_NAME: ""
  TIME: "timex"
  BRANCH: "branchx"
  DEV_BUILD_NAME: "DEV_APP_x"

stages:
- preparation
- build
- package
- deploy
- manual_rec_build
- manual_rec_package

job_preparation:
  stage: preparation
  script:
  - echo ${TIME}
  - export TIME=$(date +%Y%m%d%H%M)
  - "BRANCH=$(echo $CI_BUILD_REF_SLUG | sed 's/[^[[:alnum:]]/_/g')"
  - "DEV_BUILD_NAME=DEV_APP_${BRANCH}_${TIME}"
  - echo ${TIME}

maven-build:
  image: maven:3-jdk-8
  stage: build
  script:
  - echo ${TIME}
  - "mvn package -B"
  artifacts:
paths:
    - target/*.jar
  only:
  - merge-requests
  - /^feature\/sprint.*$/
  - /^DEV_.*$/
#  when: manual


docker-build:
  stage: package
  script:
  - echo ${TIME}
  - docker build -t registry.gitlab.com/mourad.sellam/actuator-simple .
  - docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN registry.gitlab.com
  - docker push registry.gitlab.com/mourad.sellam/actuator-simple
  only:
  - merge-requests
  - /^feature\/sprint.*$/
  - /^DEV_.*$/
  when: manual

    k8s-deploy-production:
  image: google/cloud-sdk
  stage: deploy
  script:
  - echo ${TIME}
  - echo "$GOOGLE_KEY" > key.json
  - gcloud auth activate-service-account --key-file key.json
  - gcloud config set compute/zone europe-west1-c
  - gcloud config set project actuator-sample
  - gcloud config set container/use_client_certificate True
  - gcloud container clusters get-credentials actuator-example
  - kubectl delete secret registry.gitlab.com
  - kubectl create secret docker-registry registry.gitlab.com --docker-server=https://registry.gitlab.com --docker-username=myUserName--docker-password=$REGISTRY_PASSWD --docker-email=myEmail@gmail.com
  - kubectl apply -f deployment.yml --namespace=production
  environment:
    name: production
    url: https://example.production.com
  when: manual

job_manual_rec_build:
  image: maven:3-jdk-8
  stage: manual_rec_build
  script:
  - echo ${TIME}
  - "mvn package -B"
  artifacts:
    paths:
    - target/*.jar
  when: manual
    #   allow_failure: false

job_manual_rec_package:
  stage: manual_rec_package
  variables:
  script:
    - echo ${TIME}
  - echo ${DEV_BUILD_NAME}
  - docker build -t registry.gitlab.com/mourad.sellam/actuator-simple:${DEV_BUILD_NAME} .
  - docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN registry.gitlab.com
- docker push registry.gitlab.com/mourad.sellam/actuator-simple
  artifacts:
    paths:
    - target/*.jar
  when: on_success
  #test 1

当我打电话

echo ${TIME}

显示"timex".

回声失败

您能告诉我如何存储全局变量并在每个作业中对其进行设置吗?

Could you tell me how to store a global variable and set it in each job?

推荐答案

检查是否

Check if GitLab 13.0 (May 2020) could help in your case:

从其他作业继承环境变量

现在可以在CI作业之间传递环境变量(或其他数据).

通过使用 dependencies 关键字(或DAG管道的 needs 关键字),如果作业是使用 dotenv来源的,则该作业可以继承其他作业的变量报告工件.

By using the dependencies keyword (or needs keyword for DAG pipelines), a job can inherit variables from other jobs if they are sourced with dotenv report artifacts.

与工件或传递文件相比,这提供了一种更优雅的方法来更新作业之间的变量.

This offers a more graceful approach for updating variables between jobs compared to artifacts or passing files.

请参见文档问题.

您可以从依赖作业中继承环境变量.

You can inherit environment variables from dependent jobs.

此功能利用了 artifacts:reports:dotenv 报告功能.

This feature makes use of the artifacts:reports:dotenv report feature.

带有 依赖项 的示例关键字.

Example with dependencies keyword.

build:
  stage: build
  script:
    - echo "BUILD_VERSION=hello" >> build.env
  artifacts:
    reports:
      dotenv: build.env

deploy:
  stage: deploy
  script:
    - echo $BUILD_VERSION # => hello
  dependencies:
    - build

需要 关键字:

build:
  stage: build

script:
    - echo "BUILD_VERSION=hello" >> build.env
  artifacts:
    reports:
      dotenv: build.env

deploy:
  stage: deploy
  script:
    - echo $BUILD_VERSION # => hello
  needs:
    - job: build
      artifacts: true

这篇关于无法在gitlab ci yaml文件中的作业之间共享全局变量值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆