birwin
birwin

Reputation: 2684

Gitlab CI/CD will not run my deploy stage

New to Gitlab CI/CD. My build job works wonderfully, but deploy job is never executed. I removed all rules and still it does not run.

Here is the contents of my .gitlab-ci.yml file:

build-job:
  stage: build
  script:
    - echo "STAGE - BUILD"
    - echo $CI_JOB_STAGE
    - echo $CI_COMMIT_MESSAGE
    - echo $CI_COMMIT_BRANCH
    - echo $CI_ENVIRONMENT_NAME
    - mkdir bin
    - mkdir obj
    - "dotnet build"


deploy-to-staging:
  stage: deploy
  script:
    - echo "STAGE - DEPLOY (STAGING)"

Any idea why Gitlab would skip the deploy stage? Do I have to explicitly define my stages? I tried that, but it made no difference (These lines were at the bottom of the yml file for a while):

stages:
  - build
  - deploy

Upvotes: 4

Views: 13797

Answers (2)

Mostafa Ghadimi
Mostafa Ghadimi

Reputation: 6736

As you can see in the official documentation of Gitlab CI, by defining stages, the sequence and the order of execution of jobs will be specified.

So the following gitlab-ci.yml should works:

stages: 
  - build
  - deploy


build-job:
  stage: build
  script:
    - echo "STAGE - BUILD"
    - echo $CI_JOB_STAGE
    - echo $CI_COMMIT_MESSAGE
    - echo $CI_COMMIT_BRANCH
    - echo $CI_ENVIRONMENT_NAME
    - mkdir bin
    - mkdir obj
    - "dotnet build"


deploy-to-staging:
  stage: deploy
  script:
    - echo "STAGE - DEPLOY (STAGING)"

Screenshot:

pipeline success screenshot

Upvotes: 2

Arty-chan
Arty-chan

Reputation: 2998

While it's not explicit in the stages documentation, you should generally set those at the top.

If you're getting a yaml invalid failure, then use the CI lint tool to double check your spacing and the like without having to run a pipeline.

Keep in mind that:

  1. If a job fails, it doesn't start the next stage.
  2. If you don't define stages, then it uses build, test, deploy.
  3. Any job that doesn't have a stage defined is assumed to be test.
  4. Any stage that isn't used should simply be hidden. However, the exact behaviour may depend on the version of GitLab you're on. (If memory serves correctly, this was changed, but I can't the merge request off hand.)

Upvotes: 6

Related Questions