top of page

Creating a Jenkins Pipeline can be done in two ways: directly in the Jenkins Web UI (good for testing) or using a Jenkinsfile from Source Control (Best Practice).

Here is the step-by-step guide to setting up your first Declarative Pipeline.


Phase 1: Create the Job in Jenkins

  1. Login to your Jenkins Dashboard.

  2. Click on New Item in the top-left menu.

  3. Enter a name for your pipeline (e.g., My-First-Pipeline).

  4. Select Pipeline from the list of project types.

  5. Click OK at the bottom.


Phase 2: Configure the Pipeline Script

You will be redirected to the Configuration page. Scroll down to the section labeled Pipeline. Here you have two choices:


Direct Script


  1. Find the Definition dropdown.

  2. Select Pipeline script.

  3. Copy and paste the following "Hello World" code into the text area:

    ree

pipeline {
    agent any

    stages {
        stage('Build') {
            steps {
                echo 'Building the application...'
            }
        }
        stage('Test') {
            steps {
                echo 'Running tests...'
            }
        }
        stage('Deploy') {
            steps {
                echo 'Deploying to production...'
            }
        }
    }
}

Phase 3: Run the Pipeline

  1. Click Save at the bottom of the page.

  2. You will be taken to the Project Dashboard. Click Build Now in the left-hand menu.

  3. Look at the Build History on the bottom left. You will see a flashing entry (e.g., #1).

  4. Click on the build number (#1) and then click Console Output to see the logs.



Pipeline from SCM


  • Go to your Repository: Open your browser and navigate to Add a New File:

    in your repository

    Click the "Add file" button (or the "Create new file" button if the repo is empty).


  • Name the File: In the file name field, type the exact name:

    Jenkinsfile


  • Paste the Content: 

This script outlines the three core stages of Continuous Integration/Continuous Delivery (CI/CD): Build, Test, and Deploy.

Groovy

pipeline {
    // 1. Agent: Defines where the pipeline runs. 'any' means any available Jenkins agent/node.
    agent any

    // 2. Stages: Defines the sequence of work to be performed.
    stages {
        stage('Checkout Source Code') {
            steps {
                // This command tells Jenkins to pull the code from the Git repository 
                // that you configured in the job settings.
                checkout scm
                echo "Source code successfully checked out."
            }
        }
        
        stage('Build') {
            steps {
                echo "Starting application build..."
                // Replace the 'echo' below with your actual build command (e.g., sh 'npm install' or sh 'mvn package')
                echo "Build completed successfully."
            }
        }
        
        stage('Test') {
            steps {
                echo "Running unit and integration tests..."
                // Replace the 'echo' below with your actual test command (e.g., sh 'npm test' or sh 'mvn test')
                echo "All tests passed."
            }
        }
        
        stage('Deploy') {
            steps {
                echo "Deploying application to environment..."
                // Replace the 'echo' below with your deployment commands (e.g., sh 'docker push' or sh 'scp')
                echo "Deployment finished."
            }
        }
    }
    
    // 3. Post: Actions to run after the pipeline completes, regardless of success or failure.
    post {
        always {
            echo 'Pipeline job finished.'
        }
        failure {
            echo 'The Pipeline FAILED! Review the console output immediately.'
        }
        success {
            echo 'Pipeline successfully completed all stages.'
        }
    }
}
  • Commit the Change:

    • Scroll down to the bottom.

    • Add a short commit message (e.g., "Add initial Jenkinsfile").

    • Click the green "Commit new file" button.


  1. Find the Definition dropdown.

  2. Select Pipeline script from SCM.

  3. SCM: Select Git.

  4. Repository URL: Paste your GitHub/GitLab repo URL.

  5. Credentials: Select your git credentials (if the repo is private).

  6. Script Path: Ensure this says Jenkinsfile (this means Jenkins will look for a file with that exact name in the root of your repo).


Run the Pipeline

  1. Click Save at the bottom of the page.

  2. You will be taken to the Project Dashboard. Click Build Now in the left-hand menu.

  3. Look at the Build History on the bottom left. You will see a flashing entry (e.g., #1).

  4. Click on the build number (#1) and then click Console Output to see the logs.



How to Create a Jenkins Pipeline Locally and Build It from GitHub


1) Create empty repo on GitHub


  1. Go to GitHub → New repository.

  2. Name it e.g. jenkins-pipeline-demo.

  3. Important: Do NOT add README, .gitignore or license (choose Create repository with an empty repo).


  4. Copy the repo URL (HTTPS): https://github.com/YOURUSERNAME/jenkins-pipeline-demo.git


2) Clone the repo to local (VS Code terminal)

Open VS Code → open Terminal (`Ctrl+``) and run:

# choose a folder where you want the project
cd C:\path\to\projects

# clone the empty repo
git clone https://github.com/YOURUSERNAME/jenkins-pipeline-demo.git

cd jenkins-pipeline-demo

# open folder in VS Code
code .

If Git asks for credentials when cloning a private repo, use username + PAT, or use SSH cloning if you prefer.


3) Create the Jenkinsfile locally

In VS Code:

  1. In Explorer → click New File → name it Jenkinsfile (no extension).

  2. Paste this minimal pipeline:

pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                echo 'Hello from Jenkinsfile in GitHub!'
            }
        }
    }
}

Save the file.


4) Commit & push from VS Code (Terminal or Source Control)

Using terminal in VS Code:

git add Jenkinsfile
git commit -m "Add Jenkinsfile - initial pipeline"
git push origin main

Notes:

  • If main branch doesn’t exist locally, create it:git branch -M mainthen push.

  • If git asks for username/password for HTTPS, use your GitHub username and a Personal Access Token (PAT) as password (recommended). Or set up SSH keys.

VS Code GUI alternative: Use Source Control tab → Stage → Commit → Push.


5) Run Jenkins in Docker (make sure container has git)

You said you run Jenkins in Docker. Use this recommended command to start Jenkins with persistent volume and port 8080:

docker run -d --name jenkins \
  -p 8080:8080 -p 50000:50000 \
  -v jenkins_home:/var/jenkins_home \
  jenkins/jenkins:lts


6) Create Jenkins Pipeline job pointing to GitHub


  1. Jenkins → New Item → Enter name (e.g., My-GitHub-Pipeline) → Pipeline → OK

  2. In job configuration → Pipeline section:

  3. Save.


7) Build / Test

Click Build Now.Go to the Build → Console Output and you should see:

Hello from Jenkinsfile in GitHub!

 
 
 

Step 1 — Create project folder in VS Code

  1. Open VS Code

  2. Go to File → Open Folder

  3. Create/open a folder named:hello-docker-python

Step 2 — Create files

Inside VS Code, create these files:

📄 1. hello.py

print("Hello from Dockerized Python!")

📄 2. Dockerfile

FROM python:3.11-slim
WORKDIR /app
COPY hello.py .
CMD ["python", "hello.py"]

📄 3. .dockerignore

__pycache__
*.pyc
.git

📄 4. README.md

Simple Python Docker demo.

Run:
  docker build -t hello-python .
  docker run --rm hello-python

Step 3 — Test Docker locally (optional but recommended)

Open VS Code terminal:Ctrl + `

Run:

docker build -t hello-python .

Then run:

docker run --rm hello-python

Expected output:

Hello from Dockerized Python!

If you see this, Docker is correct.

Step 4 — Upload project to GitHub from VS Code

A) Create empty GitHub repo

  1. Go to GitHub → New Repository

  2. Name: hello-docker-python

  3. Do NOT add README

Copy your repository HTTPS URL, example:

B) Push your project from VS Code

In the VS Code terminal, run these commands:

git init
git add .
git commit -m "Initial commit"
git branch -M main
git remote add origin https://github.com/yourname/hello-docker-python.git
git push -u origin main

(Replace the URL with your own)

DONE!

You will now see all your files on GitHub:

 
 
 

  1. Go to CMD and and type below command


docker pull jenkins/jenkins:lts

  1. After that write the below command


docker run -d --name jenkins -p 8080:8080 -p 50000:50000 -v jenkins_home:/var/jenkins_home jenkins/jenkins:lts

 
 
 

© 2023 by newittrendzzz.com 

  • Facebook
  • Twitter
  • Instagram
bottom of page