I am using Helm 3 and I have 3 different values.yaml per environments. If my initial release ends up in a failed state to avoid error while running helm upgrade --install myapp-dev
I would like to remove release if exists for related environment.
So according to the below script, if deploy stage runs successfully , then it will uninstall the release for next stage. What is the best practices to do this according to the in case of failure for previous stage?Should I use try/catch block or post section? Meanwhile, in which condition should I use this clean up really?
stage('Deploy to staging'){
when{
beforeAgent true
expression{return env.GIT_BRANCH == "origin/test"}
}
steps{
script{
def namespace = "test"
def ENV = "test"
sh " helm upgrade myapp-test my-chart --install -f values.${ENV}.yaml --namespace ${namespace}"
}
}
}
stage('Cleanup Stage'){
when{
beforeAgent true
expression{return env.GIT_BRANCH == "origin/test"}
}
steps{
script{
//Uninstall a release from the cluster
sh "helm uninstall myapp-test"
//See currently deployed releases
sh "helm list -all"
}
}
}
In Jenkinsfile you should actually do all the cleanup in the post
section, which can executed no matter what wrong happened. For example, in your case:
stages {
stage('Deploy to staging'){
when{
beforeAgent true
expression{return env.GIT_BRANCH == "origin/test"}
}
steps{
script{
def namespace = "test"
def ENV = "test"
sh " helm upgrade myapp-test my-chart --install -f values.${ENV}.yaml --
namespace ${namespace}"
}
}
}
}
post {
always {
helm uninstall myapp-test
}
}
That would guarantee to keep your environment clean no matter of the failures in between.