Jenkins触发器--在其他项目执行后构建

前言:

jenkins中有多种触发器可用,可以方便的控制构建的启动

这里简单介绍下项目后构建的配置方法

1. 解释:

Build after other projects are built

Set up a trigger so that when some other projects finish building, a new build is scheduled for this project. This is convenient for running an extensive test after a build is complete, for example. This configuration complements the "Build other projects" section in the "Post-build Actions" of an upstream project, but is preferable when you want to configure the downstream project.

设置一个触发器,当其他项目完成构建后,会为该项目安排一个新的构建。例如,这对于在构建完成后运行全面测试非常方便。

此配置与上游项目的"构建后操作"部分中的"构建其他项目"相辅相成,但在需要配置下游项目时更为优选。

2. 配置项目后构建的触发器的方法

方法 1: 使用 Upstream/Downstream 配置

  1. 设置触发关系

    • 在目标构建项目(比如 A)中:
      • 进入项目配置。
      • 找到 Build Triggers 部分。
      • 勾选 Build after other projects are built
      • 在输入框中填写需要触发的项目名称(比如 B)。
    • 在被触发项目(比如 B)中,不需要额外配置。
  2. 效果

    • 每当 A 完成构建时,会自动触发 B 的构建。

方法 2: 使用 Pipeline 脚本触发

如果需要在一个目录下通过脚本触发另一个目录中的项目,可以使用 Jenkins Pipeline:

  1. 在构建配置中

    • 配置 Pipeline 类型项目。
    • 使用以下 Jenkinsfile 示例代码:

    pipeline {
    agent any
    stages {
    stage('Trigger Another Build') {
    steps {
    build job: '../other-folder/other-project', wait: false
    }
    }
    }
    }

  • ../other-folder/other-project 表示另一个目录中的构建项目路径。
  • wait: false 表示不等待被触发项目完成。

方法 3: 使用插件触发

可以通过插件实现复杂的触发场景:

  1. Parameterized Trigger Plugin:

    • 安装 Parameterized Trigger Plugin 插件。
    • 配置 Build Triggers,勾选 Build after other projects are built
    • 在输入框中填写需要触发的项目,并配置参数(如果需要)。
  2. Multibranch Pipeline:

    • 如果你使用的是多分支流水线,也可以使用 Jenkins Multibranch Pipeline 插件设置分支间的依赖。

方法 4: 脚本化触发

如果在某个构建中通过脚本触发另一个项目:

  • 使用 curl 或 Jenkins 的 REST API:

    curl -X POST -u user:apiToken http://<jenkins-url>/job/<project>/build

将此命令集成到你的构建脚本中。


注意事项

  • 确保 Jenkins 的安全设置允许相应的触发操作。
  • 在引用同一个jenkins实例上的其他构建时,使用该构建的Full project name
  • 如果项目之间路径涉及不同的文件系统目录,请检查构建权限和路径映射。

根据你的实际需求和 Jenkins 的使用环境,可以选择上述方法之一来实现跨目录触发。



相关推荐
zkmall1 小时前
企业电商解决方案哪家好?ZKmall模块商城全渠道支持 + 定制化服务更省心
大数据·运维·重构·架构·开源
绝不偷吃3 小时前
ELK日志分析系统
运维·elk·jenkins
朱包林4 小时前
day45-nginx复杂跳转与https
linux·运维·服务器·网络·云计算
孙克旭_4 小时前
day045-nginx跳转功能补充与https
linux·运维·nginx·https
Hacker_Oldv6 小时前
软件测试(功能、工具、接口、性能、自动化、测开)详解
运维·自动化
Java樱木6 小时前
使用字节Trae + MCP,UI 到网页自动化。
运维·自动化
无敌暴龙兽z9 小时前
离线环境安装elk及设置密码认证
运维·elk
好奇的菜鸟9 小时前
如何在 Ubuntu 24.04 (Noble) 上使用阿里源
linux·运维·ubuntu
bcbobo21cn9 小时前
初步了解Linux etc/profile文件
linux·运维·服务器·shell·profile
wayuncn10 小时前
月付物理服务器租用平台-青蛙云
运维·服务器·服务器租用·服务器托管·物理机租用