3

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
birwin
  • 2,524
  • 2
  • 21
  • 41
  • 1
    Stages should be at the top. Did you run it through the CI linter? Did the build job pass? – Arty-chan Oct 11 '21 at 16:25
  • Thanks for your comment. It is much appreciated... Yes, the build stage always succeeds, but just stops afterward. My yaml file does pass the linter. What's odd is when I place the stages section at the top of the file, the pipeline fails with a `yaml invalid` message. According to the docs, if the stages section is missing, it uses default stages (.pre, build, test, deploy, .post)... I am wondering if I have to have something in test for the deploy to execute. – birwin Oct 11 '21 at 16:38
  • I just added a test-job for stage test whose script just echoed text to output. Now all three stages ran. I did not know you had to provide a job for every job defined... In my case, since I did not define stages, I had to provide a job for every default stage (except .pre and .post) – birwin Oct 11 '21 at 16:58
  • 1
    the way around it would be to define the stages at the top. If the linter is complaining, double check your spacing, etc. You also have access to the CI linter itself so you can check that it's valid before running a pipeline – Arty-chan Oct 11 '21 at 17:25

2 Answers2

6

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.)
Arty-chan
  • 2,572
  • 1
  • 19
  • 25
  • Here's what's interesting... If I set up my stages at the top of the file, the lint succeeds, but when I check the code in, the pipeline fails with a lint error. I have to comment the stages section for the pipeline to execute. In addition, I have to provide a job for each stage, otherwise only the build stage executes. I have verified there are no extra spaces in the file around the stages section. In fact, I move the stages section to the bottom of the file and the linter still says it is valid and the pipeline executes and succeeds. ???? I have no idea why! – birwin Oct 11 '21 at 18:01
  • If you can reproduce it in a public project and your GitLab is on the latest version, then I recommend filing a bug: https://gitlab.com/gitlab-org/gitlab/-/issues/new?issuable_template=bug – Arty-chan Oct 15 '21 at 18:05
0

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

Mostafa Ghadimi
  • 5,883
  • 8
  • 64
  • 102