Skip to content
pkieratPublic

About

A convenient wrapper script for the Jenkins CLI with support for tab completion for Bash and Zsh, local cache of jobs and parameters and fuzzy selection (using fzf).

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

11 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

jcli

jcli is a wrapper script for the Jenkins CLI. It passes its arguments to jenkins-cli.jar and adds:

  • one-time interactive configuration, with the API token stored in a separate file that is readable only by you,
  • automatic download of jenkins-cli.jar from your Jenkins server,
  • several Jenkins servers or accounts through profiles,
  • tab completion for Bash and Zsh: commands, options, job paths and build parameters,
  • a local cache of job paths used by the completion.

Dependencies

Dependency Needed for
Bash 4+ Running jcli
Java Running jenkins-cli.jar; use a version supported by your Jenkins server (recent releases need Java 17+)
curl Downloading jenkins-cli.jar; completion of build parameters
coreutils, readlink -f Locating Java when JAVA_HOME is not set
jq Completion of build parameters
Bash 5.2+ or Zsh Tab completion
fzf (optional) Fuzzy selection of job paths during completion
make, GNU sed Building from source

On the Jenkins side you need an account with an API token (your user → Security → API Token).

Installation

git clone git@gitlab.com:pkierat/jcli.git
cd jcli
make install                        # installs to ~/.local/bin
make install INSTALL_DIR=/usr/local/bin

make alone builds jcli in the repository root; you can also copy that file to any directory on your PATH.

Then run any command once to configure it:

$ jcli who-am-i
Jenkins URL (e.g. https://jenkins.example.com): https://jenkins.example.com
Jenkins username: jdoe
Jenkins token:
Downloading jenkins-cli.jar...
Done.
Authenticated as: jdoe
...

The token is not echoed while you type it.

Configuration

jcli keeps its data in ~/.jenkins-cli:

File Content
config JENKINS_URL="..."
auth username:token, passed to Jenkins as -auth @FILE (mode 600)
jenkins-cli.jar Downloaded on first use
cache/ Job paths and build parameter names used by the completion

To reconfigure, delete config or auth and run jcli again. A config file from an older version, which also held the username and token, is detected as incomplete: you are asked for the settings once and the token is moved to auth.

Environment variables:

Variable Default Description
JCLI_PROFILE – Uses config-<profile> and auth-<profile>, e.g. for a second Jenkins server
JCLI_DATA ~/.jenkins-cli Data directory
JCLI_CONFIG $JCLI_DATA/config[-<profile>] Configuration file
JCLI_AUTH $JCLI_DATA/auth[-<profile>] Credentials file
JENKINS_JAR $JCLI_DATA/jenkins-cli.jar Jenkins CLI jar
JAVA_HOME resolved from java on PATH Java used to run the jar

Example with two servers:

alias jcli-prod='JCLI_PROFILE=prod jcli'

Usage

Every Jenkins CLI command is available; jcli help lists the ones your server and permissions allow, jcli help COMMAND describes one.

jcli who-am-i                          # check the configuration
jcli list-jobs                         # top-level jobs and folders
jcli list-jobs Development/samurai     # jobs in a folder
jcli console Development/samurai/Build -n 100      # last 100 lines of the last build
jcli console Development/samurai/Build 123 -f      # follow build #123
jcli build Development/samurai/Build -p BRANCH=team/samurai -f -v

Job paths use / between folders. Quote or escape names with spaces. In multibranch pipelines a / inside a branch name is written as %2F, e.g. my-service/feature%2FX.

Useful build options:

Option Meaning
-p KEY=VALUE Build parameter; repeat for each parameter
-w Wait until the build starts
-f Wait until the build finishes; interrupting jcli does not stop the build
-s Wait until the build finishes; interrupting jcli aborts the build
-v Print the console output while waiting
-c Build only if there are SCM changes

With -f or -s the exit status reflects the build result.

Aliases work well for builds you start often:

alias deploy-tts='jcli build Development/samurai/Build\ Artifacts -p BRANCH=team/samurai -p DEPLOY_TTS=true -f'

Completion

Zsh, in ~/.zshrc:

source <(jcli completion zsh)

Bash, in ~/.bashrc:

source <(jcli completion bash)

Completion covers subcommands, options, job paths (one folder level at a time, or fuzzy with fzf) and the parameter names of a job after -p:

$ jcli build D<TAB>
DBA      Development
$ jcli build Development/samurai/Bui<TAB>
$ jcli build Development/samurai/Build\ Artifacts -p <TAB>
BRANCH=      DEPLOY_TTS=

Cache

Job paths are cached as directories in ~/.jenkins-cli/cache, and the parameter names of a job in a .params file inside its directory. Entries are never refreshed: when jobs are added, renamed or get new parameters, remove the cache (or the affected subdirectory).

rm -rf ~/.jenkins-cli/cache

Contributing

Layout

File Content
src/jcli.template.sh The main script, with placeholders for the completion scripts
src/jcli.bash Bash completion
src/jcli.zsh Zsh completion
Makefile Builds src/jcli.out by inserting the completions into the template, then copies it to jcli
jcli The built script, committed so it can be installed without make

Edit the files in src/, never jcli or src/jcli.out directly, then run make and commit the sources together with the rebuilt jcli.

Checks

There are no automated tests. Before sending a change:

make
bash -n jcli && bash -n src/jcli.bash && zsh -n src/jcli.zsh
shellcheck src/jcli.template.sh src/jcli.bash     # if installed

and try the affected commands and completion against a Jenkins instance, both for a fresh setup (JCLI_DATA=$(mktemp -d) jcli who-am-i) and an existing one.

Guidelines

  • Keep jcli a thin wrapper: arguments go to jenkins-cli.jar unchanged.
  • Never put credentials on a command line, where other users can see them in ps: use -auth @FILE for the jar and curl -K (a config file or process substitution) for curl.
  • Create files holding credentials with umask 077.
  • Write messages for the user to stderr; keep stdout for command output.
  • One topic per commit, with a short imperative subject line.

Send changes as a merge request on GitLab.

License

MIT

About

A convenient wrapper script for the Jenkins CLI with support for tab completion for Bash and Zsh, local cache of jobs and parameters and fuzzy selection (using fzf).

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages