Jenkins CLI提供了SSH和Client模式.

Docker运行Jenkins

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
version: '3'
services:
  jenkins:
    image: jenkins/jenkins:alpine
    ports:
      - 8080:8080
      - 50000:50000
      - 46059:46059
    volumes:
      - "/Users/addo/DevApps/Docker/data/jenkins:/var/jenkins_home"

note: 以为是docker运行, ssh端口设置选用了固定端口.

Client

http://JENKINS_URL/cli页面下载client jar

使用方法:

1
java -jar jenkins-cli.jar -s http://localhost:8080/ help

构建:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
java -jar jenkins-cli.jar -s http://localhost:8080/ 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

启用SSH, 使用随机端口. 也可以使用固定端口:

获取SSH端口号:

1
2
curl -Lv http://localhost:8080/login 2>&1 | grep 'X-SSH-Endpoint'
#< X-SSH-Endpoint: localhost:46059

生成ssh key:

1
ssh-keygen -t rsa -b 4096 -C "admin"

添加ssh公钥:

1
http://localhost:8080/user/admin/configure

修改ssh config, 添加如下配置:

1
2
3
Host localhost
 IdentityFile ~/.ssh/id_rsa_jenkins_cli
Port 46059

验证:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
ssh admin@localhost help
  add-job-to-view
    Adds jobs to view.
  build
    Builds a job, and optionally waits until its completion.
  cancel-quiet-down
    Cancel the effect of the "quiet-down" command.
  clear-queue
    Clears the build queue.
  connect-node
    Reconnect to a node(s)
  console
    Retrieves console output of a build.
  copy-job
    Copies a job.
    ...