Search code examples
gitlabgitlab-ci

Are multiple jobs on the same environment possible in Gitlab CI?


I'm setting up my self-hosted gitlab CI pipeline, and the following simple CI script fails on release bar step with the following error message:

The deployment job is older than the previously succeeded deployment job, and therefore cannot be run

It seems that gitlab does not allow multiple jobs to run on the same environment. Is there any way I can achieve this? I could merge all release jobs into a single one, but this seems a bit limiting, making all my steps run serially instead of in parallel. Is there any reason this is not possible?

Below is a gitlab-ci exemplifying the problem:

stages:
  - release

workflow:
  rules:
    - if: $CI_COMMIT_BRANCH == "dev"
      variables:
        STAGE: dev
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
      variables:
        STAGE: prod

release foo:
  stage: release
  tags: [shell]
  environment: $STAGE
  script:
    - echo $STAGE
    - echo "Releasing foo..."

release bar:
  stage: release
  tags: [shell]
  environment: $STAGE
  script:
    - echo $STAGE
    - echo "Releasing bar..."

I also thought of renaming the environment per job, but this also doesn't work because I have some specific environment variables that are only visible on Gitlab's environments dev and prod.


Solution

  • There might be a few options:

    Disable the deployment safety feature

    You can disable the prevent outdated deployment jobs setting which will allow your jobs to run in the scenario without any other configuration changes. Just be aware that you can potentially run into the issue this setting is meant to prevent (as described in the linked documentation).

    Use different environments

    You can set each job to deploy to a differently named environment, like $STAGE/foo or $STAGE/bar.

    I have some specific environment variables that are only visible on Gitlab's environments dev and prod.

    To get around this issue, you can change the protected environment variable rule to a wildcard, like prod/* or dev/*.

    wildcard rule