jenkins 触发远程 job 并返回值(备忘)

远程 job

有时候,可能会有 jenkins 1 任务过程中,有步骤执行 jenkins 2 中的构建任务

这个时候就需要触发远程 job 并检查返回值

具体过程

  1. jenkins 开启 SSH server

    • https://wiki.jenkins.io/display/JENKINS/Jenkins+SSH
  2. 添加公钥

    • https://www.jenkins.io/zh/doc/book/managing/cli/
    • https://docs.github.com/en/github/authenticating-to-github/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent
  3. jinkins 对应账号设置,添加公钥

  4. 远程构建命令

    可以参考官方文档: https://www.jenkins.io/doc/book/managing/cli/

    这里举例下:

    fananchong@fananchong-ubuntu:~/xindong/src/haidao/backend$ ssh -l fananchong -p 54545 172.26.144.19 build -w -v -s test_remote_job
    Started test_remote_job #6
    Started from command line by fananchong
    Running as SYSTEM
    Building in workspace /var/jenkins_home/workspace/test_remote_job
    [test_remote_job] $ /bin/sh -xe /tmp/jenkins5969487095652432181.sh
    + exit 10
    Build step 'Execute shell' marked build as failure
    Finished: FAILURE
    Completed test_remote_job #6 : FAILURE
    

传递参数

参考官方文档: https://www.jenkins.io/doc/book/managing/cli/

% ssh -l kohsuke -p 53801 localhost help build

java -jar jenkins-cli.jar build JOB [-c] [-f] [-p] [-r N] [-s] [-v] [-w]
Starts a build, and optionally waits for a completion. Aside from general
scripting use, this command can be used to invoke another job from within a
build of one job. With the -s option, this command changes the exit code based
on the outcome of the build (exit code 0 indicates a success) and interrupting
the command will interrupt the job. With the -f option, this command changes
the exit code based on the outcome of the build (exit code 0 indicates a
success) however, unlike -s, interrupting the command will not interrupt the
job (exit code 125 indicates the command was interrupted). With the -c option,
a build will only run if there has been an SCM change.
JOB : Name of the job to build
-c : Check for SCM changes before starting the build, and if there’s no
change, exit without doing a build
-f : Follow the build progress. Like -s only interrupts are not passed
through to the build.
-p : Specify the build parameters in the key=value format.
-s : Wait until the completion/abortion of the command. Interrupts are passed
through to the build.
-v : Prints out the console output of the build. Use with -s
-w : Wait until the start of the command
% ssh -l kohsuke -p 53801 localhost build build-all-software -f -v
Started build-all-software #1
Started from command line by admin
Building in workspace /tmp/jenkins/workspace/build-all-software
[build-all-software] $ /bin/sh -xe /tmp/hudson1100603797526301795.sh

  • echo hello world
    hello world
    Finished: SUCCESS
    Completed build-all-software #1 : SUCCESS
    %

可以看到用 -p 命令行参数

同时, job 要制作参数,如图举例:

jenkins 触发远程 job 并返回值(备忘)_第1张图片

jenkins 触发远程 job 并返回值(备忘)_第2张图片

多个参数传递,使用 -p x1=y1 -p x2=y2 ,举例:

[fananchong@qa3-haidao backend]$ ssh -l fananchong -p 54545 172.26.144.19 build -w -v -s ssh_test -p param1=hello -p param2=world
Started ssh_test #1
Started from command line by fananchong
Running as SYSTEM
Building in workspace /var/jenkins_home/workspace/ssh_test
[ssh_test] $ /bin/sh -xe /tmp/jenkins1988937464736498924.sh
+ echo param1:hello
param1:hello
+ echo param2:world
param2:world
Finished: SUCCESS
Completed ssh_test #1 : SUCCESS

Windows SSH Client

比较有名的,可以下载: https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html

参考文档

  • https://stackoverrun.com/cn/q/7774279

你可能感兴趣的:(linux,jenkins,远程,Job,返回值,Jenkins,SSH,远程构建)