Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins Pipeline Build Trigger with git pull, how?

I've got a maven, java project and I'm using git. I want to use jenkins for build + test + deploy (.war file) on tomcat server (on same device)

My current question is about triggering the build with pushing changes into the git repository master. However it did work with jenkins freestyle project. There I could setup my git repository, so it detected any changes and run the build.

But as far as I could make my research using a "pipeline" should be better to run the process with build + test + deploy. So I created a pipeline and also wrote a jenkinsfile.

pipeline {
    agent any

    stages {
        stage('Compile Stage') {
            steps {
               withMaven(maven: 'maven_3_5_1'){
               bat 'mvn clean compile'
               }
            }
        }
        stage('Testing Stage') {
            steps {
                withMaven(maven: 'maven_3_5_1'){
                    bat 'mvn test'
                }
            }
        }
        stage('Deployment Stage (WAR)') {
            steps {
                withMaven(maven: 'maven_3_5_1'){
                    bat 'mvn deploy'
                }
            }
        }
    }
}

The current problem is, that inside a pipeline project I could not find an option for setting up the git repository. Currently jenkins does not track any changes in git, when I push a change.

What I've to do, so jenkins runs build when changes are detected in git (like in the freestyle project)?

I thank you very much in advance.

like image 331
aydogdu Avatar asked Dec 10 '17 14:12

aydogdu


1 Answers

Definition Inside the Repository (Jenkinsfile)

You should place the pipeline definition into a file called Jenkinsfile inside your repository.

This has the great advantage that your pipeline is also versioned. Using the Multibranch Project, you can point Jenkins to your Git repo and it will automatically discover all branches containing such Jenkinsfile (and create a job for each of them). You can find more information in the documentation.

In case you don't want jobs for different branches, you can also configure the job to take the pipeline definition from SCM:

enter image description here

With that specified, you can configure the job to poll SCM changes regularly:

enter image description here


Definition in the Job

In case you really don't want to put your pipeline into the repository (I don't recommend this), then you can use the checkout step to get your code:

pipeline {
    agent any
    stages {
        stage('Compile Stage') {
            steps {
                checkout('https://git.example.com/repo.git')
                withMaven(maven: 'maven_3_5_1') {
                    bat 'mvn clean compile'
                }
            }
        }
// ...

More options for the checkout (e.g. other branches) can be found in the step documentation.

Finally, change the job to be built in regular intervals:

enter image description here

And now comes the point where I'm struggling (while editing the post): This probably builds the project every time (5min in the example). I am not sure, if currentBuild.changeSets contains the changes that are explicitly checked out with checkout. If it does, then you can check, if it contains changes and in such cases abort the build. All not very nice...

like image 55
StephenKing Avatar answered Nov 30 '22 04:11

StephenKing