Middle
How to set up a job trigger in GitLab?
sobes.tech AI
Answer from AI
To set up a manual job trigger in GitLab CI/CD, the keyword when: manual is used in the job description in the .gitlab-ci.yml file.
Example:
stages:
- build
- deploy
build_job:
stage: build
script:
- echo "Building the project..."
# Build commands
deploy_staging:
stage: deploy
script:
- echo "Deploying to staging..."
# Deployment commands
when: manual # This job needs to be triggered manually
allow_failure: false # Default is false, but explicitly set for clarity
Explanation:
when: manual: Indicates to GitLab that this job will not run automatically on code change (commit) or on schedule. It will wait for manual initiation by the user through the GitLab interface.allow_failure: false: Determines whether subsequent jobs (if any) should run if this manual job fails. If set tofalse(default), the pipeline stops if this job fails. If set totrue, the pipeline continues even if this job fails.
After adding and committing such a .gitlab-ci.yml file, a "Run" button or its status will appear next to the deploy_staging job in the GitLab pipelines interface (CI/CD -> Pipelines). Clicking this button triggers the job.
Manual jobs can also be included in stages that automatically trigger other jobs. For example:
stages:
- build
- test
- deploy
build_job:
stage: build
script:
- echo "Building..."
test_job:
stage: test
needs: [build_job]
script:
- echo "Testing..."
deploy_production:
stage: deploy
needs: [test_job]
script:
- echo "Deploying to production..."
when: manual # Only after successful testing, and then manually
variables:
TARGET_ENVIRONMENT: "production" # Example: passing variables for manual job
Here, the deploy_production job will only be available for manual execution after the successful completion of the test_job.
Additionally:
variables: Manual jobs can use variables defined in the.gitlab-ci.ymlfile or in project/group settings.rules: More complex trigger scenarios can use theruleskeyword along withwhen: manualto define conditions under which the manual job becomes available or mandatory.- API: Manual jobs can also be triggered via the GitLab CI/CD API.
Thus, when: manual is the main mechanism for implementing manual job triggers in GitLab.