我正在尝试使用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”。
您能告诉我如何存储全局变量并将其设置在每个作业中吗?
答案 0 :(得分:7)
检查GitLab 13.0 (May 2020)是否可以解决您的问题:
从其他作业继承环境变量
现在可以在CI作业之间传递环境变量(或其他数据)。
通过使用
dependencies
关键字(或对于DAG管道使用needs
关键字),如果作业源自dotenv
报告工件,则该作业可以从其他作业继承变量。与工件或传递文件相比,这提供了一种更优雅的方法来更新作业之间的变量。
请参见documentation和issue。
您可以从依赖作业中继承环境变量。
此功能利用了
artifacts:reports:dotenv
报告功能。带有
dependencies
关键字的示例。
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
带有
needs
关键字的示例:
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
答案 1 :(得分:1)
在Gitlab CE上有一个开放的issue 47517 'Pass variables between jobs'。
CI / CD通常需要将信息从一项工作传递到另一项工作, 工件可以用于此,尽管这是一个沉重的解决方案 意外的副作用。工作区是另一个建议 作业之间的文件。但是有时候您根本不想传递文件, 只是一小部分数据。
我也遇到了同样的问题,并且通过将DATA存储在文件中,然后在其他Jobs中访问它来解决此问题。
答案 2 :(得分:0)
您可以使用工件在作业之间传递数据。这是来自Flant的示例,用于检查先前的管道人工决策:
approve:
script:
- mkdir -p .ci_status
- echo $(date +%s) > .ci_status/approved
artifacts:
paths:
- .ci_status/
NOT approve:
script:
- mkdir -p .ci_status
- echo $(date +%s) > .ci_status/not_approved
artifacts:
paths:
- .ci_status/
deploy to production:
script:
- if [[ $(cat .ci_status/not_approved) > $(cat .ci_status/approved) ]]; then echo "Need approve from release engineer!"; exit 1; fi
- echo "deploy to production!"