❌

Normal view

There are new articles available, click to refresh the page.
Before yesterdayMain stream

Deploy codes to remote server over SSH method

By: Kannan
13 November 2023 at 13:46
  • Create a Git Repository name "deploycode" add README.md file mention description as deploycodetoremoteserver.

  • Clone the Git repo to local machine.

  • Create Git branch as "dev" & "test"

  • Create index.html file for both branches

kannan@kannan-PC:~/deploycode$ ls
index.html  README.md
kannan@kannan-PC:~/deploycode$ cat index.html 
<h1>Testcode</h1>
<h1>testcode version-1</h1>
<h1>testcode version-2</h1>

kannan@kannan-PC:~/deploycode$ git branch 
  dev
  main
* test
kannan@kannan-PC:~/deploycode$ git checkout dev
Switched to branch 'dev'
kannan@kannan-PC:~/deploycode$ git branch 
* dev
  main
  test
kannan@kannan-PC:~/deploycode$ ls
index.html  README.md
kannan@kannan-PC:~/deploycode$ cat index.html 
<h1>Devcode </h1>
<h1>dev code version-1</h1>
<h1>dev code version-2</h1>

  • Git add . and commit, Push to the Git repo.

Remote server setup

  • Create a remote server

  • Do the "apt update" and and "install apache2" on the both server

  • Do the the "systemctl start, enable, status of the apache service"

Jenkins setup

  • Go to Jenkins dashboard > Manage Jenkins > Plugins > "publish over SSH"

  • Manage Jenkins > System > SSH Servers (Add the SSH server details for dev and test)

Image description

Image description

  • Click on advance "Use password authentication" enter the password for the remote server.

Image description

  • Test run the configuration to check the remote server connectivity.

Jenkins Project for devserver.

  • Go to Jenkins dashboard > Add items > Freestyle project> ok

  • Select the Github project and paste the Git repo "URL".
    Image description

  • Copy the Git repo http url and mention the branch as "dev"

Image description

  • Select the "Poll SCM" and set the schedule period.
    Image description

  • Select send files or execute commands over SSH.
    Image description

  • Select Editable e mail Notification, add the email on "Project recipient list"

Image description

  • Once done it will automatically Build Now and provides the output on Console output.

Image description

Image description

Jenkins Project for testserver.

  • Follow the same configuration procedure as the above "General, source code management, Build trigger, Post buils action".
  • Only need to modify the Build steps to run the "testserver"

Image description

  • Once done it will automatically Build Now and provides the output on Console output.

Image description

Image description

Build and Push Docker images to Docker Hub using Jenkins Pipeline

By: Kannan
13 November 2023 at 11:42
  • Create a Git Repository named "dockerhub_jenkins" with Readme.md

  • Clone the repo to local machine and add the below required files

  • Create Dockerfile and image to run

FROM ubuntu:22.04
ENV DEBIAN_FRONTEND=noninteractive
RUN apt update
RUN apt install apache2 -y
RUN apt install apache2-utils -y
RUN apt clean
COPY index.html /var/www/html
EXPOSE 80
CMD ["apache2ctl","-D","FOREGROUND"]
  • Create Jenkins file
kannan@kannan-PC:~/dockerhub_jenkins$ cat Jenkinsfile 
pipeline {
  agent any
  options {
    buildDiscarder(logRotator(numToKeepStr: '5'))
  }
  environment {
    DOCKERHUB_CREDENTIALS = credentials('kannanb95-dockerhub')
  }
  stages {
    stage('Build') {
      steps {
        sh 'docker build -t kannanb95/kaniyam:latest .'
      }
    }
    stage('Login') {
      steps {
        sh 'echo $DOCKERHUB_CREDENTIALS_PSW | docker login -u $DOCKERHUB_CREDENTIALS_USR --password-stdin'
      }
    }
    stage('Push') {
      steps {
        sh 'docker push kannanb95/kaniyam:latest'
      }
    }
  }
  post {
    always {
      sh 'docker logout'
    }
  }
}

  • Do the Git commit and push to the repo.

  • Login to Dockerhub >Account settings >Security >Access Token > Create New Access Token.

Image description

  • Go to Jenkins dashboard > Manage Jenkins >Credentials > systems > add docker hub credentials > at password tab paste the access token created on Docker hub.

Image description

  • At jenkins dashboard > Add items > named as "Dockerhub_jenkins" > Pipeline.

  • Pipeline > Definition > Pipeline script from SCM > set SCM as "Git"> Copy the Repo "URL" >Set branch as "Main" > save and apply > Build Now.

Image description

Image description

Image description

Image description

Image description

  • Verify the Dockerhub

Image description

Jenkins Pipeline Concept

By: Kannan
13 November 2023 at 10:41

Using Pipeline script concept

  • Jenkins dashboard > New item > select "Pipeline" named as pipeline script.

  • Pipeline >Definition > select pipeline script> edit the script enable "Use Groovy sandbox".

Image description

Image description

Image description

Using pipeline SCM concept

  • Jenkins dashboard > New item > select "Pipeline" named as pipelinescm concept.
  • Pipeline >Definition > select pipeline SCM> select "Git" on SCM > Paste the repo URL > mention the Branch "main" save and apply > Build Now.

Image description

Image description

  • Console Output

Image description

Configure and setup Email extended notification on Jenkins and create a project to verify the notification

By: Kannan
13 November 2023 at 08:30

*On respective Email configuration *

  • set 2-factor authentication and enable & add the app password under security tab. -Create the app password for Jenkins and need to add the password on jenkins.

*On Jenkins Dashboard *

  • We have completed with the installation and basic setup of Jenkins app.

  • Jenkins dashboard > Manage Jenkins > plugins >Email Extension Template Plugin.(add these plugin for E-mail Notification)

  • Jenkins dashboard > Manage Jenkins > system >E-mail Notification.(In the password tab enter the app password which we created on gmail "without space")

Image description

  • enable the "Test configuration by sending test e-mail"
    enter the email for validation purpose and verify the email inbox for Notification.

  • On Extended Email Notification add the credientials and enable "Use SSL".

Image description

  • On Default trigger tab select "Always", "Success", "Failure if any".

Image description

  • On Jenkins dashboard > Add New Item > select "Freestyle project" > provide project name on "CAMEL Format"> ok.

Image description

  • On "Build Steps" select "Execute shell" provide the commands need to run.

Image description

  • On "Post Build Action" select "Editable E mail Notification".

  • Provide the "email ID" to whom you want to send the project Build Log Notification Apply and save.

  • Once saved Click on "Build Now" to run the project and view the console output and verify the email notification.

Image description

  • If the notification Email is getting from "address not yet found" you can modify the system admin e-mail address.

Image description

  • All the Jenkins Project logs are stored on the below path
/var/lib/jenkins/jobs/

Build and Push Docker images to DockerHub using Jenkins pipeline

By: Kannan
12 October 2023 at 16:08

We are going to build an Docker image and push the image to Dockerhub using Jenkins pipeline.

We have already installed the Jenkins and done the required settings.

  • step 1: Add your user to the docker group This command adds your current user to the docker group, which grants permission to access the Docker socket
sudo usermod -aG docker jenkins

  • step 2: Now restart the Jenkins using
sudo systemctl restart jenkins
  • step 3:
    Install docker related plugins in jenkins
    docker
    docker commons
    docker API
    docker pipeline
    docker-build-step
    CloudBees Docker Build and Publish

  • step 4:
    Login to Dockerhub and create a personal access token.

Image description

  • step 5: Add the created personal access token to jenkins credentials

Image description

  • step 6: Created a Dockerfile on the Github project Repository
kannan@kannan-PC:~/dockerhub_jenkins$ vi Dockerfile
kannan@kannan-PC:~/dockerhub_jenkins$ cat Dockerfile 
FROM ubuntu:22.04
ENV DEBIAN_FRONTEND=noninteractive
RUN apt update
RUN apt install apache2 -y
RUN apt install apache2-utils -y
RUN apt clean
COPY index.html /var/www/html
EXPOSE 80
CMD ["apache2ctl","-D","FOREGROUND"]
kannan@kannan-PC:~/dockerhub_jenkins$ vi index.html
kannan@kannan-PC:~/dockerhub_jenkins$ cat index.html 
<h1>Image created and push to Dockerhub</h1>


  • step 7: Create a jenkinsfile and add to the Gitghub project Repository
kannan@kannan-PC:~/dockerhub_jenkins$ vim Jenkinsfile
kannan@kannan-PC:~/dockerhub_jenkins$ cat Jenkinsfile 
pipeline {
  agent any
  options {
    buildDiscarder(logRotator(numToKeepStr: '5'))
  }
  environment {
    DOCKERHUB_CREDENTIALS = credentials('kannanb95-dockerhub')
  }
  stages {
    stage('Build') {
      steps {
        sh 'docker build -t kannanb95/kaniyam:latest .'
      }
    }
    stage('Login') {
      steps {
        sh 'echo $DOCKERHUB_CREDENTIALS_PSW | docker login -u $DOCKERHUB_CREDENTIALS_USR --password-stdin'
      }
    }
    stage('Push') {
      steps {
        sh 'docker push kannanb95/kaniyam:latest'
      }
    }
  }
  post {
    always {
      sh 'docker logout'
    }
  }
}
kannan@kannan-PC:~/dockerhub_jenkins$ ls
Dockerfile  index.html  Jenkinsfile  README.md

Now add,commit and push to the git main

kannan@kannan-PC:~/dockerhub_jenkins$ git add .
kannan@kannan-PC:~/dockerhub_jenkins$ git status
On branch main
Your branch is up to date with 'origin/main'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
    new file:   Dockerfile
    new file:   Jenkinsfile
    new file:   index.html

kannan@kannan-PC:~/dockerhub_jenkins$ git commit -m "commit dockerhub_jenkins"
[main 5b006c5] commit dockerhub_jenkins
 3 files changed, 41 insertions(+)
 create mode 100644 Dockerfile
 create mode 100644 Jenkinsfile
 create mode 100644 index.html
kannan@kannan-PC:~/dockerhub_jenkins$ git push origin main
Enumerating objects: 6, done.
Counting objects: 100% (6/6), done.
Delta compression using up to 8 threads
Compressing objects: 100% (4/4), done.
Writing objects: 100% (5/5), 884 bytes | 884.00 KiB/s, done.
Total 5 (delta 0), reused 0 (delta 0), pack-reused 0
To github.com:kannanb95/dockerhub_jenkins.git
   397a453..5b006c5  main -> main

  • step 8: Configure the pipeline definition as "pipeline script from SCM" and add the "Github project Repository URL" and branch as "main"

Build the Jenkins pipeline
Successfully completed the build verify the build history "Console output"
Image description

❌
❌