如何正确扩展.gitlab-ci.yml中的变量?

时间:2020-07-02 21:00:42

标签: gitlab devops gitlab-ci

在我的.gitlab-ci.yml文件中,我尝试在before_script部分中设置一个包含时间戳的变量。 然后,我想扩展该变量并将其附加到为构建创建的档案中。 该文件大致如下所示:

#.gitlab-ci.yml

image: node:14.4.0-buster

before_script:
  - export DATETIME=$(date "+%Y%m%d%H%M%S")

stages:
  #- test  # not relevant for this question
  - build
  - deploy

build:
  stage: build
  script:
    - npm install
    - npm run build
    - ls -la build
    - tar cvfJ build_${DATETIME}.tar.xz build/
    - sha1sum build_${DATETIME}.tar.xz
  artifacts:
    paths:
    - build_${DATETIME}.tar.xz

deploy:
  image: node:14.4.0-buster
  stage: deploy
  script:
    - sha1sum build_${DATETIME}.tar.xz
    - tar xvfJ build_${DATETIME}.tar.xz
    # do the actual deploy
  only:
  - master

deploy阶段在sha1sum失败。输出为:

$ sha1sum build_${DATETIME}.tar.xz
sha1sum: build_20200702165854.tar.xz: No such file or directory

这表明扩展正确完成,但是出了点问题。

我想念什么?

1 个答案:

答案 0 :(得分:1)

before_script在每个作业开始时运行,因此export DATETIME=$(date "+%Y%m%d%H%M%S")在两个阶段都不同。

例如,最好使用${CI_COMMIT_SHORT_SHA}

相关问题