Declarative Groovy Pipeline

 

-- A Sample Declarative Pipeline - Build, test and , build docker image , update GitOps repository



pipeline { agent any environment { DOCKER_REGISTRY = "your-docker-registry-url" GITOPS_REPO = "your-gitops-repo-url" GITOPS_BRANCH = "main" } stages { stage('Build and Test') { steps { // Checkout your source code from version control checkout scm // Run tests, build binaries, etc. sh 'make test' } } stage('Build Docker Image') { steps { script { def dockerImageTag = "test-${env.BUILD_NUMBER}" // Build Docker image sh "docker build -t ${DOCKER_REGISTRY}/your-image-name:${dockerImageTag} ." // Push Docker image to the registry sh "docker push ${DOCKER_REGISTRY}/your-image-name:${dockerImageTag}" // Save the image tag for deployment env.DOCKER_IMAGE_TAG = dockerImageTag } } } stage('Update GitOps Repository') { steps { script { // Clone the GitOps repository sh "git clone ${GITOPS_REPO} gitops-repo" dir("gitops-repo") { // Update the deployment file with the new image tag sh "sed -i 's#image: your-image-name:.*#image: ${DOCKER_REGISTRY}/your-image-name:${env.DOCKER_IMAGE_TAG}#' deployment.yaml" // Commit and push changes sh "git add deployment.yaml" sh "git commit -m 'Update deployment with new image tag'" sh "git push origin ${GITOPS_BRANCH}" } } } } } post { always { // Cleanup Docker images and containers sh 'docker system prune -af' } } }

Remember to replace placeholders like your-docker-registry-url, your-gitops-repo-url, your-image-name, and update the steps based on your build and deployment process. This pipeline assumes you have the necessary Docker and Git credentials configured in your Jenkins instance. Additionally, make sure to adjust error handling, security, and other considerations to meet your specific requirements.

Comments

Popular posts from this blog

JENKINS - Build In Variables