Home > Back-end >  Add image tag to kubernetes manifest with Jenkins
Add image tag to kubernetes manifest with Jenkins

Time:12-26

Currently, I choose what image to push to registry and i use a " complex " method to set the image tag to manifest before push the files to Git repos.

This is my code

stage("Push to Repo "){            
                steps {
                    script {
                    def filename = 'Path/to/file/deploy.yaml'
                    def data = readYaml file: filename
                    data.spec[0].template.spec.containers[0].image = "XXXXXXXXXXXXXXXXXXXXX:${PROJECT_VERSION}"
                    sh "rm $filename"
                    writeYaml file: filename, data: data
                    sh "sed -ie 's/- apiVersion/  apiVersion/g' Path/to/file/deploy.yaml "
                    sh "sed -i '/^          - c.*/a ---' Path/to/file/deploy.yaml "
                    sh ''' cd Path/to/file/
                        git add .
                        git commit -m "[0000] [update] update manifest to version: ${PROJECT_VERSION}  "
                        git push -u origin HEAD:branche_name '''
                }}}

I'am looking for a another way to parse the image tag directly to manifest.

Is there a Jenkins plugin to do that ?

CodePudding user response:

I use YQ tool to do this, it's an image used to edit yaml files.
Example (just docker run):

docker run --rm --user="root" 
-e TAG=dev-123456
-v "${PWD}":/workspace 
-w /workspace mikefarah/yq
eval '.spec.spec.containers.image.tag = strenv(TAG)' 
-i values.yaml

This replaces tag dev-123456 for the current tag in deployment.
I write on multiple lines to make it easier to see, you can write on one line if you want.
Link for details:
https://hub.docker.com/r/mikefarah/yq

  • Related