文件中的作业之间共享全局变量值

文件中的作业之间共享全局变量值

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

问题描述

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

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

DEV_APP_yyyyMMddhhmm

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

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

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

图像:docker:latest图片:码头工人:最新服务:- 码头工人:dind变量:DOCKER_DRIVER:覆盖SPRING_PROFILES_ACTIVE:gitlab-ci#   时间: ""#  分支: ""# REC_BUILD_NAME:"时间:时间"分支:分支"DEV_BUILD_NAME:DEV_APP_x"阶段:- 准备- 建造- 包裹- 部署- manual_rec_build- manual_rec_package工作准备:阶段:准备脚本:- 回声 ${TIME}- 出口时间=$(日期+%Y%m%d%H%M)- BRANCH=$(echo $CI_BUILD_REF_SLUG | sed 's/[^[[:alnum:]]/_/g')"- DEV_BUILD_NAME=DEV_APP_${BRANCH}_${TIME}"- 回声 ${TIME}行家构建:图片:maven:3-jdk-8阶段:构建脚本:- 回声 ${TIME}-mvn 包 -B"文物:路径:- 目标/*.jar只要:- 合并请求-/^feature/sprint.*$/-/^DEV_.*$/# 时间:手动码头工人建造:阶段:包装脚本:- 回声 ${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只要:- 合并请求-/^feature/sprint.*$/-/^DEV_.*$/时间:手动k8s-部署-生产:图片:谷歌/云-SDK阶段:部署脚本:- 回声 ${TIME}- 回声$GOOGLE_KEY">键.json- gcloud auth activate-service-account --key-file key.json- gcloud 配置设置计算/区域 europe-west1-c- gcloud 配置设置项目执行器样本- gcloud config set container/use_client_certificate True- gcloud 容器集群 get-credentials actuator-example- kubectl 删除密钥 registry.gitlab.com- kubectl 创建秘密 docker-registry registry.gitlab.com --docker-server=https://registry.gitlab.com --docker-username=myUserName--docker-password=$REGISTRY_PASSWD [email protected] kubectl apply -f deployment.yml --namespace=production环境:名称:生产网址:https://example.production.com时间:手动job_manual_rec_build:图片:maven:3-jdk-8阶段:manual_rec_build脚本:- 回声 ${TIME}-mvn 包 -B"文物:路径:- 目标/*.jar时间:手动#allow_failure: falsejob_manual_rec_package:阶段:manual_rec_package变量:脚本:- 回声 ${TIME}- 回声 ${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文物:路径:- 目标/*.jar何时:on_success#测试 1

当我打电话时

回显 ${TIME}

它显示timex".

回显失败

你能告诉我如何存储一个全局变量并在每个作业中设置它吗?

解决方案

检查是否 GitLab 13.0(2020 年 5 月) 可以帮助您解决问题:

从其他作业继承环境变量

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

通过使用 dependencies 关键字(或用于 DAG 管道的 needs 关键字),一个作业可以从其他作业继承变量,如果它们来自 dotenv报告工件.

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

请参阅 文档问题.

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

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

依赖项的示例关键字.

构建:阶段:构建脚本:- 回声BUILD_VERSION=hello">>构建.env文物:报告:dotenv: build.env部署:阶段:部署脚本:- 回声 $BUILD_VERSION # =>你好依赖项:- 建造

needs 关键字:

构建:阶段:构建脚本:- 回声BUILD_VERSION=hello">>构建.env文物:报告:dotenv: build.env部署:阶段:部署脚本:- 回声 $BUILD_VERSION # =>你好需求:- 工作:建造文物:真

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

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

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

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

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 [email protected]
  - 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

When i call

echo ${TIME}

It displays "timex".

echo faild

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:

See documentation and issue.

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 文件中的作业之间共享全局变量值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 19:34