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.
More on Write-a-pipeline tasks
Write a GitHub Actions workflow that runs tests on every PR and push to main.BeginnerWrite a workflow that builds a Docker image and pushes it to ECR using OIDC (no access keys).IntermediateWrite a matrix job that tests on Python 3.10, 3.11 and 3.12 on Linux and Windows.BeginnerWrite a declarative Jenkinsfile with build, test, Docker push and a manual approval before prod.AdvancedWrite a GitHub Actions deploy job that requires approval for production using environments.BeginnerWrite a reusable workflow and call it from another repo.Intermediate