jigfox
jigfox

Reputation: 18185

Gitlab feature branch deployment

We have successfully created a .gitlab-ci.yml which is able to deploy feature branches to different environments. We are also able to skip deployment with specific commit messages [skip deploy]

now we want to make the deployment a manual job on first run or if nothing is deployed yet, but if one there is already one deployment, the job should always run.

We tried the following settings, but it doesn't work.

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

Anybody with any ideas?

Upvotes: 0

Views: 896

Answers (1)

jigfox
jigfox

Reputation: 18185

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}

This is now working, only downside is: if you already use labels on your MRs you should use a more sophisticated script to update the labels instead of overwriting them.

Upvotes: 1

Related Questions