Interview Prep/Real-World Interview/Write-a-pipeline tasks

Write a Jenkins shared library step `deployApp(env, tag)` and use it.

AdvancedHands-onWrite-a-pipeline tasks
Answer
// vars/deployApp.groovy in the shared library repo
def call(String envName, String tag) {
  stage("Deploy ${envName}") {
    withCredentials([file(credentialsId: "kubeconfig-${envName}", variable: 'KUBECONFIG')]) {
      sh """
        helm upgrade --install app ./chart \
          --namespace ${envName} --set image.tag=${tag} \
          --wait --timeout 5m --atomic
      """
    }
  }
}
// Jenkinsfile
@Library('platform-lib@v2') _
pipeline {
  agent any
  stages {
    stage('Deploy') { steps { script { deployApp('staging', env.GIT_COMMIT.take(7)) } } }
  }
}

Key trick → Files in vars/ become global steps. Pin the library with @v2. helm --atomic rolls back automatically if the release doesn't become ready.

All Real-World Interview questions Roadmap: Interview readiness

More on Write-a-pipeline tasks