1

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

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

4

1 回答 1

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
于 2018-03-01T10:46:03.337 回答