← Back to list

How to run script on Jenkins?

Wios · 2021-11-28 15:13 · 2 claps · 3.6 min read
Open on Medium ↗
Wiki topics: ☁️ · DevOps & Cloud

How to run script on Jenkins?

Jenkins is one of of common solution when you you have plan to implement your own CICD. Jenkins is compatible with most of OS, language and it will help you save a lot of time. If everyday, you run some scripts on your local computer to do something, why don’t do it on Jenkins?

Setup

To run a script on Jenkins, first of all you need to add Execute shell from Add build step.

Then you will see a area to add your script

In this case, I use MacOS so to able run the script same as you do on your computer, you need to export path so Jenkins will know where is the plate to find the path. With path is the way we can call the command without full path. Example instead of call /usr/var/CommandX, you just need to call CommandX. To do that, you need to add CommandX to path config.

Prepare environment Settings

To export current path config to current Jenkins shell, we will need to add this one

export PATH=$PATH:/usr/local/bin

Run script directly on Jenkins

So next steps is prepare your script, example you wanna run git submodule update every time before you compile your code to binary file.

Run script from your script file

We can apply above case if you have only 1–5 scripts need to run.

Example like this

But if you have many scripts, may be 10–100 lines, if we put on Jenkins seem not a good idea. So to resolve this one, we need to put all script to one sh file. We can refer to this article https://medium.com/@kraisorn.soi/707031c694d0 to know who to put all your script to one sh file.

So instead of putting all script on Jenkins, now we can store to one script file (sh file), so on Execute shell, we just need to run that script by calling

sh <script_Name>.sh

You can run it from current directory or calling by

./MyProject/<script_Name>.sh

In our example, we navigate to our Main folder because we store our script in that folder.

So right now we can run our script quite easy.

Run script from your file with parameters

So what happen if you script require parameter, in our case, our script need to parameters: Package Name and Version.

On our local computer, we can run like this

sh xcscript.sh XGame 1.0.0

XGame is package name and 1.0.0 is the version of package.

To able to do it on Jenkins, we can add two parameters from Jenkins

Then we have to name the parameter name, in our case, we use Name so it will be more simple

We have two parameter so we need to add another one call Version with same as above steps.

So right now when you run the Job, Jenkins will ask you the parameters like this:

So we have two parameters, XGame and version will be 0.0.1. It’s done, no, we need to do one more step. Final step is link the parameters to Execute shell.

To able to do that, you just need to add the parameters after the scripts.

export PATH=$PATH:/usr/local/bin cd Main sh xcscript.sh $Name $Version

So we just added two parameters to the scripts.

So every time, after you enter the parameter, Jenkins will auto link to your scripts with your parameters.

So in this article, you know who to run the script on Jenkins.

If we have a few scripts, we can put it to Jenkins directly.

If we have a lot of scripts, we can put it in to one script file and run it on Jenkins.

If that script file need the parameter, we are able to do it on Jenkins as well.

Thanks for reading and happy coding.


메타데이터
post_id
dd893df4305c
slug
how-to-run-script-on-jenkins-dd893df4305c
url
https://medium.com/@wios/how-to-run-script-on-jenkins-dd893df4305c
canonical_url
https://medium.com/@wios/how-to-run-script-on-jenkins-dd893df4305c
author_url
https://medium.com/@wios
status
ok
fetched_at
2026-07-27 18:56:04