This document describes how to use Continuous Integration to release a project to a K8s cluster.
Before configuring the CODING Continuous Integration (CODING-CI) build environment, you must activate the CODING DevOps service for your Tencent Cloud account.
Follow the steps below to easily deploy a project to a K8s cluster through Continuous Integration:
pipeline {
agent any
stages {
stage('Check out') {
steps {
checkout([$class: 'GitSCM', branches: [[name: env.GIT_BUILD_REF]],
userRemoteConfigs: [[url: env.GIT_REPO_URL, credentialsId: env.CREDENTIALS_ID]]])
}
}
stage('Build') {
steps {
echo 'building...'
script {
// Modify dockerServer, dockerPath, and imageName
dockerServer = 'codes-farm-docker.pkg.coding.net'
dockerPath = '/laravel-demo/laravel-docker'
imageName = "${dockerServer}${dockerPath}/laravel-demo:1.0.0"
def customImage = docker.build(imageName)
// Push Docker image to repository
docker.withRegistry("https://${dockerServer}", CODING_ARTIFACTS_CREDENTIALS_ID) {
customImage.push()
}
}
}
}
stage('Deploy to K8s') {
steps {
echo 'deploying...'
script {
// Modify credentialsId: Fill in the K8s credential ID
withKubeConfig([credentialsId: 'f23cc59c-dfd1-40b9-a12f-2c9b6909e908']) {
// Use kubectl to create a K8s secret key: originates from the environment variables DOCKER_USER and DOCKER_PASSWORD
sh(script: "kubectl create secret docker-registry coding --docker-server=${dockerServer} --docker-username=${env.DOCKER_USER} --docker-password=${env.DOCKER_PASSWORD} --docker-email=support@coding.net", returnStatus: true)
// Use kubectl to modify the K8s deployment: Specify the Docker image link and secret key
// Modify laravel-demo and web to the values in your deployment
sh "kubectl patch deployment laravel-demo --patch '{\"spec\": {\"template\": {\"spec\": {\"containers\": [{\"name\": \"web\", \"image\": \"${imageName}\"}], \"imagePullSecrets\": [{\"name\": \"coding\"}]}}}}'"
}
}
}
}
}
}
As a K8s deployment includes at least five steps, we recommend you to use Continuous Deployment instead of writing all of them in Continuous Integration to facilitate future maintenance.
Was this page helpful?