如何在 Gitlab CI 脚本中删除符号 link 的目标?
How to delete target of symbolic link in Gitlab CI script?
我在设置 .gitlab-ci.yml
文件时遇到问题。我想删除旧的提交,所以我在 (Debian) 服务器上只有最新的和以前的提交可用。
出于某种原因,当脚本被执行时,它没有按预期运行 - 提交被正确切换,但旧的仍然存在于文件夹中并且没有被删除 rm -rf $(readlink <link>)
但是,如果我使用同一用户在服务器上使用 SSH 并执行完全相同的步骤,rm -rf $(readlink <link>)
会成功删除实际的符号链接目标(预期行为)。当从 CI 脚本完成同样的事情时,目标不会被删除。
也许我错过了一些变量转义或括号?
这是我的(修剪过的).gitlab-ci.yml
文件:
variables:
SSH_USER: "user@server"
PROJECT_DIR: "/domains/example.com/project-name"
DEPLOY_DIR: "${PROJECT_DIR}/deploy"
STAGING_TEMP_DIR: "${PROJECT_DIR}/staging-temp"
# is this correct?
DELETE_STAGING_TEMP: "'$$(readlink -f ${STAGING_TEMP_DIR})'"
stages:
- build
- deploy
- post-deploy
Post Deploy staging:
stage: post-deploy
script:
# switch last version
- ssh $SSH_USER mv ${PROJECT_DIR}/staging-previous ${STAGING_TEMP_DIR} || true
- ssh $SSH_USER mv ${PROJECT_DIR}/staging-latest ${PROJECT_DIR}/staging-previous || true
- ssh $SSH_USER rm -rf ${DELETE_STAGING_TEMP}
- ssh $SSH_USER unlink ${STAGING_TEMP_DIR} || true
- ssh $SSH_USER ln -s ${DEPLOY_DIR}/${CI_COMMIT_REF_NAME}/${CI_COMMIT_SHA} ${PROJECT_DIR}/staging-latest
我也试过这个变体:
variables:
SSH_USER: "user@server"
PROJECT_DIR: "/domains/example.com/project-name"
DEPLOY_DIR: "${PROJECT_DIR}/deploy"
STAGING_TEMP_DIR: "${PROJECT_DIR}/staging-temp"
Post Deploy staging:
stage: post-deploy
script:
# switch last version
- ssh $SSH_USER mv ${PROJECT_DIR}/staging-previous ${STAGING_TEMP_DIR} || true
- ssh $SSH_USER mv ${PROJECT_DIR}/staging-latest ${PROJECT_DIR}/staging-previous || true
# is this correct?
- ssh $SSH_USER rm -rf "$(readlink -f ${STAGING_TEMP_DIR})"
- ssh $SSH_USER unlink ${STAGING_TEMP_DIR} || true
- ssh $SSH_USER ln -s ${DEPLOY_DIR}/${CI_COMMIT_REF_NAME}/${CI_COMMIT_SHA} ${PROJECT_DIR}/staging-latest
请注意:
${PROJECT_DIR}/staging-previous
和 ${PROJECT_DIR}/staging-latest
是提交的符号链接。
有没有人有类似的问题?或者您是否使用其他方法删除旧的(不再需要的)提交?
终于找到了解决方案——转义反引号很糟糕`
Post Deploy STAGING:
stage: post-deploy
script:
# checks if current commit is already set up as staging-latest
# if not, then this is new release, so switch last version:
# - move staging-previous to temp
# - move staging-latest to staging-previous
# - delete the content of temp dir and unlink temp
- >
ssh $SSH_USER "if [ \"\`readlink ${PROJECT_DIR}/staging-latest\`\" != \"${DEPLOY_DIR}/${CI_COMMIT_REF_NAME}/${CI_COMMIT_SHA}\" ];
then \`mv ${PROJECT_DIR}/staging-previous ${STAGING_TEMP_DIR} || true;
mv ${PROJECT_DIR}/staging-latest ${PROJECT_DIR}/staging-previous || true\`;
rm -rf \`readlink -f ${STAGING_TEMP_DIR}\`;
unlink ${STAGING_TEMP_DIR} || true;
fi;"
- ssh $SSH_USER ln -s ${DEPLOY_DIR}/${CI_COMMIT_REF_NAME}/${CI_COMMIT_SHA} ${PROJECT_DIR}/staging-latest || true
我在设置 .gitlab-ci.yml
文件时遇到问题。我想删除旧的提交,所以我在 (Debian) 服务器上只有最新的和以前的提交可用。
出于某种原因,当脚本被执行时,它没有按预期运行 - 提交被正确切换,但旧的仍然存在于文件夹中并且没有被删除 rm -rf $(readlink <link>)
但是,如果我使用同一用户在服务器上使用 SSH 并执行完全相同的步骤,rm -rf $(readlink <link>)
会成功删除实际的符号链接目标(预期行为)。当从 CI 脚本完成同样的事情时,目标不会被删除。
也许我错过了一些变量转义或括号?
这是我的(修剪过的).gitlab-ci.yml
文件:
variables:
SSH_USER: "user@server"
PROJECT_DIR: "/domains/example.com/project-name"
DEPLOY_DIR: "${PROJECT_DIR}/deploy"
STAGING_TEMP_DIR: "${PROJECT_DIR}/staging-temp"
# is this correct?
DELETE_STAGING_TEMP: "'$$(readlink -f ${STAGING_TEMP_DIR})'"
stages:
- build
- deploy
- post-deploy
Post Deploy staging:
stage: post-deploy
script:
# switch last version
- ssh $SSH_USER mv ${PROJECT_DIR}/staging-previous ${STAGING_TEMP_DIR} || true
- ssh $SSH_USER mv ${PROJECT_DIR}/staging-latest ${PROJECT_DIR}/staging-previous || true
- ssh $SSH_USER rm -rf ${DELETE_STAGING_TEMP}
- ssh $SSH_USER unlink ${STAGING_TEMP_DIR} || true
- ssh $SSH_USER ln -s ${DEPLOY_DIR}/${CI_COMMIT_REF_NAME}/${CI_COMMIT_SHA} ${PROJECT_DIR}/staging-latest
我也试过这个变体:
variables:
SSH_USER: "user@server"
PROJECT_DIR: "/domains/example.com/project-name"
DEPLOY_DIR: "${PROJECT_DIR}/deploy"
STAGING_TEMP_DIR: "${PROJECT_DIR}/staging-temp"
Post Deploy staging:
stage: post-deploy
script:
# switch last version
- ssh $SSH_USER mv ${PROJECT_DIR}/staging-previous ${STAGING_TEMP_DIR} || true
- ssh $SSH_USER mv ${PROJECT_DIR}/staging-latest ${PROJECT_DIR}/staging-previous || true
# is this correct?
- ssh $SSH_USER rm -rf "$(readlink -f ${STAGING_TEMP_DIR})"
- ssh $SSH_USER unlink ${STAGING_TEMP_DIR} || true
- ssh $SSH_USER ln -s ${DEPLOY_DIR}/${CI_COMMIT_REF_NAME}/${CI_COMMIT_SHA} ${PROJECT_DIR}/staging-latest
请注意:
${PROJECT_DIR}/staging-previous
和 ${PROJECT_DIR}/staging-latest
是提交的符号链接。
有没有人有类似的问题?或者您是否使用其他方法删除旧的(不再需要的)提交?
终于找到了解决方案——转义反引号很糟糕`
Post Deploy STAGING:
stage: post-deploy
script:
# checks if current commit is already set up as staging-latest
# if not, then this is new release, so switch last version:
# - move staging-previous to temp
# - move staging-latest to staging-previous
# - delete the content of temp dir and unlink temp
- >
ssh $SSH_USER "if [ \"\`readlink ${PROJECT_DIR}/staging-latest\`\" != \"${DEPLOY_DIR}/${CI_COMMIT_REF_NAME}/${CI_COMMIT_SHA}\" ];
then \`mv ${PROJECT_DIR}/staging-previous ${STAGING_TEMP_DIR} || true;
mv ${PROJECT_DIR}/staging-latest ${PROJECT_DIR}/staging-previous || true\`;
rm -rf \`readlink -f ${STAGING_TEMP_DIR}\`;
unlink ${STAGING_TEMP_DIR} || true;
fi;"
- ssh $SSH_USER ln -s ${DEPLOY_DIR}/${CI_COMMIT_REF_NAME}/${CI_COMMIT_SHA} ${PROJECT_DIR}/staging-latest || true