Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to trigger Jenkins build without cloning the repository it is hooked to?

Tags:

git

jenkins

gitea

I'm dynamically creating jenkins jobs using a config.xml file as a template. Basically what I want to achieve is that, when someone pushes to the repository, this will trigger the job in jenkins. This job should then pull a docker image, create a container and clone the repository it is hooked to inside it. The idea is to avoid any malicious code being downloaded to our server. Instead, it will be downloaded inside a docker container, run an executable inside the container, and then the container will be removed.

The problem is that whenever someone pushes to the git repository, the jenkins job automatically clones the repo. Is there a way to keep the hook to the repo but stop it from cloning?

We are not using a jenkinsfile because it would have to be inside the repository, and anybody could modify it, so that's why we are creating the jenkins job from a config.xml template.

I read that the option skipdefaultcheckout exists inside jenkinsfile in order to stop cloning the repo? Is it possible to set this up inside config.xml? Is this the correct option to solve what I'm trying to do?

like image 271
Ant100 Avatar asked Nov 21 '20 02:11

Ant100


People also ask

How do you trigger a build in Jenkins?

Follow the steps as mentioned below to trigger a Jenkins job automatically based on GitHub's webhook configurations: Step 1: Go to the Configuration page of the respective job and under the build trigger section, check the "GitHub hook trigger for GITScm polling" checkbox and click on the Save button.

What are all the different ways that you can trigger a Jenkins build?

Developers can follow these three steps to implement a remote Jenkins build trigger: Create a Jenkins build job and enable the Trigger builds remotely checkbox. Provide an authentication token; This can be any text string of your choice. Invoke the Jenkins build URL to remotely trigger the build job.


1 Answers

Assumption: Relevant docker plugins are already installed on Jenkins.

Install ssh-agent plugin to pass ssh credentials to docker container for cloning the repo inside docker.

Sample groovy snippet for repo checkout within docker container that can used.

withDockerContainer(args: '-u root', image: "${image}") {
  sshagent(['jenkins-credentials']) {
    sh "mkdir ~/.ssh/ && echo -e 'Host *\n  StrictHostKeyChecking no' > ~/.ssh/config && cat ~/.ssh/config && ssh-add -l"
    git changelog: false, credentialsId: '<ID>', poll: false, url: "<REPO URL>"
    sh 'echo "repo cloned inside container !!!"'
  }
}

like image 95
Shashank Sinha Avatar answered Sep 24 '22 12:09

Shashank Sinha