...
Jenkins 설치 및 실행 with Docker-compose
https://github.com/Sanses/devops_06_03_jenkins/blob/master/src/jenkins_local/docker-compose.yml
Code Block version: '3.9' services: jenkins: image: jenkins/jenkins:latest #2.60.3 container_name: jenkins environment: - "TZ=Asia/Seoul" ports: - "8080:8080" volumes: - "./data:/var/jenkins_home"
...
Jenkins Pipeline Overview
Jenkins 설치 with Docker
https://github.com/Sanses/devops_06_03_jenkins/blob/master/src/jenkins_remote_docker/Dockerfile
Code Block title Dockerfile FROM jenkins/jenkins:lts ARG DOCKER_GID=1000 USER root # docker install COPY scripts/install_docker.sh /install_docker.sh RUN chmod +x /install_docker.sh && \ /install_docker.sh # aws cli install COPY scripts/install_aws.sh /install_aws.sh RUN chmod +x /install_aws.sh && \ /install_aws.sh # set jenkins user to host docker group RUN /usr/sbin/groupadd -g ${DOCKER_GID:-1000} -f docker && \ /usr/sbin/usermod -aG docker jenkins USER jenkins
Code Block title docker-compose.yml version: '3.8' services: jenkins: build: context: "" args: DOCKER_GID: ${DOCKER_GID} container_name: jenkins environment: TZ: "Asia/Seoul" JAVA_OPTS: "-Dhudson.model.DownloadService.noSignatureCheck=true" DOCKER_GID: ${DOCKER_GID} ports: - "8080:8080" volumes: - "./data:/var/jenkins_home" - "/var/run/docker.sock:/var/run/docker.sock"
Jenkins Pipeline - Jenkinsfile
https://github.com/Sanses/devops_sample_app_python/blob/master/deploy/Jenkinsfile
Code Block pipeline { agent { label 'master' } parameters { booleanParam(name : 'BUILD_DOCKER_IMAGE', defaultValue : true, description : 'BUILD_DOCKER_IMAGE') booleanParam(name : 'RUN_TEST', defaultValue : true, description : 'RUN_TEST') booleanParam(name : 'PUSH_DOCKER_IMAGE', defaultValue : true, description : 'PUSH_DOCKER_IMAGE') booleanParam(name : 'PROMPT_FOR_DEPLOY', defaultValue : false, description : 'PROMPT_FOR_DEPLOY') booleanParam(name : 'DEPLOY_WORKLOAD', defaultValue : true, description : 'DEPLOY_WORKLOAD') // CI string(name : 'AWS_ACCOUNT_ID', defaultValue : '657976307134', description : 'AWS_ACCOUNT_ID') string(name : 'DOCKER_IMAGE_NAME', defaultValue : 'demo', description : 'DOCKER_IMAGE_NAME') string(name : 'DOCKER_TAG', defaultValue : '1.0.0', description : 'DOCKER_TAG') // CD string(name : 'TARGET_SVR_USER', defaultValue : 'ec2-user', description : 'TARGET_SVR_USER') string(name : 'TARGET_SVR_PATH', defaultValue : '/home/ec2-user/', description : 'TARGET_SVR_PATH') string(name : 'TARGET_SVR', defaultValue : '10.0.3.61', description : 'TARGET_SVR') } environment { REGION = "ap-northeast-2" ECR_REPOSITORY = "${params.AWS_ACCOUNT_ID}.dkr.ecr.ap-northeast-2.amazonaws.com" ECR_DOCKER_IMAGE = "${ECR_REPOSITORY}/${params.DOCKER_IMAGE_NAME}" ECR_DOCKER_TAG = "${params.DOCKER_TAG}" } stages { stage('============ Build Docker Image ============') { when { expression { return params.BUILD_DOCKER_IMAGE } } agent { label 'build' } steps { dir("${env.WORKSPACE}") { sh 'docker build -t ${ECR_DOCKER_IMAGE}:${ECR_DOCKER_TAG} .' } } post { always { echo "Docker build success!" } } } stage('============ Run test code ============') { when { expression { return params.RUN_TEST } } agent { label 'build' } steps { sh''' aws ecr get-login-password --region ${REGION} | docker login --username AWS --password-stdin ${ECR_REPOSITORY} docker run --rm ${ECR_DOCKER_IMAGE}:${ECR_DOCKER_TAG} /root/.local/bin/pytest -v ''' } } stage('============ Push Docker Image ============') { when { expression { return params.PUSH_DOCKER_IMAGE } } agent { label 'build' } steps { echo "Push Docker Image to ECR" sh''' aws ecr get-login-password --region ${REGION} | docker login --username AWS --password-stdin ${ECR_REPOSITORY} docker push ${ECR_DOCKER_IMAGE}:${ECR_DOCKER_TAG} ''' } } stage('Prompt for deploy') { when { expression { return params.PROMPT_FOR_DEPLOY } } agent { label 'deploy' } steps { script { env.APPROAL_NUM = input message: 'Please enter the approval number', parameters: [string(defaultValue: '', description: '', name: 'APPROVAL_NUM')] } echo "${env.APPROAL_NUM}" } } stage('============ Deploy workload ============') { when { expression { return params.DEPLOY_WORKLOAD } } agent { label 'deploy' } steps { sshagent (credentials: ['aws_ec2_user_ssh']) { sh """#!/bin/bash scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no \ deploy/docker-compose.yml \ ${params.TARGET_SVR_USER}@${params.TARGET_SVR}:${params.TARGET_SVR_PATH}; ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no \ ${params.TARGET_SVR_USER}@${params.TARGET_SVR} \ 'aws ecr get-login-password --region ${REGION} | docker login --username AWS --password-stdin ${ECR_REPOSITORY}; \ export IMAGE=${ECR_DOCKER_IMAGE}; \ export TAG=${ECR_DOCKER_TAG}; \ docker-compose -f docker-compose.yml down; docker-compose -f docker-compose.yml up -d'; """ } } } } post { failure { slackSend( channel: "#jenkins_test", color: "danger", message: "[Failed] Job:${env.JOB_NAME}, Build num:#${env.BUILD_NUMBER} @channel (<${env.RUN_DISPLAY_URL}|open job detail>)" ) } } }