【发布时间】:2020-05-03 07:27:27
【问题描述】:
我正在尝试使用 Helm 3 和 jenkins 部署 k8s 集群。 Jenkins 和 k8s 在不同的服务器上运行。我合并了 kubeconfig 文件,所有信息都在一个配置文件 ./kube 目录中。我想根据 GIT_BRANCH 值将我的应用程序部署到相关环境和命名空间。我对以下脚本有两个问题。
1.我应该存储 k8s 集群凭据并将在管道中使用的最佳方式是什么。我看到了一些插件,例如 Kubernetes CLI,但我不确定它是否能满足我的要求。如果我使用这个插件,我应该手动将 k8s 文件存储到 Jenkins 机器中还是这个插件已经通过上传配置文件来处理这个问题。
2.我应该更改以下脚本中的任何内容以遵循最佳实践吗?
stage('Deploy to dev'){
script{
steps{
if(env.GIT_BRANCH.contains("dev")){
def namespace="dev"
def ENV="development"
withCredentials([file(credentialsId: ...)]) {
// change context with related namespace
sh "kubectl config set-context $(kubectl config current-context) --namespace=${namespace}"
//Deploy with Helm
echo "Deploying"
sh "helm upgrade --install road-dashboard -f values.${ENV}.yaml --set tag=$TAG --namespace ${namespace}"
}
}
}
}
stage('Deploy to Test'){
script{
steps{
if(env.GIT_BRANCH.contains("test")){
def namespace="test"
def ENV="test"
withCredentials([file(credentialsId: ...)]) {
// change context with related namespace
sh "kubectl config set-context $(kubectl config current-context) --namespace=${namespace}"
//Deploy with Helm
echo "Deploying"
sh "helm upgrade --install road-dashboard -f values.${ENV}.yaml --set tag=$TAG --namespace ${namespace}"
}
}
}
}
}
stage ('Deploy to Production'){
when {
anyOf{
environment name: 'DEPLOY_TO_PROD' , value: 'true'
}
}
steps{
script{
DEPLOY_PROD = false
def namespace = "production"
withCredentials([file(credentialsId: 'kube-config', variable: 'kubecfg')]){
//Change context with related namespace
sh "kubectl config set-context $(kubectl config current-context) --namespace=${namespace}"
//Deploy with Helm
echo "Deploying to production"
sh "helm upgrade --install road-dashboard -f values.${ENV}.yaml --set tag=$TAG --namespace ${namespace}"
}
}
}
}
【问题讨论】:
标签: jenkins kubernetes jenkins-pipeline jenkins-plugins kubernetes-helm