Skip to content
Tutorials Center
My learning

Topics

Lesson 36 of 36 ยท Projects and Practice

Practice: GitLab CI/CD Questions and Fixes

Test your GitLab knowledge with practice questions on rules, needs, variables, environments, registries and security, plus broken pipeline snippets to diagnose and repair.

Reading about GitLab is not the same as using it. This practice lesson gives you short exercises of the kind that appear in interviews and in daily work: predict what a pipeline does, find the bug in a snippet, and choose the safer setting. Try each one before reading the answer.

The snippets are read-only because GitLab cannot run on this site. Two runnable stand-ins at the end let you test your predictions: a tiny rules evaluator and a variable-expansion model. Both are simplified models for learning, not GitLab's engines. Topics refer back to this part of the course, for example Environments and Deployments, CI Variables and Secrets Security and Pipeline Efficiency Best Practices.

Part 1: Predict the result

Exercise 1. A merge request pipeline runs this:

YAML
test:
  script: ./run-tests.sh
  rules:
    - if: $CI_COMMIT_BRANCH == "main"

Does test run in a merge request pipeline? No. In merge request pipelines CI_COMMIT_BRANCH is not set (use CI_MERGE_REQUEST_SOURCE_BRANCH_NAME or check CI_PIPELINE_SOURCE == "merge_request_event"). The job is not created, and if no job matches the whole pipeline may not be created at all.

Exercise 2. Which job runs first?

YAML
stages: [build, test]

slow_build:
  stage: build
  script: sleep 600
quick_build:
  stage: build
  script: echo done
test_quick:
  stage: test
  needs: [quick_build]
  script: ./test.sh
test_all:
  stage: test
  script: ./test-all.sh

test_quick starts as soon as quick_build is done, because needs skips the stage wait. test_all has no needs, so it waits for both build jobs, including the ten-minute one.

Exercise 3. A variable TOKEN is Protected and Masked. A developer pushes the branch experiment (unprotected) and the job prints echo "token=$TOKEN". What does the log show?

token= followed by nothing: protected variables are not given to jobs on unprotected branches, so the variable is empty. Masking only matters when the value is present.

Exercise 4. A deploy job has environment: name: review/$CI_COMMIT_REF_SLUG and the branch is Feature/Add_Search. What is the environment name?

review/feature-add-search: the slug is lowercase and every character that is not a letter or number becomes a hyphen.

Exercise 5. Two pipelines for main start within a minute. Both have deploy_production with resource_group: production. What happens?

One runs and the other waits in the queue until the first finishes. Without the resource group both could deploy at the same time.

Exercise 6. Which of these image tags is the best choice for a production deployment: latest, main, 9fc1ab27, 1.4?

9fc1ab27 (a commit-based tag, immutable). 1.4 is a moving minor tag, main moves with every merge, latest hides the version.

Part 2: Find the bug

Bug A. The release job fails with "tag does not exist":

YAML
create_release:
  stage: build
  script: echo "release"
  release:
    tag_name: $CI_COMMIT_TAG
    name: "Release $CI_COMMIT_TAG"

Problem: no rules restrict the job to tag pipelines, so on a branch $CI_COMMIT_TAG is empty. It also sits in the build stage, before the files it should link exist. Fix: add rules: - if: $CI_COMMIT_TAG, move the job to a late stage, and use needs for the publish jobs.

Bug B. Packages sometimes fail to publish with "version already exists":

YAML
publish:
  script: npm publish
  rules:
    - if: $CI_COMMIT_BRANCH == "main"

Problem: the job runs on every merge to main, but the version in package.json only changes at release time, so the second publish of the same version is rejected. Fix: publish from tag pipelines only, and make the version come from the tag.

Bug C. A secret leaks:

YAML
deploy:
  script:
    - set -x
    - curl -H "Authorization: Bearer $API_TOKEN" https://api.example.com/deploy

Problem: set -x prints each command after expansion, so the token appears in the log, even if it is masked in some forms. Masking is a safety net only. Fix: remove set -x, use --header @file with a File variable or use ID tokens.

Bug D. The review environment stays after the merge:

YAML
deploy_review:
  environment:
    name: review/$CI_COMMIT_REF_SLUG
    on_stop: stop_review_app
stop_review:
  environment:
    name: review/$CI_COMMIT_REF_SLUG
    action: stop

Problem: on_stop names stop_review_app, but the job is called stop_review. Fix: the names must match exactly.

Bug E. The pipeline is slow even though needs is used:

YAML
test:
  needs: [build]
  script: npm ci && npm test

Problem: no cache, so npm ci downloads everything each time; and needs copies the build artifacts although the test does not use them. Fix: add a lock-file cache key and, if the artifacts are not needed, use needs: [{ job: build, artifacts: false }].

Part 3: Choose the safer option

  1. Where should the production database password live? A protected, environment-scoped, masked variable, or better a secret manager, not the repository and not an unprotected group variable.
  2. Developers need to see production logs only. Give them a role that grants no deploy right, and give access to logs through your logging system. Never add them as Maintainers for convenience.
  3. A job needs a cloud role for ten minutes. Use OIDC ID tokens instead of a stored key.
  4. A contractor needs to pull images. Create a deploy token with read_registry and an expiry date.
  5. The team wants all projects to run the security scans. A scan execution policy or a compliance pipeline, not a wiki page asking people to include the template.

Stand-in: a rules evaluator

Predict the outcome of each case before running. The evaluator supports if conditions of the form variable equals text, variable matches a regular expression, and when, with the first matching rule winning (the real engine supports more).

JS What will this rule list do?

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The wrong rule list blocks the job on every normal day, because != "true" is true then, which maps to never. Rule order and the direction of the test are classic mistakes.

Stand-in: variable expansion

Variables can contain other variables. This model expands $NAME and ${NAME} references, leaves unknown names empty (as the shell would) and stops at a depth limit.

JS Variable expansion

Press Run (or Ctrl + Enter) to see the console output here.

No output.

An empty expansion of a missing variable is a common source of silent bugs: docker push $IMAGE with an empty IMAGE fails with an odd message far from the cause.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. In a merge request pipeline, a rule if: $CI_COMMIT_BRANCH == 'main' ...

    Merge request pipelines have no CI_COMMIT_BRANCH; test CI_PIPELINE_SOURCE or the merge request variables instead.
  2. 2. A protected variable is used by a job on an unprotected branch. What is its value?

    GitLab does not pass protected variables to unprotected refs, so the job sees nothing.
  3. 3. on_stop names stop_review_app but the stop job is named stop_review. What happens?

    The link between the deploy job and the stop job is the exact job name.
  4. 4. Which is safer for a short-lived cloud credential?

    ID tokens remove the long-lived key, and the credentials expire within minutes.
  5. 5. Why is publishing a package on every merge to main risky?

    Versions are immutable; publish from tag pipelines whose version comes from the tag.

Summary

  • Many pipeline bugs come from rules (wrong variable, wrong direction, wrong place), mismatched names, and missing ordering between jobs.
  • Protected variables, masking and scopes work together; masking is the weakest layer.
  • Prefer immutable tags, tag-only publishing, short-lived credentials and policies that enforce what pages only recommend.
  • Practice by predicting first and then checking against a model or the real CI Lint.

Next: Back to the GitLab track