How to trigger Jenkins build from command line

Automate your build script using jenkins.
Automate your build script using jenkins.

Pragmatic programmers automates everything 🙂

Yes! And why not build a script for your Jenkins build. Well, it’s not that hectic but still requires few steps:

  • Open the browser.
  • Write the Jenkins URL.
  • Login.
  • Find the job.
  • Click build. 🙂

Won’t you feel happy, if the same operations work for only one step? Write the command and press enter! Boom!! 🙂

Lets see how to accomplish this. 🙂

We have discussed the solution here based on the OS.

OS X

If you are a Mac guy/girl, its pretty easy. Just write the following command:

Do not forget to replace the placeholder values.

Jenkins User Configure
Jenkins User Configure
  • <your_jenkins_username> is your Jenkins username that you use to login to Jenkins.
  • To get your <your_jenkins_API_key>:
    • Login to your Jenkins.
    • Click on the username in the top right.
    • Click on Configure.
    • Click on ‘Show API Token’ button.
    • You got it. 🙂
  • <jenkins_server_url> is your respective Jenkins server URL (development or staging).
  • <your_jenkins_job_name> is the job that you want to build.
Jenkins user get API token
Jenkins user get API token

Prepare the command by replacing the placeholders with proper values as described above and run it.

That’s it. 🙂 This is how you can trigger jenkins build from command line 🙂

Isn’t it fabulous? 🙂

Do not forget to save the command somewhere. You can also create different batch files for different projects for even easier operation. 🙂

How to commit, push and trigger jenkins build at once from command line.

Linux

If you are a nix guy/girl but could not catch the Mac yet, don’t worry. 🙂 You can just follow the instructions for OS X above. It will just work. 🙂

Alternatively, you can use ‘wget’ instead of ‘curl’ if you’re a wget fan. 🙂

And if you’re a ‘wget’ fan, you already know how to use ‘wget’. Hence, I am not repeating the instructions for ‘wget’. 🙂

It’s just replacing ‘curl’ with ‘wget’ and parameter are same. 🙂

Doesn’t work? Better use ‘curl’ lah… 🙂

Windows

That’s the 3rd in my list. If you are actually a *nix guy/girl but somehow forced to use windows and helpless :|, Why don’t you use gitbash in windows and use *nix commands. 🙂 That will just work. 🙂

If you do not understand, what I mean, (then probably you’re not a *nix person :P) ‘gitbash’ is a program combined of git and bash. A bash is shell that runs commands once you type the name of command and press enter. 🙂

For *nix fans in windows, good news is, gitbash accepts all basic *nix commands. 🙂

However, if you are a born windows user and scared of *nix, you can use Jenkins-cli.jar to run command line build commands.

  • Download Jenkins-cli.jar from http://<your_jenkins_server_url>/jnlpJars/jenkins-cli.jar. (Here <your_jenkins_server_url> is your respective Jenkins server URL (development or staging))
  • Go to http://<your_jenkins_server_url>/cli and build your command by following the instructions there.

Again do not forget to save the command somewhere. You can also create different batch files for different projects for even easier operation. 🙂

Disclaimer: I am using OS X/*nix while writing this article and hence tested those environments. I did not test the windows .jar option. Please let me know if you face any issue here. I will fix/correct and will update this article. Thank you!

And please forgive my pinches for using windows. I just could not love windows over *nix OSes yet.

Next recommended article to read: how to commit, push and trigger Jenkins build at once in one command.

Leave a Reply

Your email address will not be published. Required fields are marked *