Sebastian Mendez
Sebastian Mendez

Reputation: 2981

Jenkins create single job for building many repos

I have quite a few repositories with a very similar structure. They all contain a Dockerfile which builds an image based on the contents of the repository. My end goal is so that whenever any of the repositories is pushed to, the Dockerfile is automatically rebuilt and published to a private registry. The command that is needed to build the image is essentially docker build -t $REPO_NAME .

Is there a way in Jenkins to use a single job to build a Dockerfile from an arbitrary repo, possibly using a single webhook URL for all of the repos? I want the process of adding a new repo of similar structure to be fairly easy. Or is there a better way of doing this entirely, and I'm just facing an X/Y problem?

Upvotes: 0

Views: 84

Answers (3)

Ian W
Ian W

Reputation: 4767

We have team who did a similar approach to build ~65 webservices. Implementation-wise, it's the Build with Parameters, and the repo name is the parameter.

The problem is (our configuration, may be by policy) Jenkins can only run one instance of a job at a time, so the rest are queued, so often there are idle executors but pending jobs. You'd also have trouble triggering the job via webhook.

A better option would be to write a job or groovy script that created a standard job for each repo via a template or script, differing by the job/repo name.

Upvotes: 1

Yogesh_D
Yogesh_D

Reputation: 18764

Yes you can. You can use something like the Generic Webhook Trigger. You can configure Github to send out a web hook once you have merged to say develop/master (depending on the branching strategy that you guys use). Then you can potentially lay down a convention saying that the root folder of each repo will have a build.sh that is called by this generic job. And once the build.sh completes the build it will trigger the docker build. The build.sh may not be necessary if say all your projects are maven or node.js/npm based. However, having build.sh gives repo owners a capability to customise their builds as they require.

Again this relies a lot on convention wherein you have to ensure all your repos have similar structures, and the artifacts that come out of those are stored the same way (maven repo, docker repo etc).

And to ensure that builds are not held up you can set the jobs to be run concurrently and let Jenkins manage workspaces.

Note: Having said all of this, I would still recommend that you use the pipeline way of doing builds where in, there is one job per repo and use Jenkinsfile to manage the builds.

Upvotes: 0

marxmacher
marxmacher

Reputation: 601

Why not use the Github plugin. I believe it should be this one : GitHub Branch Source -https://plugins.jenkins.io/github-branch-source

Only thing you need to do when creating a new repo is copy the Jenkinsfile from any of your other repos and tweak it to fit your new project and when you commit (assuming you have webhooks enables) the project will be added and ran.

Hope this helps.

Upvotes: 2

Related Questions