如何在Gitlab CI脚本中删除符号链接的目标?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在Gitlab CI脚本中删除符号链接的目标?相关的知识,希望对你有一定的参考价值。

我在设置.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脚本中删除符号链接的目标?的主要内容,如果未能解决你的问题,请参考以下文章

如何在 gitlab ci 的构建脚本中使用 sudo?

GitLab CI/CD 脚本改进

我如何在 gitlab-ci 中同时运行循环?

如何将 exe 输出分配给 gitlab ci 脚本中的变量?

如何在 gitlab-ci build 中跳过前面的 maven 目标?

将脚本附加到一个阶段 .gitlab-ci.yml