Test Yourself!

These ten questions turn on Jenkins 8,793 behaviors that catch experienced developers: strings where you expect numbers, results that are not what they seem, directives that quietly win, and a backup that restores without error yet locks everyone out. Every job ran on this chapter's controller, l2-jenkins (Jenkins 2.568.3, with agent-ssh carrying the label linux). Create each Pipeline job under the name in its first comment, paste the script into its Definition box, build it once, and write down what the console prints and why before you check Appendix H, which gives the real output, the reason and the section it comes from.

Questions 1-3: strings, results and step output (scripted job ty-a)Groovy
// ty-a: 1. Is the branch 'main'?  2. What do COUNT + 1 and DRY_RUN give?  3. What is the result?
node('linux') {
  def branch = sh(returnStdout: true, script: 'echo main')
  echo "1: ${branch == 'main'} ${branch.size()}"
  env.COUNT = 3
  env.DRY_RUN = false
  echo "2: ${env.COUNT + 1} ${env.DRY_RUN ? 'dry run' : 'real run'}"
  echo "3: ${currentBuild.result} ${currentBuild.currentResult}"
}
Question 4: environment against env (job ty-4)Groovy
// ty-4: 4. Which TARGET does each echo print?
pipeline {
  agent none
  environment { TARGET = 'staging' }
  stages {
    stage('Override') {
      steps {
        script { env.TARGET = 'production' }
        echo "4: ${env.TARGET}"
        withEnv(['TARGET=production']) { echo "4: ${env.TARGET}" }
      }
    }
  }
}
Questions 5-7: timeouts, failing tests and branch conditions (jobs ty-5, ty-6, ty-7)Groovy
// ty-5: 5. Which post blocks run, in what order, and what is the build's result?
pipeline {
  agent none
  options { timeout(time: 5, unit: 'SECONDS') }
  stages { stage('Slow') { steps { sleep 30 } } }
  post {
    failure { echo '5: failure' }
    aborted { echo '5: aborted' }
    always  { echo "5: ${currentBuild.currentResult}" }
  }
}
// ty-6: 6. One test fails. Does the failure block run? What is the result?
pipeline {
  agent { label 'linux' }
  stages {
    stage('Test') {
      steps {
        writeFile file: 'r.xml', text: '<testsuite name="api" tests="1" failures="1">' +
          '<testcase name="price"><failure message="400 !== 404"/></testcase></testsuite>'
        junit 'r.xml'
        echo '6: after junit'
      }
    }
  }
  post {
    failure      { echo '6: failure' }
    unsuccessful { echo "6: unsuccessful, ${currentBuild.currentResult}" }
  }
}
// ty-7: 7. A plain Pipeline job, not a multibranch branch. Does Deploy run?
pipeline {
  agent none
  stages {
    stage('Deploy') {
      when { branch 'main' }
      steps { echo '7: deploying main' }
    }
  }
}
Questions 8-9: Groovy scope and step context (scripted jobs ty-8, ty-9)Python
// ty-8: 8. Which of the two functions can see its variable?
count = 1
def total = 2
def showCount() { "count=${count}" }
def showTotal() { "total=${total}" }
echo "8: ${showCount()}"
echo "8: ${showTotal()}"
// ty-9: 9. What happens to this two-line Pipeline?
echo '9: writing'
writeFile file: 'notes.txt', text: 'BookNest'
Question 10: restoring the backup of Section 5.17.1 without secrets/Shell
cd ~/v5-ch5/backup && IMG=jenkins/jenkins:2.568.3-lts-jdk21
docker run --rm --entrypoint tar -v l2-ty10-home:/var/jenkins_home -v "$PWD":/backup:ro $IMG \
  -xzf /backup/jenkins-home-2026-09-26.tgz -C /var/jenkins_home --exclude=./secrets
docker run -d --name l2-ty10 -p 127.0.0.1:32084:8080 \
  -v l2-ty10-home:/var/jenkins_home $IMG >/dev/null
sleep 30   # 10. Can admin still use its API token? What does the log say about users?
curl -s -o /dev/null -w '%{http_code}\n' -u "admin:$JENKINS_TOKEN" http://localhost:32084/api/json
docker logs l2-ty10 2>&1 | grep -c 'ignoring /var/jenkins_home/users'
docker rm -f l2-ty10 >/dev/null && docker volume rm l2-ty10-home >/dev/null