1

I want to be able to have a pipeline that deploys a main branch to a test environment and then a versioned release to production.

The versioned prod env will only be have a newer versioned deployed once testing has been signed off.

I want to have a single pipeline that does this, that I can run on a schedule so that prod is always up to date with certificates etc.

I have tried to do something like the following, but this only runs the deploy:test and not the deploy:prod

deploy:test:
  image: alpine:3.18
  stage: release
  script:
    - echo "this is the $CI_DEFAULT_BRANCH"
    - cat version.txt
  rules:
    - if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH'
      when: always

deploy:prod:
  image: alpine:3.18
  stage: release
  script:
    - cat version.txt
  rules:
    - if: $CI_COMMIT_TAG == "v11.0.0"
      when: always

Is something like this possible with gitlab ci?

danrhjones
  • 17
  • 7

1 Answers1

1

CI_COMMIT_TAG and CI_COMMIT_BRANCH have mutually exclusive availability in a pipeline. IF CI_COMMIT_TAG exists, then it is impossible for CI_COMMIT_BRANCH to exist in the same pipeline and vice versa. This is because tags do not belong to branches, which is why tag pipelines are separate from branch pipelines.

So, you cannot have only a "single pipeline" how you have written this CI definition.

However, you can have a pipeline that will create the tags/release using the release: keyword (or use release-cli or call the API in a script). Then it will trigger a second pipeline for the tag that was created where the rule based on CI_COMMIT_TAG can be matched. You will have two pipelines, but the effect is similar.


# set a variable `THIS_VERSION` for all other jobs in the pipeline
# based on the contents of `version.txt`
set-version:
  stage: .pre
  script:
    - version="$(cat version.txt)"  # e.g. "v1.0.0"
    - echo "THIS_VERSION=${version}" > version.env
  artifacts:
    reports:
      dotenv: version.env

deploy:test:
  stage: release
  # ...

# run this job to promote to production
# creates a new tag pipeline, which will trigger the `deploy:prod:` job
make_release:
  stage: release
  script:
    - echo "creating release for version $THIS_VERSION"
  rules:
    - if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH'
  when: manual
  allow_failure: false
  release: # dynamically create the release and tag
    tag_name: $THIS_VERSION
    name: 'Release $THIS_VERSION'
    description: 'Release of $THIS_VERSION'

deploy:prod:
  image: alpine:3.18
  stage: release
  script:
    - echo "version is $CI_COMMIT_TAG"
  rules:
    - if: $CI_COMMIT_TAG =~ /v\d+\.\d+\.\d+/
sytech
  • 29,298
  • 3
  • 45
  • 86