POSTS
Jenkins Tutorial
Jenkins step by step create automation for applications
Jenkins is the best tool to create automation for our applications CI/CD. now we want to create a simple automation for our application step by step. first, we should know about how to Jenkins work.
install jenkins on ubuntu
recent version are available in an apt repository.
wget -q -O - https://pkg.jenkins.io/debian/jenkins.io.key | sudo apt-key add -
sudo sh -c 'echo deb http://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list'
sudo apt-get update
sudo apt-get install jenkins
jenkins log /var/log/jenkins/jenkins.log jenkins setting port in /etc/default/jenkins
jenkins pipline
Now we want to describe about jenkins pipeline. jenkins pipeline use groovy.we have two method type pipeline and node(script) to wirte our automation.node is an old way and you can’t use it on SCM and you should use it in Jenkins directly.becuase of that we use pipeline instead of that.
pipeline syntax
pipeline define our entire build process. which includes stages for building application. Stage block defines our task in the pipeline like build and deploy. The step is a single task and tells what to do at the particular point in time. example of pilpeline :
pipeline {
   parameters {
      string(name: 'IMAGE_REPO_NAME', defaultValue: 'myrepo/myappauto', description: '')
      string(name: 'LATEST_BUILD_TAG', defaultValue: 'build-latest', description: '')
      string(name: 'DOCKER_COMPOSE_FILENAME', defaultValue: 'docker-compose.yml', description: '')
      string(name: 'DOCKER_STACK_NAME', defaultValue: 'my_stack', description: '')
      booleanParam(name: 'PUSH_DOCKER_IMAGES', defaultValue: true, description: '')
    }
    agent any
    stages {
        stage('docker build')
        {
           environment {
              BUILD_IMAGE_REPO_TAG = "${params.IMAGE_REPO_NAME}:${env.BUILD_TAG}"
            }
            steps{
               sh "docker build . -t $BUILD_IMAGE_REPO_TAG"
               sh "docker tag $BUILD_IMAGE_REPO_TAG ${params.IMAGE_REPO_NAME}:${params.LATEST_BUILD_TAG}"
               sh "docker tag $BUILD_IMAGE_REPO_TAG ${params.IMAGE_REPO_NAME}:latest"
            }
        }
         stage('docker push'){
              when{
                expression {
                  return params.PUSH_DOCKER_IMAGES
                }
              }
              environment {
                BUILD_IMAGE_REPO_TAG = "${params.IMAGE_REPO_NAME}:${env.BUILD_TAG}"
              }
              steps{
                sh "docker push $BUILD_IMAGE_REPO_TAG"
                sh "docker push ${params.IMAGE_REPO_NAME}:${params.LATEST_BUILD_TAG}"
                sh "docker push ${params.IMAGE_REPO_NAME}:latest"
              }
            }
          stage('Docker Stack Deploy'){
               steps{
                  sh "docker stack deploy -c ${params.DOCKER_COMPOSE_FILENAME} ${params.DOCKER_STACK_NAME}"
                 }
              }
    }
}
Agent is our executive environment when you declare agent in stage or step says to it run in this environment.
any is the main shell.
String interpolation with ${} in the double quotation
you can put it on the root of your application and then create a pipeline with Jenkins
step 1
select name for your Jenkins pipeline then select pipeline.

step 2
in section piplen in difination select pipeline script form scm and then set repository URL and set Credentials for your repo and then select branch for your repo like below image image and save it.now you can build it.
