diff --git a/.github/workflows/develop.yml b/.github/workflows/develop.yml index fa1452a0..018c485e 100644 --- a/.github/workflows/develop.yml +++ b/.github/workflows/develop.yml @@ -4,7 +4,6 @@ on: pull_request: branches: [ "develop" ] - permissions: contents: read @@ -14,13 +13,32 @@ jobs: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 - - name: Set up JDK 11 + - name: 'Checkout' + uses: actions/checkout@v3 + + - name: 'Set up JDK 17' uses: actions/setup-java@v3 with: java-version: '17' distribution: 'temurin' - - name: Build with Gradle - uses: gradle/gradle-build-action@0d13054264b0bb894ded474f08ebb30921341cee + + - name: Gradle cache + uses: actions/cache@v3 with: - arguments: clean build -x test + path: | + ~/.gradle/caches + ~/.gradle/wrapper + key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }} + restore-keys: | + ${{ runner.os }}-gradle + + + - name: Create application.yml + run: | + mkdir -p ./src/main/resources + touch ./application.yml + echo "${{ secrets.YML }}" > ./src/main/resources/application.yml + cat ./src/main/resources/application.yml + + - name: 'Build with Gradle' + run: ./gradlew build -x test diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 538ff036..2b02f810 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -1,71 +1,81 @@ -name: Spring Boot & Gradle CI/CD +name: CICD-MAIN on: push: - branches: [ main ] + branches: [ "main" ] - -# 본인이 설정한 값을 여기서 채워넣습니다. -# 리전, 버킷 이름, CodeDeploy 앱 이름, CodeDeploy 배포 그룹 이름 env: AWS_REGION: ap-northeast-2 - S3_BUCKET_NAME: favoriteplace-bucket - CODE_DEPLOY_APPLICATION_NAME: favorite-place-app - CODE_DEPLOY_DEPLOYMENT_GROUP_NAME: favorite-deploy-group - -permissions: - contents: read + ECR_REGISTRY: 859043921675.dkr.ecr.ap-northeast-2.amazonaws.com + ECR_REPOSITORY: moemoe + IMAGE_TAG: ${{ github.sha }} jobs: - deploy: - name: Deploy + ci: runs-on: ubuntu-latest - environment: production steps: - # (1) 기본 체크아웃 - - name: Checkout - uses: actions/checkout@v3 + - name: Checkout source code + uses: actions/checkout@v3 + + - name: Set up JDK 17 + uses: actions/setup-java@v3 + with: + distribution: 'temurin' + java-version: '17' + + - name: Gradle Cache + uses: actions/cache@v3 + with: + path: | + ~/.gradle/caches + ~/.gradle/wrapper + key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }} + restore-keys: | + ${{ runner.os }}-gradle- - # (2) JDK 11 세팅 - - name: Set up JDK 17 - uses: actions/setup-java@v3 - with: - distribution: 'temurin' - java-version: '17' + - name: Create application.yml + run: | + mkdir -p ./src/main/resources + touch ./application.yml + echo "${{ secrets.YML }}" > ./src/main/resources/application.yml + cat ./src/main/resources/application.yml - # (3) Gradle build (Test 제외) - - name: Grant execute permission for gradlew - run: chmod +x ./gradlew + - name: Build with Gradle + run: | + chmod +x ./gradlew + ./gradlew clean build -x test --parallel --build-cache --daemon - - name: Build with Gradle - uses: gradle/gradle-build-action@0d13054264b0bb894ded474f08ebb30921341cee - with: - arguments: clean build -x test + - name: 🔐 Configure AWS credentials + uses: aws-actions/configure-aws-credentials@v1 + with: + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + aws-region: ${{ env.AWS_REGION }} - # (4) AWS 인증 (IAM 사용자 Access Key, Secret Key 활용) - - name: Configure AWS credentials - uses: aws-actions/configure-aws-credentials@v1 - with: - aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} - aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} - aws-region: ${{ env.AWS_REGION }} + - name: 🔑 Login to Amazon ECR + id: login-ecr + uses: aws-actions/amazon-ecr-login@v1 - # (5) 빌드 결과물을 S3 버킷에 업로드 - - name: Upload to AWS S3 - run: | - aws deploy push \ - --application-name ${{ env.CODE_DEPLOY_APPLICATION_NAME }} \ - --ignore-hidden-files \ - --s3-location s3://$S3_BUCKET_NAME/$GITHUB_SHA.zip \ - --source . + - name: 🐳 Build, tag, and push Docker image + run: | + docker build -t $ECR_REPOSITORY:$IMAGE_TAG . + docker tag $ECR_REPOSITORY:$IMAGE_TAG $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG + docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG + + cd: + needs: ci + runs-on: ubuntu-latest + + steps: + - name: 🚀 Deploy via SSH + uses: appleboy/ssh-action@master + with: + host: ${{ secrets.SERVER_IP }} + username: ${{ secrets.SERVER_USER }} + key: ${{ secrets.SERVER_KEY }} + script: | + cd ~ + ./deploy.sh ${{ github.sha }} + docker image prune -f - # (6) S3 버킷에 있는 파일을 대상으로 CodeDeploy 실행 - - name: Deploy to AWS EC2 from S3 - run: | - aws deploy create-deployment \ - --application-name ${{ env.CODE_DEPLOY_APPLICATION_NAME }} \ - --deployment-config-name CodeDeployDefault.AllAtOnce \ - --deployment-group-name ${{ env.CODE_DEPLOY_DEPLOYMENT_GROUP_NAME }} \ - --s3-location bucket=$S3_BUCKET_NAME,key=$GITHUB_SHA.zip,bundleType=zip \ - --file-exists-behavior OVERWRITE diff --git a/DockerFile b/DockerFile new file mode 100644 index 00000000..102e2196 --- /dev/null +++ b/DockerFile @@ -0,0 +1,12 @@ +FROM eclipse-temurin:17-jdk-alpine + +WORKDIR /app + +COPY build/libs/favoriteplace-0.0.1-SNAPSHOT.jar ./app.jar + +RUN apk add --no-cache tzdata && \ + cp /usr/share/zoneinfo/Asia/Seoul /etc/localtime && \ + echo "Asia/Seoul" > /etc/timezone && \ + apk del tzdata + +CMD ["java", "-Duser.timezone=Asia/Seoul", "-jar", "-Dspring.profiles.active=prod", "app.jar"] diff --git a/build.gradle b/build.gradle index e950970c..8faf7c1f 100644 --- a/build.gradle +++ b/build.gradle @@ -79,6 +79,9 @@ dependencies { // Jackson (Date/Time Serialization) implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310' + + //actuator + implementation 'org.springframework.boot:spring-boot-starter-actuator' } diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 00000000..1b9de434 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,40 @@ +services: + redis: + container_name: redis + image: redis:alpine + ports: + - "6379:6379" + networks: + - app-network + + blue: + container_name: moemoe-blue + image: 859043921675.dkr.ecr.ap-northeast-2.amazonaws.com/moemoe:latest + expose: + - 8080 + ports: + - "8081:8080" + environment: + - TZ=Asia/Seoul + depends_on: + - redis + networks: + - app-network + + green: + container_name: moemoe-green + image: 859043921675.dkr.ecr.ap-northeast-2.amazonaws.com/moemoe:latest + expose: + - 8080 + ports: + - "8082:8080" + environment: + - TZ=Asia/Seoul + depends_on: + - redis + networks: + - app-network + +networks: + app-network: + driver: bridge