在 Jenkins 上自动化 Git 参数

Automate Git Parameter on Jenkins

我目前正在开发一个 CI 项目,其中 jenkins 根据对 git 存储库所做的更改构建软件包,而我 运行 遇到了一些问题使用 git 个参数。

包的名称根据版本号更改,因此我目前通过 git 参数插件.

这部分工作正常。但是,在部署项目之前,用户必须进入 jenkins 应用程序并从下拉菜单中选择正确的版本号作为构建参数。我希望这部分完全自动化,以便 jenkins 脚本每次都知道选择最高版本号。这消除了用户每次想要创建新包时都需要进入并从下拉菜单中选择正确的数字。

这可能吗?

尝试使用 Jenkins 共享库并使用 . groovy 文件。

I currently get a list of the current version numbers […] through the git parameter plug in

据此,我假设您的意思是您正在使用 Git 标签或分支名称(例如 v1.0、v1.1、v2.0)来识别版本。


当您手动触发构建使用 Git 参数插件的作业时,它会获取标签列表(或分支等),并在网络中显示它们 UI您可以在构建实际开始之前从中进行选择,然后您必须滚动浏览并单击最新的标签名称。

当自动触发构建时(通过计时器、另一个构建、提交挂钩等),构建将只使用您在参数上配置的“默认值”。哪个没用。

我想这就是您可能遇到的两个问题。

然而,尽管某些 documentation/inline 帮助似乎建议,但设置默认值 不是 强制性的。

通过设置默认值,结合“智能降序”排序,应该可以达到预期的效果。例如:

pipeline {
  agent any
  triggers {
    // Trigger a build automatically every minute,
    // to demonstrate that automated builds will
    // correctly pick up the latest Git tag name
    cron('* * * * *')
  }
  parameters {
    // Sort the Git repo's tags in descending order of version, so
    // the newest one will appear at the top of the parameter list
    gitParameter name: 'MY_LATEST_TAG',
                 type: 'PT_TAG',
                 sortMode: 'DESCENDING_SMART'
  }
  stages {
    stage('blahem') {
      steps {
        // Clone the repo, at the tag specified by the parameter
        checkout([
          $class: 'GitSCM',
          userRemoteConfigs: [[url: 'https://github.com/jenkinsci/git-parameter-plugin']],
          branches: [[name: "refs/tags/${params.MY_LATEST_TAG}"]]
        ])
      }
    }
  }
}

(注意:如果您使用的是 Pipeline(与 Freestyle 作业相反),您应该首先使用有效值作为分支名称构建一次 Pipeline(例如 master 而不是 refs/tags/${params.MY_LATEST_TAG}),以允许正确获取 SCM 配置)

如果您从网络触发此管道的构建 UI,您会看到标签按版本降序正确排序:

git-parameter-0.9.11
git-parameter-0.9.10
git-parameter-0.9.9
git-parameter-0.9.8
…

但是,如果使用默认或非智能排序顺序,这些值将作为字符串进行比较,因此您最终会得到类似这样的结果(即字典顺序正确,但语义不好):

git-parameter-0.9.2
git-parameter-0.9.11
git-parameter-0.9.10
git-parameter-0.9.1
git-parameter-0.9.0
…

通过指定默认值,当自动触发此管道的构建时,将在运行时获取标签列表,并在 (正确排序)列表将用作参数值。