Jenkins - 使用相同的参数停止并发作业

时间:2016-06-07 11:31:13

标签: jenkins jenkins-plugins

我有一个用于db回滚脚本的Jenkins作业,该脚本使用每个环境的选项参数(使用NodeLabel参数插件)。

我希望这些作业能够同时运行,但仅适用于不同的环境。

"必要时执行并发构建"已启用。

E.g。如果作业正在运行LIVE,请允许某人再次为TEST运行作业(这可行)。但是,如果LIVE已经在运行并且有人再次为LIVE运行作业,则不要运行。

这个插件似乎符合我的需要,但没有显示在Manage Jenkins的可用插件列表中。

https://wiki.jenkins-ci.org/display/JENKINS/Concurrent+Run+Blocker+Plugin

还有其他方法吗?

1 个答案:

答案 0 :(得分:1)

现有的Jenkins插件有一个解决方案:

  • 创建名为 Starter的 Freestyle项目,仅用于节点上的并发构建
  • ☑此版本已参数化

    • 节点 [ NodeLabel Parameter Plugin ]
      • 姓名:NODE
    • 选择参数
      • 姓名:JOB
      • 选择:... the jobs' names you'd like to start with this ...
  • <强>构建

#!/bin/bash +x -e  
# Bash 4 needed for associative arrays

# From http://stackoverflow.com/questions/37678188/jenkins-stop-concurrent-job-with-same-parameter

echo '  Build --> Conditional step (single) --> Execute Shell'
echo "  Checking whether job '$JOB' runs on node '$NODE'"

echo '  Creating array'
declare -A computers

# ------------------------------------------------------------------------
# Declare your nodes and their executors here as mentioned, for instance,
# in the API URI 'http://<jenkins>/computer/(master)/executors/0/api/xml':
computers=(       #                          ^^^^^^            ^
  [master]="0 1 2 3"
  [slave]="0 1"
  )
# Note: Executor indices DO NOT conform to the numbers in Jenkins' 
#       Build Executor Status UI.
# ------------------------------------------------------------------------

echo "  Checking executors of node '$NODE'"
for computer in ${!computers[@]} ; do
  for executorIdx in ${computers[$computer]} ; do

    if [[ $computer == $NODE ]] ; then

      if [[ "$computer" == "master" ]] ; then
        node="(${computer})"
      else
        node=$computer
      fi
      url="${JENKINS_URL}/computer/${node}/executors/${executorIdx}/api/xml?tree=currentExecutable\[url\]"
      echo "    $url"

      xml=$(curl -s $url)
      #echo $computer, $executorIdx, $xml

      if [[ "$xml" == *"/job/${JOB}"* ]] ; then
        echo "    Job '$JOB' is already building on '$computer' executor index '$executorIdx'"
        echo '  Exiting with 1'
        exit 1
      fi
    fi
  done
done

echo '  Exiting with 0'

  • 构建器:Set the build result
    • 结果:Aborted

  • 条件步骤(单个)
    • 运行?:Current build status
    • 构建器:Trigger/call build on other projects
      • 构建触发器:
        • 要构建的项目:$JOB [忽略错误消息]
        • 节点标签参数
          • 姓名:NODE [或您在下游工作中如何称呼]
          • 节点:$NODE