샤인의 IT (막 적는) 메모장

[Jenkins] DSL & 파이프라인 본문

DevOps

[Jenkins] DSL & 파이프라인

신샤인 2022. 11. 8. 20:47
반응형

노션에서 정리한 내용 정리

 

 

DSL

  • DSL 플러그인 설치
    • Job DSL 플러그인 설치
    • Job 생성 후 Build에서 확인 가능

 

 

Jenkins Job DSL Plugin

 

jenkinsci.github.io

 

  • 문법
    • Job : Job생성
    • Description : Job 프로젝트 Description 추가
    • Param
    • scm
    • triggers
    • steps
    • mailer
    job('job_dsl_example2') {
    			description('This is my awesome job')
    
    			parameters {
    				stringParam('Planet', defaultValue = 'world', description = 'this is the world')
    				booleanParam('FLAG', true)
    				choiceParam('OPTION', ['option 1 (default)', 'option 2', 'option 3'])
    			}
    
    			scm {
    				git('<https://github.com/jenkins-docs/simple-java-maven-app>', 'master')
    			}
    
    			triggers {
    					cron('H 5 * * 7')
    			}
    
    			steps {
    					shell("""
    							echo 'hello world'
    							echo 'Running'
    								""")
    
    			publishers {
    					mailer('me@example.com', true, true)
    }
    

 

Pipeline

 

 

  • Jenkinsfile
    • Pipeline 플러그인 설치
      • Default로 설치되어 있음~
      • Job → Pipeline 생성
    pipeline {
        agent any
    
        stages {
            stage('Build') { 
                steps {
                    echo 'Building...' 
                }
            }
            stage('Test') { 
                steps {
                    echo 'Testing...' 
                }
            }
            stage('Deploy') { 
                steps {
                    echo 'Deploying...' 
                }
            }
        }
    }
    
    pipeline {
        agent any
    
        stages {
            stage('Build') { 
                steps {
                    sh 'echo "My first pipeline"'
                    sh '''
                        echo "By the way, I can do more stuff in here"
                        ls -alh
                        '''
                }
            }
    		}
    }
    
    pipeline {
        agent any
    
        stages {
            stage('Timeout') { 
                steps {
                    retry(3) {
    						sh 'I am not going to work :c'
    				}
                }
            }
    	}
    }
    
    • 환경변수 지정
    pipeline {
        agent any
    	
    		environment {
    				NAME = 'Shin'
    				LASTNAME = 'JS'
    		}
    
        stages {
            stage('Build') { 
                steps {
                    sh 'echo $NAME $LASTNAME'
                }
            }
    		}
    }
    
    • Credential 지정
    pipeline {
        agent any
    	
    		environment {
    				secret = credentials('TEST')
    		}
    
        stages {
            stage('Build') { 
                steps {
                    sh 'echo $secret'
                }
            }
    		}
    }
    
    • Post Action 지정
    pipeline {
        agent any
    
        stages {
            stage('Test') { 
                steps {
                    sh 'echo "I am good"'
                }
            }
    	}
    		post {
    			always {
    					echo 'I will always get executed :D'
    			}
    			success {
    					echo 'I will only get executed  if this success'
    			}
    			failure {
    					echo 'I will always get executed if this fails'
    			}
    			unstable {
    					echo 'I will always get executed if this is unstable'
    			}
    		}
    }
    
  • Jenkinsfile (Declarative Pipeline) pipeline { agent any stages { stage('Build') { steps { // } } stage('Test') { steps { // } } stage('Deploy') { steps { // } } } }

Docker + Jenkins Pipeline + Maven 예제

  • Build → Test → Push → Deploy 순서
  • Docker in Docker 구성 후
#Dockerfile
FROM jenkins/jenkins:centos7

USER root

#Install Ansible
RUN     curl -O <https://bootstrap.pypa.io/pip/2.7/get-pip.py> && \\
        python get-pip.py && \\
        pip install ansible --upgrade

#Install Docker

RUN     yum -y update && yum install -y yum-utils && \\
        yum-config-manager --add-repo <https://download.docker.com/linux/centos/docker-ce.repo> && \\
        yum install -y docker-ce docker-ce-cli containerd.io

#Install Compose
RUN     curl -L "<https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$>(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose && \\
        chmod +x /usr/local/bin/docker-compose

RUN     usermod -aG docker jenkins

USER jenkins

#docker-compose.yml
services:
  jenkins:
    container_name: jenkins
    image: jenkins/docker
    build:
      context: pipeline
    ports:
    - "8080:8080"
    volumes:
    - "$PWD/jenkins_data:/var/jenkins_home"
    - "/var/run/docker.sock:/var/run/docker.sock" #docker 소켓 주목
    environment:
    - "TZ=Asia/Seoul"
    networks:
    - net

#권한 부여 후 -> 젠킨스 컨테이너에서 Docker 명령어 사용 시 Host 컨테이너 정보 확인 가능
chown 1000:1000 /var/run/docker.sock
  • Pipeline 설정
#Jenkinsfile
pipeline {
    agent any

    stages {
        stage('Build') { 
            steps {
                sh '''
									echo Build
								
										''' 
            }
        }
        stage('Test') { 
            steps {
                sh 'echo Testing...' 
            }
        }
        stage('Push') { 
            steps {
                sh 'echo Deploying...' 
            }
        }
        stage('Deploy') { 
            steps {
                sh 'echo Deploying...' 
            }
        }
    }
}
  • Git 소스 설정
git clone <https://github.com/jenkins-docs/simple-java-maven-app.git>

cp simple-app~ pipeline/java-app

  • Maven Container 설정
docker pull maven:3.6.0-alpine

#JAVA APP 경로 볼륨 마운트
docker run -it --name mvn -v $PWD/java-app:/app -v /root/.m2/:/root/.m2 -w maven:3.6.0-alpine mvn -B -DskipTests clean package

#Shell Script Troubleshoot
#Workdir 지정 시 Run 바로 뒤에 붙여서 넣어줘야 함. 맨 뒷쪽에 추가할 경우 Exec 명령어 중간에서 오류 발생
#!/bin/bash

echo "**************************************************************"
echo "Building jar File!!!!!!!!!!!"
echo "**************************************************************"
docker run --rm -w /app -v $PWD/java-app:/app -v /root/.m2/:/root/.m2 maven:3.6.0-alpine "$@"
  • openjdk Build 컨테이너 설정
#Dockerfile-Java
FROM openjdk:18-jdk-alpine

RUN mkdir /app
COPY *.jar /app/app.jar

CMD java -jar /app/app.jar

#Build
docker build -f Dockerfile -t test .

#Docker-compose.yml
[vagrant@jenkins build]$ cat docker-compose.yml 
version: '3'
services:
  app:
    image: "app:$BUILD_TAG"
    build:
      context: .
      dockerfile: Dockerfile-Java

docker-compose -f docker-compose.yml build
  • 이미지 Build 스크립트 설정
#!/bin/bash

cp -f java-app/target/*.jar jenkins/build/

echo "***************************************************"
echo "Building Docker Image!"

cd jenkins/build && docker-compose -f docker-compose.yml build --no-cache
  • Jenkinsfile 설정
pipeline {
    agent any

    stages {
        stage('Build') {
            steps {
                sh '''
                ./jenkins/build/mvn.sh mvn -B -DskipTests clean package
                ./build.sh

                '''
            }
        }
        stage('Test') {
            steps {
                sh '''
								./jenkins/build/mvn.sh mvn test
								'''
            }
        }
        stage('Push') {
            steps {
                sh './jenkins/push/push.sh'
            }
        }
        stage('Deploy') {
            steps {
                sh './jenkins/deploy/deploy.sh'
            }
        }
    }
}
  • Image Push
#Push.sh

#!/bin/bash

cp -f java-app/target/*.jar jenkins/build/

echo "***************************************************"
echo "pushing Docker Image!"

echo "Docker Login!"
docker login -u id -p $PASS

echo "Tagging Image!"
docker tag $IMAGE:$BUILD_TAG id/$IMAGE:$BUILD_TAG

echo "pushing Image!"
docker push id/$IMAGE:$BUILD_TAG
  • Image Deploy
#Deploy.sh

#!/bin/bash

echo $IMAGE > /tmp/.auth
echo $BUILD_TAG >> /tmp/.auth
echo $PASS >> /tmp/.auth

scp -i /opt/prod /tmp/.auth user@IP:/tmp/.auth

scp -i /opt/prod ./jenkins/deploy/publish user@IP:/tmp/publish

scp -i /opt/prod user@IP "/tmp/publish"
  • 배포 서버 설정
#docker-compose.yml
version: '3'
services:
  maven:
    image: id/$IMAGE:$TAG
		container_name: maven-app

#!/bin/bash

export IMAGE=$(sed -n '1p' /tmp/.auth)
export BUILD_TAG=$(sed -n '2p' /tmp/.auth)
export $PASS=$(sed -n '3p' /tmp/.auth)

docker login -u id -p $PASS
cd ~/maven && docker-compose up -d

반응형

'DevOps' 카테고리의 다른 글

[Jenkins] 기본 설정 정리  (0) 2022.11.08
[Jenkins] 컨테이너 구성 및 기본 job 테스트  (0) 2022.11.08
[DevOps] DevOps 기본 개념 정리  (0) 2022.01.15
Comments