Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: dev-aws-CI-CD.yml #30

Merged
merged 12 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
159 changes: 159 additions & 0 deletions .github/workflows/dev-aws-CI-CD.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
name: dev - CI/CD to Amazon ECS

on:
push:
branches: [ "develop" ]
pull_request:
branches: [ "develop" ]

permissions:
contents: read
checks: write
issues: write

jobs:
build:
runs-on: ubuntu-latest
environment: dev
if: github.event_name == 'push' || github.event_name == 'pull_request'
env:
jasypt.encryptor.password: ${{ secrets.JASYPT_ENCRYPTOR_PASSWORD }}
DOCKER_REPOSITORY: ${{ secrets.DOCKER_REPOSITORY }}
DOCKER_REPOSITORY_NGINX: ${{ secrets.DOCKER_REPOSITORY_NGINX }}
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'temurin'

- name: Gradle Caching
uses: actions/cache@v3
with:
path: |
~/.gradle/caches
~/.gradle/wrapper
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
restore-keys: |
${{ runner.os }}-gradle-

- name: Grant Execute Permission For Gradlew
run: chmod +x gradlew

- name: Set up Environment Variables
run: |
echo "JASYPT_ENCRYPTOR_PASSWORD=${{ secrets.JASYPT_ENCRYPTOR_PASSWORD }}" >> $GITHUB_ENV
echo "DOCKER_REPOSITORY=${{ secrets.DOCKER_REPOSITORY }}" >> $GITHUB_ENV
echo "DOCKER_REPOSITORY_NGINX=${{ secrets.DOCKER_REPOSITORY_NGINX }}" >> $GITHUB_ENV

- name: Build With Gradle
run: ./gradlew build -x test --warning-mode all

## Build and test
- name: 테스트 코드 실행
run: ./gradlew --info test

- name: Publish Unit Test Results
uses: EnricoMi/publish-unit-test-result-action@v1
if: ${{ always() }}
with:
files: build/test-results/**/*.xml

- name: Publish Test Report
uses: mikepenz/action-junit-report@v3
if: success() || failure()
with:
report_paths: '**/build/test-results/test/TEST-*.xml'

deploy:
name: Deploy
runs-on: ubuntu-latest
environment: dev
if: github.event_name == 'push'
needs: build
env:
JASYPT_ENCRYPTOR_PASSWORD: ${{ secrets.JASYPT_ENCRYPTOR_PASSWORD }}
DOCKER_REPOSITORY: ${{ secrets.DOCKER_REPOSITORY }}
DOCKER_REPOSITORY_NGINX: ${{ secrets.DOCKER_REPOSITORY_NGINX }}
steps:
- name: Send docker-compose.yml
uses: appleboy/scp-action@master
with:
username: ubuntu
host: ${{ secrets.AWS_DEV_HOSTNAME }}
key: ${{ secrets.AWS_DEV_PRIVATE_KEY }}
source: "./docker-compose.yml"
target: "/home/ubuntu/"

- name: Send nginx.conf
uses: appleboy/scp-action@master
with:
username: ubuntu
host: ${{ secrets.AWS_DEV_HOSTNAME }}
key: ${{ secrets.AWS_DEV_PRIVATE_KEY }}
source: "./nginx/nginx.conf"
target: "/home/ubuntu/"

## Docker login
- name: Docker Login
run: |
docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }}

## Nginx 도커 이미지 빌드 후 도커허브에 push하기
- name: Docker build & Push for Nginx
run: |
docker build -f dockerfile-nginx -t ${{ secrets.DOCKER_REPOSITORY_NGINX }} .
docker push ${{ secrets.DOCKER_REPOSITORY_NGINX }}

## Spring Boot 도커 이미지 빌드 후 도커허브에 push하기
- name: Docker build & Push for Spring Boot
run: |
docker build -t ${{ secrets.DOCKER_REPOSITORY }} .
docker push ${{ secrets.DOCKER_REPOSITORY }}

# SSH로 서버에 연결
- name: Connect to Server
uses: appleboy/ssh-action@master
with:
username: ubuntu
host: ${{ secrets.AWS_DEV_HOSTNAME }}
key: ${{ secrets.AWS_DEV_PRIVATE_KEY }}
script: | # SSH 연결 후 실행할 명령어들
docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }}
export DOCKER_REPOSITORY=${{ secrets.DOCKER_REPOSITORY }}
export DOCKER_REPOSITORY_NGINX=${{ secrets.DOCKER_REPOSITORY_NGINX }}
export JASYPT_ENCRYPTOR_PASSWORD=${{ secrets.JASYPT_ENCRYPTOR_PASSWORD }}
# Pull 최신 이미지
docker-compose -f /home/ubuntu/docker-compose.yml pull
# 새 컨테이너 실행
docker-compose -f /home/ubuntu/docker-compose.yml up -d --build

discord-notify:
name: Discord Notify
runs-on: ubuntu-latest
environment: dev
needs: [ build, deploy ]
if: always()
env:
jasypt.encryptor.password: ${{ secrets.JASYPT_ENCRYPTOR_PASSWORD }}
steps:
- name: Send Discord Notification
uses: sarisia/actions-status-discord@v1
with:
webhook: ${{ secrets.DISCORD_WEBHOOK_URL }}
status: ${{ job.status }}
title: "CI/CD Pipeline Status"
description: |
${{ format(
'The CI/CD pipeline has completed.\n- **Build Job Status**: {0}\n- **Deploy Job Status**: {1}\n- **Branch**: {2}\n- **Commit**: {3}\n- **Author**: {4}',
needs.build.result,
needs.deploy.result,
github.ref,
github.sha,
github.actor
) }}
url: "https://github.com/sarisia/actions-status-discord"
username: GitHub Actions Bot
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ ARG JAR_FILE=/build/libs/clothstar-0.0.1-SNAPSHOT.jar

COPY ${JAR_FILE} app.jar

ENTRYPOINT ["java","-jar","/app.jar"]
ENTRYPOINT ["java","-Dspring.profiles.active=dev","-jar","/app.jar"]
2 changes: 2 additions & 0 deletions Dockerfile-nginx
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
FROM nginx
COPY ./nginx/conf.d/nginx.conf /etc/nginx/conf.d
26 changes: 26 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
version: '3'

services:

application:
container_name: clothstar-springboot-dev
image: ${DOCKER_REPOSITORY}:latest
environment:
- JASYPT_ENCRYPTOR_PASSWORD=${JASYPT_ENCRYPTOR_PASSWORD}
build:
context: ./
dockerfile: Dockerfile
ports:
- "8080:8080"
restart: on-failure
networks:
- preon_net


# nginx:
# container_name: nginx
# image: ${DOCKER_REPOSITORY_NGINX}:latest
# ports:
# - 80:80
# depends_on:
# - application
13 changes: 13 additions & 0 deletions nginx/conf.d/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
server {
listen 80;
server_name *.compute.amazonaws.com
access_log off;

location / {
proxy_pass http://clothstar:8080;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Forwarded-Host $server_name;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,19 @@ package org.store.clothstar.common.config
import org.jasypt.encryption.StringEncryptor
import org.jasypt.encryption.pbe.PooledPBEStringEncryptor
import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig
import org.springframework.beans.factory.annotation.Value
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration

@Configuration
class JasyptConfig {

@Value("\${jasypt.encryptor.password}") // 환경 변수 또는 application.yml에서 가져오기
private lateinit var key: String

@Bean("jasyptStringEncryptor")
fun stringEncryptor(): StringEncryptor {
val key = "my_jasypt_key"

val encryptor = PooledPBEStringEncryptor()
val config = SimpleStringPBEConfig()

Expand Down
1 change: 1 addition & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
jasypt:
encryptor:
bean: jasyptStringEncryptor
password: ${ JASYPT_ENCRYPTOR_PASSWORD}

spring:
profiles:
Expand Down
Loading