Spring Boot CI/CD (EC2 + Jenkins)

형석이의 성장일기·2023년 10월 9일
0

이제 본격적으로 배포에 사용할 Item(pipeline)을 구축하자

백엔드 배포용 pipeline

pipeline {
    agent any
    stages {
        stage('github clone') {
            steps {
                git branch: '배포할 브랜치', credentialsId: 'jenkins_token', url: 'Gitlab 레포지토리 https 주소'
            }
        }
        stage('Build') {
            steps {
                dir("./프로젝트 폴더명") {
                    sh "chmod +x ./gradlew"
                    sh "./gradlew clean build"
                }
            }
        }
        stage('Deployment') {
            steps {
                sshagent(credentials: ['ssh_key']) {
                    sh '''
                        ssh -o StrictHostKeyChecking=no ubuntu@ec2_public_ip
                        scp /var/jenkins_home/workspace/back/프로젝트 폴더명/build/libs/프로젝트 폴더명-0.0.1-SNAPSHOT.jar ubuntu@ec2_public_ip:/home/ubuntu/Backend
                        ssh -t ubuntu@ec2_public_ip ./deploy_be.sh
                    '''
                }
            }
        }
    }
}

/home/ubuntu/Backend 디렉토리 생성

mkdir Backend

/home/ubuntu/deploy_be.sh

cd Backend
sudo docker compose down
sudo docker compose up -d --build
yes | sudo docker system prune

/home/ubuntu/Backend/Dockerfile

FROM adoptopenjdk/openjdk11

# DB
ENV DB_URL=
ENV DB_USERNAME=
ENV DB_PASSWORD=

# jwt
ENV JWT_SECRET_KEY=Z29nby10bS1zZXJ2ZXItZGxyamVvYW9yb3JodG9kZ290c3Atam9vbmdhbmduaW0teWVvbHNpbWhpaGFsZ2VveW8K

# Social Login
ENV KAKAO_CLIENT_ID=

# S3
ENV S3_BUCKET=
ENV CREDENTIALS_ACCESS_KEY=
ENV CREDENTIALS_SECRET_KEY=
ENV REGION_STATIC=ap-northeast-2

COPY ./mt-0.0.1-SNAPSHOT.jar /mt-0.0.1-SNAPSHOT.jar
CMD ["java", "-jar", "mt-0.0.1-SNAPSHOT.jar"]

/home/ubuntu/Backend/docker-compose.yml

version: '3'
services:
        spring-boot-app:
                build:
                        context: .
                        dockerfile: Dockerfile
                image: mt-back
                container_name: mt-back
                ports:
                        - "9090:9090"

GitLab webhook 설정

profile
이사중 .. -> https://gudtjr2949.tistory.com/

0개의 댓글