简体   繁体   中英

Jenkins pipeline: How to build new artifacts only if has a new commit

stage('Checkout repo'){
    git branch: 'mybranch', 
    credentialsId: '6b83e39e-1c8c-44c2-9165-b1f5a857f6cb', 
    url: 'git@gitlab.com:myproject.git'}

stage('run tests'){
    sh 'mvn test'
}

stage('build artefact'){
    sh 'mvn clean package'
}

how to run tests and make new artifact only if has a new commit? without a trigger

You could use environment variables from the Git Plugin and create a condition:

if (env.GIT_COMMIT != env.GIT_PREVIOUS_SUCCESSFUL_COMMIT) {
  stage('run tests'){
    sh 'mvn test'
  }
  stage('build artefact'){
    sh 'mvn clean package'
  }
}
  • GIT_COMMIT - SHA of the current commit
  • GIT_PREVIOUS_SUCCESSFUL_COMMIT - SHA of the previous successfully built commit from the same branch.

There are three ways to trigger your Jenkins job automatically.

  1. You can use SCM polling in your pipeline job so that it will trigger your job each time there is a change.

    You can enable Poll SCM under ~Build Triggers` section of your pipeline job configuration.

    在此输入图像描述

  2. You can use Webhooks to trigger your jobs automatically when there is a change in your GitHub repository.

    Follow this documentation for Webhook implementation.

  3. You can use post-commit hook to trigger the job when there is a commit in your repository.

    Create a file called post-commit under the .git/hooks directory of the repository and add the following script to it:

    #!/bin/bash curl --user 'user:pass' -X POST "http://server.org.com/jenkins/job/JOB-NAME/build" --data token=mytoken1 --data delay=0sec

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