简体   繁体   English

Jenkins管道:如果有新的提交,如何构建新工件

[英]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: 您可以使用Git插件中的环境变量并创建条件:

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_COMMIT - 当前提交的SHA
  • GIT_PREVIOUS_SUCCESSFUL_COMMIT - SHA of the previous successfully built commit from the same branch. GIT_PREVIOUS_SUCCESSFUL_COMMIT - 来自同一分支的上一个成功构建的提交的SHA。

There are three ways to trigger your Jenkins job automatically. 有三种方法可以自动触发Jenkins作业。

  1. You can use SCM polling in your pipeline job so that it will trigger your job each time there is a change. 您可以在管道作业中使用SCM polling ,以便每次发生更改时都会触发您的作业。

    You can enable Poll SCM under ~Build Triggers` section of your pipeline job configuration. 您可以在管道作业配置的“Build Triggers”部分下启用Poll SCM

    在此输入图像描述

  2. You can use Webhooks to trigger your jobs automatically when there is a change in your GitHub repository. GitHub存储库发生更改时,您可以使用Webhooks自动触发作业。

    Follow this documentation for Webhook implementation. 请遵循Webhook实施的此文档

  3. You can use post-commit hook to trigger the job when there is a commit in your repository. 当存储库中存在提交时,您可以使用post-commit hook来触发作业。

    Create a file called post-commit under the .git/hooks directory of the repository and add the following script to it: 在存储库的.git/hooks目录下创建一个名为post-commit的文件,并向其添加以下脚本:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Jenkins脚本化管道可为每个新提交触发 - Jenkins scripted pipeline to trigger for every new commit 每当有新的提交发生时就进行构建,Jenkins + github? - Build whenever a new commit happen , Jenkins + github? 在同一 Jenkins 管道中的下一个新代理中,如何在上一个结束的代理中获取自定义环境变量(使用 AWS 代码提交 Jenkins 项目) - At next new agent in the same Jenkins Pipeline, how to get customized environment variable in last ended agent (using AWS Code commit Jenkins project) 当在同一个提交上推送新标签时,jenkins 不会触发构建 - jenkins does not trigger build when a new tag is pushed on the same commit 基于新的 GitHub 版本触发 Jenkins 构建或提交到 master 分支 - Trigger Jenkins build based on a new GitHub release or commit to the master branch Jenkins Multibranch Pipeline 仅检查推送到 GIT 的新文件或更改文件 - Jenkins Multibranch Pipeline check only the new or changed files pushed to GIT Jenkins 自动提交工件 - Jenkins automatically commit artifacts 如何只提交修改过的(而不是新的或删除的)文件? - How to commit only modified (and not new or deleted) files? 让Concourse仅在文件diff上构建新的docker容器而不是在提交时构建 - Have Concourse only build new docker containers on file diff not on commit 使用Jenkins Pipeline作业将Jenkins作业构建状态发布到Gitlab提交 - Publish Jenkins Job build status to Gitlab commit with Jenkins Pipeline job
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM