gpt4 book ai didi

yaml - 无法在 gitlab ci yaml 文件中的作业之间共享全局变量值

转载 作者:行者123 更新时间:2023-12-05 00:46:56 24 4
gpt4 key购买 nike

我正在尝试使用 gitlab ci 构建一个应用程序。

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

DEV_APP_yyyyMMddhhmm



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

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

这是我的 .gitlab-ci.yml 文件:
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”。

echo faild

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

最佳答案

检查是否 GitLab 13.0 (May 2020)可以帮助您的情况:

Inherit environment variables from other jobs

Passing environment variables (or other data) between CI jobs is now possible.

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.


documentationissue .

You can inherit environment variables from dependent jobs.

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

Example with the needs keyword:

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

关于yaml - 无法在 gitlab ci yaml 文件中的作业之间共享全局变量值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52928915/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com