Gitlab功能分支部署

时间:2020-01-08 14:22:59

标签: gitlab-ci

我们已经成功创建了.gitlab-ci.yml,它能够将要素分支部署到不同的环境。我们还可以跳过带有特定提交消息[skip deploy]

的部署

现在,我们希望在首次运行时将该部署设为manual作业,或者如果尚未部署任何部署,但是如果已经存在一个部署,则该作业应始终运行。

我们尝试了以下设置,但不起作用。

deploy:
  rules
    - if: $CI_MERGE_REQUEST_ID && $CI_COMMIT_MESSAGE =~ /\[skip\sdeploy\]/
      when: never
    - if: $CI_MERGE_REQUEST_ID
      exists:
        - already-deployed
      when: always
    - if: $CI_MERGE_REQUEST_ID
      when: manual
  script:
    - echo "already-deployed" > already-deployed
  cache:
    key: ${CI_COMMIT_REF_SLUG}
    paths:
      - already-deployed

有人有什么想法吗?

1 个答案:

答案 0 :(得分:0)

deploy:
  rules:
    # don't create job for [skip deploy]
    - if: $CI_MERGE_REQUEST_ID && $CI_COMMIT_MESSAGE =~ /\[skip\sdeploy\]/
      when: never
    # run always when it is a MR and label `deployed exists`
    - if: $CI_MERGE_REQUEST_ID && $CI_MERGE_REQUEST_LABELS =~ /\bdeployed\b/
      when: always
    # otherwise create a manual job for MRs
    - if: $CI_MERGE_REQUEST_ID
      when: manual
  after_script:
    - |
      curl \
      --request PUT \
      --header "Content-Type: application/json" \
      --header "Private-Token: ${GITLAB_TOKEN}" \
      --data '{"labels": "deployed"}' \
      https://${GITLAB_SERVER}/api/v4/projects/${CI_PROJECT_ID}/merge_requests/${CI_MERGE_REQUEST_IID}

现在可以正常工作,唯一的缺点是:如果您已经在MR上使用了标签,则应使用更复杂的脚本来更新标签,而不是覆盖标签。