DEV Community

Cover image for How to run any Jenkins Job from the Command Line
gocruncher
gocruncher

Posted on • Edited on

How to run any Jenkins Job from the Command Line

For the last 2 years, My small team has been working with a lot of Jenkins environments and has to run a bunch of Jenkins jobs. Eventually, it got us so tired of using the browser every time and we decided to make life easier and to develop a tool that will help us get rid of these browser things

Either this is familiar to you or if you just would like to simplify your work, then let me take you through a guide on how to get started

Jenkins-job-cli

This is an open-source CLI tool written in GO to run any Jenkins Job/Build from the Command-line/Terminal: https://github.com/gocruncher/jenkins-job-cli.

Alt Text

Installation

Fetch the latest release for your platform:

Linux

sudo wget https://github.com/gocruncher/jenkins-job-cli/releases/download/v1.1.0/jenkins-job-cli-1.1.0-linux-amd64 -O /usr/local/bin/jj sudo chmod +x /usr/local/bin/jj 
Enter fullscreen mode Exit fullscreen mode

OS X brew

brew tap gocruncher/tap brew install jj 
Enter fullscreen mode Exit fullscreen mode

OS X bash

sudo curl -Lo /usr/local/bin/jj https://github.com/gocruncher/jenkins-job-cli/releases/download/v1.1.0/jenkins-job-cli-1.1.0-darwin-amd64 sudo chmod +x /usr/local/bin/jj 
Enter fullscreen mode Exit fullscreen mode

Configure Access to Multiple Jenkins

jj set dev_jenkins --url "https://myjenkins.com" --login admin --token 11aa0926784999dab5 
Enter fullscreen mode Exit fullscreen mode

where the token is available in your personal configuration page of the Jenkins. Go to the Jenkins Web Interface and click your name on the top right corner on every page, then click "Configure" to see your API token.

In case, when Jenkins is available without authorization:

jj set dev_jenkins --url "https://myjenkins.com" 
Enter fullscreen mode Exit fullscreen mode

or just run the following command in dialog execution mode:

jj set dev_jenkins 
Enter fullscreen mode Exit fullscreen mode

Shell autocompletion

As a recommendation, you can enable shell autocompletion for convenient work. To do this, run following:

# for zsh completion: echo 'source <(jj completion zsh)' >>~/.zshrc # for bash completion: echo 'source <(jj completion bash)' >>~/.bashrc 
Enter fullscreen mode Exit fullscreen mode

if this does not work for some reason, try following command that might help you to figure out what is wrong:

jj completion check 
Enter fullscreen mode Exit fullscreen mode

Examples

# Configure Access to the Jenkins jj set dev-jenkins # Start 'app-build' job in the current Jenkins jj run app-build # Start 'web-build' job in Jenkins named prod jj run -n prod web-build # makes a specific Jenkins name by default jj use PROD 
Enter fullscreen mode Exit fullscreen mode

So, I hope you find this tool helpful. I would be pretty happy, if you haven't already, please check it out and let me know what you think or what problems did you face.

Top comments (0)