DEV Community

Wycliffe A. Onyango
Wycliffe A. Onyango

Posted on

100 Days of DevOps: Day 78

Jenkins Conditional Pipeline Deployment for Nautilus Web App

Objective

The goal of this task was to configure a Jenkins Pipeline job that automatically deploys a static website from the Gitea repository depending on the parameters selected to the Nautilus Application Servers, using the Storage Server (ststor01) as the Jenkins agent.

The deployed web application should be accessible directly from the root URL:

https://8091-port-7l43zyf3btna3i6q.labs.kodekloud.com/
Enter fullscreen mode Exit fullscreen mode

and not from a subdirectory like /web_app.


Step-by-Step Implementation

1. Install and Configure Jenkins Plugins

To ensure the pipeline could interact with Git and remote agents, the following plugins were verified and installed via Manage Jenkins → Plugins:

  • Git Plugin → enables Jenkins to clone and pull from Git repositories.

  • SSH Agent Plugin → required for communication between Jenkins and remote agents.

  • Pipeline Plugin → allows scripted and declarative pipelines to run.

After installation, Jenkins was restarted to apply changes.


2. Configure Jenkins Credentials

Instead of using an SSH key, the connection to the Storage Server was configured to use username and password authentication.

Steps:

  1. Go to Manage Jenkins → Credentials → System → Global Credentials → Add Credentials
  2. Select “Username and Password” as the credential type.

  3. Enter:

  • Username: natasha
  • Password: (used password provided in the task)
  • ID: ststor01-password

4.Click Save.

These credentials were later linked to the Storage Server node so Jenkins could authenticate via SSH using natasha’s account.


3. Add Jenkins Agent Node (Storage Server)

A new node was created to allow Jenkins to deploy code directly on the Storage Server.

Steps:

  1. Navigate to Manage Jenkins → Nodes → New Node
  2. Configure the node with:
  • Name: Storage Server
  • Remote Root Directory: /var/www/html
  • Labels: ststor01
  • Launch method: Launch agents via SSH
  • Host: IP or hostname of the Storage Server
  • Credentials: select natasha (ststor01-password)

3.Save and verify the node connects successfully and shows “Online.”


4. Verify Environment on Storage Server

On the Storage Server, verify prerequisites and directory setup:

whoami
# natasha
cd /var/www/html
git --version   # confirm git installed
Enter fullscreen mode Exit fullscreen mode

Apache was already running on port 8080 and serving from /var/www/html.


5. Access Jenkins and Gitea

Logged into both systems using provided credentials:

  • Jenkins:

    • Username: admin
    • Password: Adm!n321
  • Gitea:

    • Repository URL: https://80-port-7l43zyf3btna3i6q.labs.kodekloud.com/sarah/web_app
    • Username: sarah
    • Password: (used as provided)

Confirmed repository contained master and feature branches.


6. Create the Jenkins Pipeline Job

  1. Click New Item → Pipeline
  2. Name it nautilus-webapp-job
  3. Enable “This project is parameterized” and add a String Parameter:
  • Name: BRANCH
  • Default Value: master
  • Description: Branch to deploy (master or feature)

4.Under the Pipeline script section, paste the working Groovy script below.


7. Final Working Jenkins Pipeline Script

node('ststor01') {
    stage('Deploy') {
        def branchName = params.BRANCH?.trim()
        if (!branchName) {
            branchName = "master"
        }

        if (branchName != "master" && branchName != "feature") {
            error("Invalid BRANCH parameter: ${branchName}. Use 'master' or 'feature'.")
        }

        sh """
            set -e
            echo "=== Deployment started for branch: ${branchName} ==="

            cd /var/www/html

            if [ ! -d .git ]; then
                echo "Repository not found. Cloning..."
                git clone https://80-port-7l43zyf3btna3i6q.labs.kodekloud.com/sarah/web_app.git .
            fi

            echo "Fetching and resetting to ${branchName}"
            git fetch origin
            git checkout -f ${branchName} || git checkout -b ${branchName} origin/${branchName}
            git reset --hard origin/${branchName}

            echo "=== Deployment complete at /var/www/html ==="
        """
    }
}
Enter fullscreen mode Exit fullscreen mode

This script:

  • Automatically clones the repository if missing.
  • Fetches and synchronizes the correct branch (master or feature).
  • Deploys directly to /var/www/html, ensuring no subdirectory path like /web_app.

8. Run and Verify the Pipeline

Triggered the job with parameter:

BRANCH = master
Enter fullscreen mode Exit fullscreen mode

Expected Console Output:

=== Deployment started for branch: master ===
Fetching and resetting to master
HEAD is now at e12c8f4 Updated index.html
=== Deployment complete at /var/www/html ===
Enter fullscreen mode Exit fullscreen mode

Job completed successfully with no workspace errors.


9. Validation

Opened the web app at:

https://8091-port-7l43zyf3btna3i6q.labs.kodekloud.com/
Enter fullscreen mode Exit fullscreen mode

Confirmed the content loaded correctly from the root URL (no /web_app subpath), proving that deployment targeted the proper directory.


Key Notes

  • Installed and verified Git, SSH Agent, and Pipeline plugins before running jobs.
  • Used username + password authentication for the natasha account (no SSH key).
  • Always ensure node labels match (ststor01).
  • The workspace issues were avoided by deploying directly under /var/www/html.
  • The BRANCH parameter allows switching between master and feature easily.

result

Top comments (0)