进入管道的下一阶段时删除工件
Artifact removed when entering next stage in pipeline
我正在 GitLab CI 中创建管道,目前一切正常,除了一件事,那就是当我在前一阶段将 txt 文件保存到工件时,它会在下一阶段被删除。
这是工作 1:
prep for build (other branches):
stage: Fetching tags
image: alpine
allow_failure: true
script:
- apk add git
- git --version
- LATEST_TAG=$(git describe --abbrev=0 --tags)
- echo $LATEST_TAG > tags.txt
- echo "Latest tag from other branches is:"
- cat tags.txt
only:
refs:
- master
- /^(([0-9]+)\.)?([0-9]+)\.x/
- rc
artifacts:
paths:
- tags.txt
expire_in: 4 week
tags:
- auth
- dev
- docker
这是使用作业 1 中的工件的作业 2:
execution (other branches):
stage: Build and Push Image
dependencies:
- prep for build (other branches)
image:
name: gcr.io/kaniko-project/executor:debug
entrypoint: [""]
script:
- LATEST_TAG=cat tags.txt
- echo "Latest tag is $LATEST_TAG"
- echo "{\"auths\":{\"$HARBOR_REGISTRY\":{\"username\":\"$HARBOR_USER\",\"password\":\"$HARBOR_PASSWORD\"}}}" > /kaniko/.docker/config.json
- /kaniko/executor --cache=true --context $CI_PROJECT_DIR --dockerfile $CI_PROJECT_DIR/Dockerfile --destination $HARBOR_REGISTRY_IMAGE:$LATEST_TAG
only:
refs:
- master
- /^(([0-9]+)\.)?([0-9]+)\.x/
- rc
tags:
- auth
- dev
- docker
这些是作业 2 的日志:
Checking out 21a67bd3 as master...
Removing CHANGELOG.md
Removing coverage-report/
Removing coverletReport/
Removing tags.txt
Skipping Git submodules setup
Downloading artifacts
00:01
Downloading artifacts for prep for build (other branches) (50868)...
Downloading artifacts from coordinator... ok id=50868 responseStatus=200 OK token=sADMXgpX
Executing "step_script" stage of the job script
.....
LATEST_TAG=cat tags.txt
/busybox/sh: eval: line 109: tags.txt: not found
Cleaning up file based variables
00:01
ERROR: Job failed: exit code 127
知道为什么会这样吗?
工件下载正常,问题是您设置变量的方式 LATEST_TAG
。您似乎试图将其设置为操作 cat tags.txt
的结果,但您却将其设置为字符串 cat
。 tags.txt
行的下一部分被解释为另一个命令,所以它说 tags.txt not found
因为它不能将 tags.txt
计算为命令。
将该行更改为 LATEST_TAG=$(cat tags.txt)
以将 LATEST_TAG
设置为 cat tags.txt
的结果。
有关 $(command)
的更多信息,请参阅 this answer
我正在 GitLab CI 中创建管道,目前一切正常,除了一件事,那就是当我在前一阶段将 txt 文件保存到工件时,它会在下一阶段被删除。
这是工作 1:
prep for build (other branches):
stage: Fetching tags
image: alpine
allow_failure: true
script:
- apk add git
- git --version
- LATEST_TAG=$(git describe --abbrev=0 --tags)
- echo $LATEST_TAG > tags.txt
- echo "Latest tag from other branches is:"
- cat tags.txt
only:
refs:
- master
- /^(([0-9]+)\.)?([0-9]+)\.x/
- rc
artifacts:
paths:
- tags.txt
expire_in: 4 week
tags:
- auth
- dev
- docker
这是使用作业 1 中的工件的作业 2:
execution (other branches):
stage: Build and Push Image
dependencies:
- prep for build (other branches)
image:
name: gcr.io/kaniko-project/executor:debug
entrypoint: [""]
script:
- LATEST_TAG=cat tags.txt
- echo "Latest tag is $LATEST_TAG"
- echo "{\"auths\":{\"$HARBOR_REGISTRY\":{\"username\":\"$HARBOR_USER\",\"password\":\"$HARBOR_PASSWORD\"}}}" > /kaniko/.docker/config.json
- /kaniko/executor --cache=true --context $CI_PROJECT_DIR --dockerfile $CI_PROJECT_DIR/Dockerfile --destination $HARBOR_REGISTRY_IMAGE:$LATEST_TAG
only:
refs:
- master
- /^(([0-9]+)\.)?([0-9]+)\.x/
- rc
tags:
- auth
- dev
- docker
这些是作业 2 的日志:
Checking out 21a67bd3 as master...
Removing CHANGELOG.md
Removing coverage-report/
Removing coverletReport/
Removing tags.txt
Skipping Git submodules setup
Downloading artifacts
00:01
Downloading artifacts for prep for build (other branches) (50868)...
Downloading artifacts from coordinator... ok id=50868 responseStatus=200 OK token=sADMXgpX
Executing "step_script" stage of the job script
.....
LATEST_TAG=cat tags.txt
/busybox/sh: eval: line 109: tags.txt: not found
Cleaning up file based variables
00:01
ERROR: Job failed: exit code 127
知道为什么会这样吗?
工件下载正常,问题是您设置变量的方式 LATEST_TAG
。您似乎试图将其设置为操作 cat tags.txt
的结果,但您却将其设置为字符串 cat
。 tags.txt
行的下一部分被解释为另一个命令,所以它说 tags.txt not found
因为它不能将 tags.txt
计算为命令。
将该行更改为 LATEST_TAG=$(cat tags.txt)
以将 LATEST_TAG
设置为 cat tags.txt
的结果。
有关 $(command)