简体   繁体   中英

How do use “changeset” in Jenkinsfile in dynamically generated steps?

I have a Jenkinsfile which as one of its stages builds several Docker images and pushes them to a registry. There is quite a long and growing list of these images, so I don't want to repetitively declare the build. Instead, I have a variable:

def dockerImages = ["myimage1","myimage2","myimage3"]

And then have the following stage:

stage("Initiate docker image builds") {
    steps {
        script {
            dockerImages.each { image ->
                stage ("${image}") {
                    utils.doStuff(${image}
                }
            }
        }
    }
}

I only want build to happen when there is a change, so I could do something like:

stage("Initiate docker image builds") {
    when{ 
          changeset "dockerfiles/**"
    }    
    steps {
        script {
            dockerImages.each { image ->
                stage ("${image}") {
                    utils.doStuff(${image}
                }
            }
        }
    }
}

But this would trigger building all the images if there was a change on just one of them. Is there a way that I could modify my script to have the when apply to the inner stage ("${image}" section? The syntax doesn't appear to allow when on that level.

You can inspect the Changeset to see what files have changed eg https://issues.jenkins.io/browse/JENKINS-58441

Looks quite messy code though.

Could also break the dockerfiles out to their own repos. It might seem "wasteful" having a repo for a single file, but it totally removes situations like this.

Or have separate Jenkinsfiles and jenkins jobs for each container build which just look it its Dockerfile has changed. Would need a lot of executors though if you had lots of containers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM