【发布时间】:2021-07-07 14:34:50
【问题描述】:
在下面的gitlab-ci.yml 文件中,我使用我的项目的提交哈希作为其 docker 容器的标签。它应该执行以下操作:
- 服务器上的 ssh
- 将当前本地提交的哈希保存到名为
current的文件中 - 拉取新哈希并将其保存到名为
new的文件中 - 从现有备份 docker 容器的标签中获取哈希并将其保存到
old - 移除旧的 docker 容器
- 将最后一个当前的重新标记为新旧的
- 使用来自
new的最新哈希创建一个新容器
# Setup SSH deploy keys
before_script:
- apt-get update -qq
- apt-get install -qq git
- 'which ssh-agent || ( apt-get install -qq openssh-client )'
- eval $(ssh-agent -s)
- mkdir -p ~/.ssh
- echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null
- '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'
stages:
- update
- create
update_project:
stage: update
script:
- ssh $SSH_CONNECTION "cd /$PROJECT_NAME && git rev-parse --short HEAD > current && git checkout $BRANCH && git reset --hard && git pull origin $BRANCH && git rev-parse --short HEAD > new && exit"
create_docker_container:
stage: create
script:
- ssh $SSH_CONNECTION "cd /$PROJECT_NAME && docker images ${PROJECT_NAME}_old --format='{{ .Tag }}' > old && docker image rm ${PROJECT_NAME}_old:$(< old) && docker tag ${PROJECT_NAME}:$(< current) ${PROJECT_NAME}_old:$(< current) && docker image rm ${PROJECT_NAME}:$(< current) && docker build -t ${PROJECT_NAME}:$(< new) . && exit"
如您所见,例如。在${PROJECT_NAME}_old:$(< old) 中,我使用$(< x) 来访问整个yml 文件中的本地文件new、current 和old,这没有按预期工作。我的 gitlab-ci 失败了:
Running with gitlab-runner 12.9.0 (4c96e5ad)
on test abc123yz
Preparing the "docker" executor
Using Docker executor with image python:3.8.8-slim-buster ...
Pulling docker image python:3.8.8-slim-buster ...
Using docker image sha256:d97718c69437f6fcb5dd4588fc35ebfa2de7bfe536d0ce7d859d26f48e9ac840 for python:3.8.8-slim-buster ...
Preparing environment
Running on runner-yetZ9g1t-project-799-concurrent-0 via computer...
Getting source from Git repository
Fetching changes with git depth set to 50...
Reinitialized existing Git repository in /builds/user/myproject/.git/
Checking out 123abc45 as master...
Skipping Git submodules setup
Restoring cache
Downloading artifacts
Running before_script and script
...
$ ssh $SSH_CONNECTION "cd /$PROJECT_NAME && docker images ${PROJECT_NAME}_old --format='{{ .Tag }}' > old && docker image rm ${PROJECT_NAME}_old:$(< old) && docker tag ${PROJECT_NAME}:$(< current) ${PROJECT_NAME}_old:$(< current) && docker image rm ${PROJECT_NAME}:$(< current) && docker build -t ${PROJECT_NAME}:$(< new) . && exit"
/bin/bash: line 123: old: No such file or directory
/bin/bash: line 123: current: No such file or directory
/bin/bash: line 123: current: No such file or directory
/bin/bash: line 123: current: No such file or directory
/bin/bash: line 123: new: No such file or directory
Error response from daemon: invalid reference format
Running after_script
Uploading artifacts for failed job
ERROR: Job failed: exit code 1
我的 gitlab 跑步者似乎无法评估 $(< x)。如果我一一手动尝试完全相同的步骤,一切正常。难道我做错了什么?我还能在这里使用什么?
【问题讨论】:
-
$(< x)打开文件并将/dev/fd/<fd of x>作为参数传递,它不会跨ssh引用同一个文件。 -
请注意,这个问题与 Git 本身无关(尽管它与 bash 和 ssh 有很大关系,正如 @mkayaalp 所指出的,当然还有 gitlab 用于 yml)。