Skip to main content

Command Palette

Search for a command to run...

Learning Jenkins – Part 2: Running Shell Scripts, Parameterized Jobs & GitHub Integration

Published
4 min read

Now in Part 2, we’ll go a step further:

  • Running Shell Scripts with Jenkins

  • Creating Parameterized Jobs

  • Connecting Jenkins with GitHub for Continuous Integration

By the end of this post, you’ll have a basic CI setup where code pushed to GitHub triggers a Jenkins job.


Running Shell Scripts in Jenkins

Jenkins can do much more than just printing text — it can install software, run deployments, and execute complex scripts.

Step 1 – Create a Freestyle Job

  1. From the Jenkins dashboard → Click New Item

  2. Enter a job name (e.g., RunShellJob)

  3. Select Freestyle project → Click OK


Step 2 – Add a Build Step

Scroll to the Build section → Click Add build step → Select Execute Shell

You can either:

  • Directly write commands, or

  • Point to an existing script file


Method 1 – Direct Shell Commands

Example: Install Apache web server.

sudo yum install httpd -y
sudo systemctl enable httpd
sudo systemctl start httpd

Good for quick commands.


Method 2 – Running a Script File

Store commands in a file (e.g., apache.sh) and call it.

apache.sh

#!/bin/bash
sudo yum install httpd -y
sudo systemctl enable httpd
sudo systemctl start httpd
echo "Apache Installed Successfully!"

Make it executable:

chmod +x apache.sh

In Jenkins → Execute Shell → add:

sh /home/ec2-user/apache.sh

Cleaner and reusable.


Step 3 – Verify

Click Build Now → Check logs in Console Output → Apache should install and run.
Test in browser: http://<EC2-Public-IP>


Parameterized Builds in Jenkins

Default jobs always run the same way. But what if you need to:

  • Deploy different branches (dev, staging, prod)

  • Specify an app version

  • Run jobs with different inputs

That’s where Parameters help.


Example 1 – String Parameter

  1. Enable: This project is parameterized

  2. Add String Parameter:

    • Name: APP_VERSION

    • Default: v1.0.0

  3. In Execute Shell:

echo "Deploying Application Version: $APP_VERSION"

At build time, Jenkins asks for input.


Example 2 – Choice Parameter

  1. Add Choice Parameter:

    • Name: BRANCH_NAME

    • Choices:

        dev
        staging
        production
      
  2. In Execute Shell:

echo "Building branch: $BRANCH_NAME"

Jenkins shows a dropdown menu during build.


Example 3 – Conditional Script

Use parameters inside scripts:

if [ "$BRANCH_NAME" == "production" ]; then
  echo "Deploying to Production Server"
  # Prod deployment steps
else
  echo "Deploying to $BRANCH_NAME environment"
  # Other environment deployment steps
fi

One job can serve multiple environments.


GitHub Integration with Jenkins

Now let’s connect Jenkins with GitHub to trigger builds when code changes.


Step 1 – Configure GitHub Repository in Jenkins

  1. Open your job → Configure

  2. Under Source Code Management, select Git

  3. Add your GitHub repo URL (HTTPS or SSH)
    Example:

     https://github.com/your-username/sample-app.git
    
  4. If private, add GitHub credentials in Jenkins.


Step 2 – Set Branch

Specify which branch Jenkins should build, e.g., */main.


Step 3 – Add a Build Step

In Build → Execute Shell, add commands:

echo "Pulling latest code from GitHub..."
ls -l

(We’ll replace this with Maven/Gradle build later.)


Step 4 – Configure GitHub Webhook

So that Jenkins gets triggered automatically:

  1. Go to your GitHub repo → Settings → Webhooks

  2. Add webhook:

    • Payload URL: http://<your-jenkins-server>:8080/github-webhook/

    • Content type: application/json

    • Events: Choose “Just the push event”

    • Save


Step 5 – Enable GitHub Trigger in Jenkins

In Jenkins job config → Build Triggers → Check:
GitHub hook trigger for GITScm polling


Step 6 – Test the Integration

  1. Push code to GitHub

  2. Jenkins job should trigger automatically

  3. Check Console Output to verify build

Congratulations! You now have basic CI in place.


Throttling Builds

Sometimes, frequent commits can overload Jenkins.
Solution: Throttle Builds plugin → Limit number of builds at once or per hour.

Example: Allow max 2 builds at a time → others will wait in queue.


CI/CD Flow with GitHub + Jenkins

Here’s a simple diagram showing how Jenkins fits with GitHub:


Key Takeaways from Part 2

  • Jenkins can run shell commands or execute scripts

  • Parameters make jobs flexible & reusable

  • GitHub integration enables automated builds on every push

  • This forms the foundation of CI pipelines


This series is my way of learning Jenkins step by step and sharing it as I go.
If you’re following along, try:

  • Running a script via Jenkins

  • Creating a parameterized job

  • Linking Jenkins to your GitHub repo

More from this blog

Documenting My DevOps Journey

40 posts