使用 Jenkins 作业将 Helm 图表部署到 Kubernetes

Pet*_*zov 5 jenkins jenkins-groovy jenkins-pipeline

我想创建一个 Jenkins 作业,将 Helm Chart 部署到 Kubernetes 集群中。Helm 图表存储在 Bitbucket 存储库中。

    pipeline {
        agent any
        stages {
            stage('Download Helm Charts') {
                steps {
                    sh "echo 'Downloading Helm Charts from Bitbucket repository...'"
                    git checkout http://192.168.1.30:7990/scm/jen/helm.git
                    // not sure do I need ot point the root folder of the Helm repository or only the single chart
                }
            }
            stage('Test Kubernetes version') {
                steps {
                    sh "echo 'Checking Kubernetes version..'"
                    // How to do remote test of kubernetes version
                }
            }
            stage('Push Helm Charts to Kubernetes') {
                steps {
                    sh "echo 'building..'"
                    // push here helm chart from Jenkins server to Kubernetes cluster
                }
            }     
            stage('Build Image') {
                steps {
                    sh "echo 'building..'"
                    git checkout http://192.168.1.30:7990/scm/jen/spring-boot-microservice.git
                    // execute Java -jar ... and build docker image
                }
            }
            stage('Push Image into Nexus registry') {
                steps {
                    sh "echo 'building..'"
                    // push compiled docker image into Nexus repository
                }
            }
            stage('Deploy Image from Nexus registry into Kubernetes') {
                steps {
                    sh "echo 'building..'"
                }
            }
            stage('Test'){
                steps {
                    sh "echo 'Testing...'"
                    // implement a check here is it deployed sucessfully
                }
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

我需要在这个 Jenkins 文件中添加什么配置才能从 bitbucket 下载 Heml 图表存储库并将配置应用到 Kubernetes 集群中?你能给我一个这样的 Jenkins 文件的例子吗?

ycr*_*ycr 1

假设您的 Pipeline 已位于 Helm 图表所在的 Bitbucket 存储库中。以下是存储库的示例结构。

\n
jenkins\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 app1-charts\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 Chart.yaml\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 templates\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 app1.yml\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 _helpers.tpl\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 values.yaml\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 Jenkinsfile\n
Run Code Online (Sandbox Code Playgroud)\n

Jenkinsfile目录中将有一个简单的管道,如下所示。

\n
jenkins\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 app1-charts\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 Chart.yaml\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 templates\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 app1.yml\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 _helpers.tpl\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 values.yaml\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 Jenkinsfile\n
Run Code Online (Sandbox Code Playgroud)\n

上述管道假设您在 Jenkins 代理中设置了 Helm。现在在 Jenkins 中,您可以创建一个新管道作业并选择选项Pipeline Script from SCM,然后提供 Bitbucket 配置。

\n

詹金斯管道

\n

确保Script Path设置为指向您的Jenkinsfile. 另外,请确保Lightweight checkout未选中。

\n

添加配置后,这将克隆带有 Helm 图表的存储库,并在存储库中运行管道,该管道将执行 Helm 图表。

\n

更新:检查 bitbucket 存储库的管道

\n

检查以下管道示例。如果存储库是私有的,您可能必须生成具有必要权限的访问令牌才能签出存储库。

\n
pipeline {\n    agent any\n    stages {\n        stage(\'Build1\') {\n            steps { \n                echo "Doing some build here if you need"\n            }\n        }\n        \n        stage(\'Applying helm charts\') {\n            steps {\n                echo "Running Helm"\n                sh "helm upgrade --wait --timeout 60 --set image.tag=${SOME_INPUT} app1-name ./app1-charts"\n            }\n        }\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

更新

\n
pipeline {\n    agent any\n\n    stages {\n        stage(\'checkout\') {\n            steps {\n                echo \'Checking out code from bitbucket\'\n                git(url: \'https://x-token-auth:REPO_ACCESS_TOKEN@bitbucket.org/ORG_NAME/your-repo.git\', branch: \'master\')\n                sh """\n                cd your-helm-directory\n                helm install .............. Or whatever command you need\n                """\n            }\n        }\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

更新

\n

这是如何列出图像的完整工作示例。

\n
pipeline {\n    agent any\n    stages {\n        stage(\'Download Helm Charts\') {\n            steps {\n                sh "echo \'Downloading Helm Charts from Bitbucket repository...\'"\n                git(url: \'http://192.168.1.30:7990/scm/jen/helm.git\', branch: \'master\')\n                \n            }\n        }\n        stage(\'Test Kubernetes version\') {\n            steps {\n                sh "echo \'Checking Kubernetes version..\'"\n                sh\'\'\'\n                  kubectl version -o json // After getting the verson do whatever validations. \n                \'\'\'\n            }\n        }\n        stage(\'Push Helm Charts to Kubernetes\') {\n            steps {\n                sh "echo \'building..\'"\n                sh """\n                  cd your-helm-directory\n                  helm install .............. Or whatever command you need\n                """\n            }\n        }     \n        stage(\'Build Image\') {\n            steps {\n                sh "echo \'building..\'"\n                git checkout http://192.168.1.30:7990/scm/jen/spring-boot-microservice.git\n                // execute Java -jar ... and build docker image\n                sh\'\'\'\n                 git clone http://192.168.1.30:7990/scm/jen/spring-boot-microservice.git\n                  cd TO_JAR_LOCATION \n                  Java -jar some.jsr\n                  #Assuming you have a docker file defined\n                  docker build -t nexusurl/Imagename:5.0 .\n                \'\'\'\n            }\n        }\n        stage(\'Push Image into Nexus registry\') {\n            steps {\n                sh "echo \'building..\'"\n                sh\'\'\'\n                  docker push nexusurl/Imagename:1.0\n                \'\'\'\n                // push compiled docker image into Nexus repository\n            }\n        }\n        stage(\'Deploy Image from Nexus registry into Kubernetes\') {\n            steps {\n                sh "echo \'building..\'"\n                # This again depends on how you plan to update. Assuming you are using the same helm charts then you can override what ever imagetag in your values file and run. Or else update the values file and then run. \n                sh\'\'\'\n                    helm install --set image.tag=5.0\n                \'\'\'\n            }\n        }\n        stage(\'Test\'){\n            steps {\n                sh "echo \'Testing...\'"\n                // This depends on how you have written your tests. \n            }\n        }\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

在此输入图像描述

\n