假设您的 Pipeline 已位于 Helm 图表所在的 Bitbucket 存储库中。以下是存储库的示例结构。
jenkins
├── app1-charts
│ ├── Chart.yaml
│ ├── templates
│ │ ├── app1.yml
│ │ └── _helpers.tpl
│ └── values.yaml
└── Jenkinsfile
The Jenkinsfile
在根目录中会有一个简单的管道,如下所示。
pipeline {
agent any
stages {
stage('Build1') {
steps {
echo "Doing some build here if you need"
}
}
stage('Applying helm charts') {
steps {
echo "Running Helm"
sh "helm upgrade --wait --timeout 60 --set image.tag=${SOME_INPUT} app1-name ./app1-charts"
}
}
}
}
上述管道假设您在 Jenkins 代理中设置了 Helm。现在在 Jenkins 中,您可以创建一个新的管道作业并选择该选项Pipeline Script from SCM
,然后提供 Bitbucket 配置。
确保Script Path
设置为指向您的Jenkinsfile
。另外,请确保Lightweight checkout
未选中。
添加配置后,这将克隆带有 Helm 图表的存储库,并在存储库中运行管道,该管道将执行 Helm 图表。
更新:检查 bitbucket 存储库的管道
检查以下管道示例。如果存储库是私有的,您可能必须生成具有必要权限的访问令牌才能签出存储库。
pipeline {
agent any
stages {
stage('checkout') {
steps {
echo 'Checking out code from bitbucket'
git(url: 'https://x-token-auth:[email protected] /cdn-cgi/l/email-protection/ORG_NAME/your-repo.git', branch: 'master')
sh """
cd your-helm-directory
helm install .............. Or whatever command you need
"""
}
}
}
}
Update
pipeline {
agent any
stages {
stage('Download Helm Charts') {
steps {
sh "echo 'Downloading Helm Charts from Bitbucket repository...'"
git(url: 'http://192.168.1.30:7990/scm/jen/helm.git', branch: 'master')
}
}
stage('Test Kubernetes version') {
steps {
sh "echo 'Checking Kubernetes version..'"
sh'''
kubectl version -o json // After getting the verson do whatever validations.
'''
}
}
stage('Push Helm Charts to Kubernetes') {
steps {
sh "echo 'building..'"
sh """
cd your-helm-directory
helm install .............. Or whatever command you need
"""
}
}
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
sh'''
git clone http://192.168.1.30:7990/scm/jen/spring-boot-microservice.git
cd TO_JAR_LOCATION
Java -jar some.jsr
#Assuming you have a docker file defined
docker build -t nexusurl/Imagename:5.0 .
'''
}
}
stage('Push Image into Nexus registry') {
steps {
sh "echo 'building..'"
sh'''
docker push nexusurl/Imagename:1.0
'''
// push compiled docker image into Nexus repository
}
}
stage('Deploy Image from Nexus registry into Kubernetes') {
steps {
sh "echo 'building..'"
# 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.
sh'''
helm install --set image.tag=5.0
'''
}
}
stage('Test'){
steps {
sh "echo 'Testing...'"
// This depends on how you have written your tests.
}
}
}
}
Update
这是如何列出图像的完整工作示例。
pipeline {
agent any
stages {
stage('Hello') {
steps {
echo 'Hello World'
script {
INPUT_IMAGE_TO_DEPLOY = input message: 'Please Select Docker Image to deploy', ok: 'Next',
parameters: [
choice(name: 'IMAGE_NAME', choices: getDockerImages(), description: 'Select Docker Image to delete')]
}
}
}
}
}
def getDockerImages() {
REPOSITORY = 'YOUR_REPO_NAME'
output = sh(returnStdout: true, script: "aws ecr list-images --repository-name $REPOSITORY --output json").trim()
json = readJSON text: output
println json
return json.imageIds.imageTag
}