Jenkins Pipeline

App Demo
http://gitlab.ztidev.com/teerakorn/jenkins-pipeline

Preview:
1. Pull source code
2. Build docker image
3. Push image to Docker registry
4. Remote to Server
5. Run docker image

ขั้นตอนการสร้าง Jenkins Pipeline มีดังนี้

  1. Install Docker on server และ Docker IO
apt-get update
apt-get install -y docker.io

2. Install Jenkins container

docker run -d --name jenkins -e JENKINS_OPTS="--prefix=/jenkins"  -p 8090:8080 -p 50000:50000 -v /var/run/docker.sock:/var/run/docker.sock -v jenkins-data:/var/jenkins_home jenkins/jenkins

3. Install ‘SSH Agent Plugin’ plug-in

Manage Jenkins > Plugins > Available plugins

4. Create Pipeline

pipeline {
    agent any

    environment {
        GIT_REPO_URL = 'http://165.22.249.52:8888/teerakorn/jenkins-pipeline.git'
        GIT_CREDENTIALS_ID = 'gitlab_account'  // Jenkins credential for Git
        DOCKER_IMAGE_NAME = 'jenkins-pipeline'
        DOCKERHUB_CREDENTIALS_ID = 'dockerhub'  // Jenkins credential for Docker Hub
        REMOTE_SERVER = '128.199.105.53'
        SERVER_CREDENTIALS_ID = 'serverdev_account'  // Jenkins credential for remote server
        DOCKERHUB_USERNAME = 'zealtech'
        APP_CONTAINER_NAME = 'pipeline-app'
        APP_CONTAINER_PORT = '88'
    }

    stages {
        stage('Pull Source Code') {
            steps {
                script {
                    // Pull the repository using the specified branch and credentials
                    git credentialsId: env.GIT_CREDENTIALS_ID, url: env.GIT_REPO_URL, branch: 'main'
                }
            }
        }

        
        stage('Build Docker Image') {
            steps {
                script {
                    // Build the Docker image
                    sh "docker build -t ${DOCKER_IMAGE_NAME} ."
                }
            }
        }
        
        stage('Login Docker Hub') {
            steps {
                script {
                    // Log in to Docker Hub
                    withCredentials([usernamePassword(credentialsId: env.DOCKERHUB_CREDENTIALS_ID, passwordVariable: 'DOCKER_PASSWORD', usernameVariable: 'DOCKER_USERNAME')]) {
                        sh "echo \$DOCKER_PASSWORD | docker login -u \$DOCKER_USERNAME --password-stdin"
                    }
                }
            }
        }
        
        
        stage('Tag and Push Image') {
            steps {
                script {
                    // Push the Docker image to Docker Hub
                    sh "docker tag ${DOCKER_IMAGE_NAME} ${DOCKERHUB_USERNAME}/jenkins-pipeline:latest"
                    sh "docker push ${DOCKERHUB_USERNAME}/${DOCKER_IMAGE_NAME}:latest"
                }
            }
        }
        
        
        stage('Deploy to Remote Server') {
            steps {
                script {
                    // Use the SSH Agent plugin to set up the SSH key
                    sshagent([SERVER_CREDENTIALS_ID]) {
                        sh """
                        ssh -o StrictHostKeyChecking=no root@${REMOTE_SERVER} '
                        # Pull the Docker image from Docker Hub
                        
                        docker stop ${APP_CONTAINER_NAME}
                        docker rm ${APP_CONTAINER_NAME}
                        docker pull ${DOCKERHUB_USERNAME}/${DOCKER_IMAGE_NAME}:latest

                        # Run the Docker container with specified settings
                        docker run -d --name ${APP_CONTAINER_NAME} -p ${APP_CONTAINER_PORT}:8080 -v /data:/data --restart always ${DOCKERHUB_USERNAME}/${DOCKER_IMAGE_NAME}:latest
                        '
                        """
                    }
                }
            }
        }
    }

    post {
        always {
            cleanWs()  // Clean workspace after build
        }
    }
}