Bird
Raised Fist0
Jenkinsdevops~10 mins

Source code management setup in Jenkins - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to specify the Git repository URL in Jenkins pipeline.

Jenkins
pipeline {
  agent any
  stages {
    stage('Checkout') {
      steps {
        git url: '[1]'
      }
    }
  }
}
Drag options to blanks, or click blank then click option'
Ahttps://github.com/example/repo.git
Bgit clone https://github.com/example/repo.git
Crepo.git
Dhttps://github.com/example/repo
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'git clone' command inside the url parameter
Omitting the .git suffix in the URL
Using only the repository name without URL
2fill in blank
medium

Complete the code to checkout a specific branch named 'develop' in Jenkins pipeline.

Jenkins
pipeline {
  agent any
  stages {
    stage('Checkout') {
      steps {
        git branch: '[1]', url: 'https://github.com/example/repo.git'
      }
    }
  }
}
Drag options to blanks, or click blank then click option'
Amain
Bmaster
Cdevelop
Dfeature
Attempts:
3 left
💡 Hint
Common Mistakes
Using the default branch name 'main' instead of 'develop'
Using 'master' when the branch is 'develop'
Leaving branch parameter empty
3fill in blank
hard

Fix the error in the Jenkins pipeline code to properly checkout a Git repository with credentials.

Jenkins
pipeline {
  agent any
  stages {
    stage('Checkout') {
      steps {
        git url: 'https://github.com/example/repo.git', credentialsId: '[1]'
      }
    }
  }
}
Drag options to blanks, or click blank then click option'
Ahttps://github.com/credentials
Bgit-username
Cpassword123
Dmy-git-credentials
Attempts:
3 left
💡 Hint
Common Mistakes
Using username or password directly instead of credentials ID
Using a URL as credentials ID
Leaving credentialsId empty
4fill in blank
hard

Fill both blanks to configure a Jenkins pipeline to checkout a Git repository with a specific branch and credentials.

Jenkins
pipeline {
  agent any
  stages {
    stage('Checkout') {
      steps {
        git url: 'https://github.com/example/repo.git', branch: '[1]', credentialsId: '[2]'
      }
    }
  }
}
Drag options to blanks, or click blank then click option'
Afeature-branch
Bmain
Cmy-credentials-id
Duser-pass
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up branch name and credentials ID
Using username or password instead of credentials ID
Using default branch when a specific branch is needed
5fill in blank
hard

Fill all three blanks to create a Jenkins pipeline snippet that checks out a Git repository, uses a specific branch, and cleans the workspace before checkout.

Jenkins
pipeline {
  agent any
  stages {
    stage('Checkout') {
      steps {
        cleanWs()
        git url: '[1]', branch: '[2]', [3]
      }
    }
  }
}
Drag options to blanks, or click blank then click option'
Ahttps://github.com/example/repo.git
Bdevelop
CcredentialsId: 'git-creds'
Dsh 'rm -rf *'
Attempts:
3 left
💡 Hint
Common Mistakes
Using shell command to clean workspace instead of cleanWs()
Omitting credentialsId when authentication is needed
Using incomplete or incorrect repository URL

Practice

(1/5)
1. What is the main purpose of setting up source code management (SCM) in Jenkins?
easy
A. To automatically fetch the latest code from a repository
B. To manually upload code files to Jenkins
C. To create user accounts for Jenkins
D. To monitor server hardware status

Solution

  1. Step 1: Understand SCM role in Jenkins

    SCM connects Jenkins to your code repository to get the latest code automatically.
  2. Step 2: Compare options

    Only To automatically fetch the latest code from a repository describes automatic fetching of code, which is the main purpose of SCM setup.
  3. Final Answer:

    To automatically fetch the latest code from a repository -> Option A
  4. Quick Check:

    SCM = automatic code fetch [OK]
Hint: SCM means Jenkins pulls code automatically [OK]
Common Mistakes:
  • Thinking SCM uploads code manually
  • Confusing SCM with user management
  • Assuming SCM monitors hardware
2. Which Jenkins pipeline syntax correctly checks out code from a Git repository URL https://github.com/example/repo.git on branch main?
easy
A. git url: 'https://github.com/example/repo.git' branch: 'main'
B. checkout([$class: 'GitSCM', branches: [[name: 'main']], userRemoteConfigs: [[url: 'https://github.com/example/repo.git']]])
C. checkout scm: 'https://github.com/example/repo.git', branch: 'main'
D. gitCheckout('https://github.com/example/repo.git', 'main')

Solution

  1. Step 1: Recall Jenkins pipeline checkout syntax

    The correct syntax uses checkout with a map specifying $class: 'GitSCM', branches, and userRemoteConfigs.
  2. Step 2: Validate options

    checkout([$class: 'GitSCM', branches: [[name: 'main']], userRemoteConfigs: [[url: 'https://github.com/example/repo.git']]]) matches the correct syntax. Options B, C, and D are incorrect Jenkins pipeline syntax.
  3. Final Answer:

    checkout([$class: 'GitSCM', branches: [[name: 'main']], userRemoteConfigs: [[url: 'https://github.com/example/repo.git']]]) -> Option B
  4. Quick Check:

    Use checkout with GitSCM map [OK]
Hint: Use checkout with $class: 'GitSCM' for Git repos [OK]
Common Mistakes:
  • Using incorrect method names like gitCheckout
  • Missing $class: 'GitSCM' in checkout
  • Wrong parameter names or structure
3. Given this Jenkins pipeline snippet, what will be the output if the repository URL is invalid?
pipeline {
  agent any
  stages {
    stage('Checkout') {
      steps {
        checkout([$class: 'GitSCM', branches: [[name: 'main']], userRemoteConfigs: [[url: 'https://invalid-url/repo.git']]])
      }
    }
  }
}
medium
A. The pipeline will retry checkout indefinitely
B. The pipeline will succeed but skip checkout
C. The pipeline will fail with a checkout error
D. The pipeline will ignore the invalid URL and continue

Solution

  1. Step 1: Understand checkout behavior with invalid URL

    If the Git URL is invalid, Jenkins cannot fetch code and throws an error causing the pipeline to fail.
  2. Step 2: Analyze options

    Only The pipeline will fail with a checkout error correctly states the pipeline fails with a checkout error. Others describe incorrect behaviors.
  3. Final Answer:

    The pipeline will fail with a checkout error -> Option C
  4. Quick Check:

    Invalid URL causes checkout failure [OK]
Hint: Invalid repo URL causes pipeline failure [OK]
Common Mistakes:
  • Assuming pipeline skips checkout silently
  • Thinking Jenkins retries forever
  • Believing pipeline ignores errors
4. You wrote this Jenkins pipeline snippet to checkout code but it fails with a syntax error:
checkout([$class: 'GitSCM', branches: [name: 'main'], userRemoteConfigs: [[url: 'https://github.com/example/repo.git']]])
What is the error and how to fix it?
medium
A. Missing quotes around $class; fix to "$class: 'GitSCM'"
B. Wrong URL format; fix to 'git@github.com:example/repo.git'
C. Incorrect method name; fix to gitCheckout(...)
D. Missing double brackets around branches list; fix to branches: [[name: 'main']]

Solution

  1. Step 1: Identify syntax error in branches parameter

    The branches parameter expects a list of maps, so it must be double bracketed: [[name: 'main']].
  2. Step 2: Correct the branches syntax

    Change branches: [name: 'main'] to branches: [[name: 'main']] to fix the syntax error.
  3. Final Answer:

    Missing double brackets around branches list; fix to branches: [[name: 'main']] -> Option D
  4. Quick Check:

    Branches list needs double brackets [OK]
Hint: Branches must be a list of maps with double brackets [OK]
Common Mistakes:
  • Using single brackets for branches list
  • Changing URL format unnecessarily
  • Misquoting $class key
5. You want Jenkins to checkout code from a Git repository and only build when the develop branch changes. Which setup correctly achieves this in a declarative pipeline?
hard
A.
pipeline {
  agent any
  triggers {
    pollSCM('H/5 * * * *')
  }
  stages {
    stage('Checkout') {
      steps {
        git branch: 'develop', url: 'https://github.com/example/repo.git'
      }
    }
  }
}
B.
pipeline {
  agent any
  triggers {
    pollSCM('H/5 * * * *')
  }
  stages {
    stage('Checkout') {
      steps {
        checkout([$class: 'GitSCM', branches: [[name: 'main']], userRemoteConfigs: [[url: 'https://github.com/example/repo.git']]])
      }
    }
  }
}
C.
pipeline {
  agent any
  triggers {
    cron('H/5 * * * *')
  }
  stages {
    stage('Checkout') {
      steps {
        checkout([$class: 'GitSCM', branches: [[name: 'develop']], userRemoteConfigs: [[url: 'https://github.com/example/repo.git']]])
      }
    }
  }
}
D. >erp/<} } } } )]]]'tig.oper/elpmaxe/moc.buhtig//:sptth' :lru[[ :sgifnoCetomeRresu ,]]'niam' :eman[[ :sehcnarb ,'MCStiG' :ssalc$[(tuokcehc { spets { )'tuokcehC'(egats { segats } )'* * * * 5/H'(MCSllop { sreggirt yna tnega { enilepip>erp<

Solution

  1. Step 1: Understand trigger and checkout requirements

    To build only when the develop branch changes, use pollSCM trigger and checkout the develop branch.
  2. Step 2: Compare checkout methods

    pipeline {
      agent any
      triggers {
        pollSCM('H/5 * * * *')
      }
      stages {
        stage('Checkout') {
          steps {
            git branch: 'develop', url: 'https://github.com/example/repo.git'
          }
        }
      }
    }
    uses the simpler git step with branch and URL, which works well with pollSCM trigger to watch develop branch changes.
  3. Step 3: Identify why others are incorrect

    Options A and C use checkout with GitSCM but C uses cron trigger (not SCM polling).
    pipeline {
      agent any
      triggers {
        pollSCM('H/5 * * * *')
      }
      stages {
        stage('Checkout') {
          steps {
            checkout([$class: 'GitSCM', branches: [[name: 'main']], userRemoteConfigs: [[url: 'https://github.com/example/repo.git']]])
          }
        }
      }
    }
    checks out main branch, not develop.
  4. Final Answer:

    Use pollSCM trigger with git step for develop branch checkout -> Option A
  5. Quick Check:

    pollSCM + git step + develop branch = correct setup [OK]
Hint: Use pollSCM trigger and git step with branch name [OK]
Common Mistakes:
  • Using cron instead of pollSCM for SCM changes
  • Checking out wrong branch
  • Using complex checkout syntax unnecessarily
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard