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

时间:2017-11-30 09:32:17

标签: continuous-integration gitlab gitlab-ci rm readlink

我在设置.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是提交的符号链接。

有没有人有类似的问题?或者您是否使用其他方法删除旧的(不再需要)提交?

1 个答案:

答案 0 :(得分:0)

终于找到了一个解决方案 - 这是一个糟糕的反引号“

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