添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
大方的铁板烧  ·  Job Artifacts API | ...·  1 月前    · 
可爱的滑板  ·  sonarqube+gitlab -·  1 月前    · 
空虚的显示器  ·  Gitlab database ...·  2 周前    · 
有情有义的葡萄酒  ·  GitLab: suddenly I ...·  2 周前    · 
飞翔的鼠标垫  ·  Pipeline show 0 Tests ...·  2 周前    · 
读研的作业本  ·  [PDF] Differentiating ...·  10 月前    · 
仗义的火腿肠  ·  改变osg ...·  1 年前    · 

In GitLab CI/CD, you can use if-else conditions to control the execution of jobs based on certain conditions. The conditions are usually defined using variables and expressions. Here's an example of how you can use if-else conditions in a GitLab CI/CD pipeline:

stages:
  - test
  - deploy
variables:
  MY_VARIABLE: "some_value"
job1:
  stage: test
  script:
    - echo "Running tests"
  only:
    - master
job2:
  stage: deploy
  script:
    - echo "Deploying"
  only:
    - master
  when: manual
conditional_job:
  stage: deploy
  script:
    - echo "This job runs conditionally"
  only:
    - master
  when: manual
  rules:
    - exists:
        - $MY_VARIABLE
    - changes:
        - some_file.txt
# Using if-else condition
conditional_job_2:
  stage: deploy
  script:
    - echo "This job runs conditionally based on if-else"
  only:
    - master
  when: manual
  rules:
    - if: '$CI_COMMIT_BRANCH == "master"'
      exists:
        - $MY_VARIABLE
    - changes:
        - some_file.txt
    - changes:
        - another_file.txt
    - when: on_failure
      changes:
        - yet_another_file.txt

In this example:

  • job1 and job2 are standard jobs that run on the master branch. job2 is a manual job, which requires manual interaction to be executed.
  • conditional_job is a job that runs conditionally based on the existence of the variable $MY_VARIABLE and changes to certain files.
  • conditional_job_2 demonstrates an if-else condition. It runs conditionally based on the branch and whether specific changes occurred.

You can customize the conditions based on your specific requirements. The rules keyword allows you to define multiple conditions using logical operators ( and , or , exists , changes , etc.).

Please adjust the conditions, stages, and scripts according to your specific needs and the structure of your GitLab CI/CD pipeline.

Examples

  1. "GitLab CI if-else condition syntax"

    • Code Implementation:
      script:
        - if [ "$CI_COMMIT_BRANCH" == "main" ]; then
            echo "This is the main branch.";
            echo "This is not the main branch.";
      
    • Description: Uses a simple if-else condition to check if the current branch is "main" in a GitLab CI script.
  2. "GitLab CI if-else based on variable"

    • Code Implementation:
      script:
        - if [ "$CI_PIPELINE_SOURCE" == "web" ]; then
            echo "Pipeline triggered from the web interface.";
            echo "Pipeline triggered from another source.";
      
    • Description: Uses an if-else condition based on the value of a CI variable ( CI_PIPELINE_SOURCE in this example).
  3. "GitLab CI if-else based on job status"

    • Code Implementation:
      deploy:
        script:
          - echo "Deploying..."
        rules:
          - if: '$CI_COMMIT_TAG'
            when: on_success
          - when: never
      
    • Description: Uses if-else logic with rules to deploy only when a tag is present and the previous job was successful.
  4. "GitLab CI if-else based on file changes"

    • Code Implementation:
      script:
        - if [ -n "$(git diff --name-only $CI_COMMIT_BEFORE_SHA $CI_COMMIT_SHA)" ]; then
            echo "Files changed, triggering build.";
            echo "No file changes, skipping build.";
      
    • Description: Checks if there are file changes between commits using git diff and triggers the build accordingly.
  5. "GitLab CI if-else based on branch wildcard"

    • Code Implementation:
      script:
        - if [[ "$CI_COMMIT_BRANCH" == feature/* ]]; then
            echo "This is a feature branch.";
            echo "This is not a feature branch.";
      
    • Description: Uses a wildcard in the branch name to match feature branches in the if-else condition.
  6. "GitLab CI if-else based on environment"

    • Code Implementation:
      script:
        - if [ "$CI_ENVIRONMENT_NAME" == "production" ]; then
            echo "Deploying to production environment.";
            echo "Not deploying to production environment.";
      
    • Description: Uses the environment name in the if-else condition to control deployment.
  7. "GitLab CI if-else based on file existence"

    • Code Implementation:
      script:
        - if [ -f "config.yaml" ]; then
            echo "Config file exists.";
            echo "Config file not found.";
      
    • Description: Checks if a specific file exists using the -f flag in the if-else condition.
  8. "GitLab CI if-else based on manual job"

    • Code Implementation:
      deploy:
        script:
          - echo "Deploying..."
        rules:
          - if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH'
            when: manual
          - when: never
      
    • Description: Uses if-else logic with rules to deploy manually only for the default branch.
  9. "GitLab CI if-else based on variable existence"

    • Code Implementation:
      script:
        - if [ -n "$CUSTOM_VARIABLE" ]; then
            echo "Custom variable is set.";
            echo "Custom variable is not set.";
      
    • Description: Checks if a custom variable is set using the -n flag in the if-else condition.
  10. "GitLab CI if-else based on job status in before_script"

    • Code Implementation:
      before_script:
        - if [ "$CI_COMMIT_TAG" ]; then
            export BUILD_TYPE="Release";
            export BUILD_TYPE="Debug";
      script:
        - echo "Building in $BUILD_TYPE mode."
      
    • Description: Sets an environment variable in before_script based on the presence of a commit tag for use in the subsequent script.

More Tags

rank to-char django-models interface sqlexception convenience-methods valueerror browser-cache android-recyclerview cfeclipse

More Programming Questions