Skip to content

Update deploy.yml

Update deploy.yml #11

Workflow file for this run

name: CI/CD Deployment
on:
push:
branches:
- main # main 브랜치에 푸시될 때 트리거
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
distribution: 'temurin'
java-version: '17'
- name: Grant execute permission for gradlew
run: chmod +x ./gradlew
- name: Build project
env:
SERVER_SSL_KEY_STORE: ${{ secrets.SERVER_SSL_KEY_STORE }}
SERVER_SSL_KEY_STORE_PASSWORD: ${{ secrets.SERVER_SSL_KEY_STORE_PASSWORD }}
JWT_SECRET_KEY: ${{ secrets.JWT_SECRET_KEY }}
DATABASE_URL: ${{ secrets.DATABASE_URL }}
DATABASE_USERNAME: ${{ secrets.DATABASE_USERNAME }}
DATABASE_PASSWORD: ${{ secrets.DATABASE_PASSWORD }}
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
run: ./gradlew clean build
- name: Copy JAR to remote server
env:
SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }}
run: |
set -x # 명령어를 실행할 때마다 출력
mkdir -p ~/.ssh
echo "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa
chmod 600 ~/.ssh/id_rsa
echo -e "Host mykeyhost\n User ubuntu\n HostName 13.209.198.1\n IdentityFile ~/.ssh/id_rsa\n StrictHostKeyChecking no" > ~/.ssh/config
ls -l ~/.ssh # SSH 디렉토리 확인
scp build/libs/demo-0.0.1-SNAPSHOT.jar mykeyhost:~/
- name: Deploy application
env:
SERVER_SSL_KEY_STORE: ${{ secrets.SERVER_SSL_KEY_STORE }}
SERVER_SSL_KEY_STORE_PASSWORD: ${{ secrets.SERVER_SSL_KEY_STORE_PASSWORD }}
JWT_SECRET_KEY: ${{ secrets.JWT_SECRET_KEY }}
DATABASE_URL: ${{ secrets.DATABASE_URL }}
DATABASE_USERNAME: ${{ secrets.DATABASE_USERNAME }}
DATABASE_PASSWORD: ${{ secrets.DATABASE_PASSWORD }}
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
run: |
set -x # 명령어를 실행할 때마다 출력
ssh mykeyhost << 'EOF'
sudo pkill -f 'java -jar' || true # 기존 실행 중인 서버 프로세스 종료
sudo nohup java -jar ~/demo-0.0.1-SNAPSHOT.jar --server.port=443 --server.ssl.key-store=$SERVER_SSL_KEY_STORE --server.ssl.key-store-password=$SERVER_SSL_KEY_STORE_PASSWORD --jwt.secret-key=$JWT_SECRET_KEY --spring.datasource.url=$DATABASE_URL --spring.datasource.username=$DATABASE_USERNAME --spring.datasource.password=$DATABASE_PASSWORD --cloud.aws.credentials.accessKey=$AWS_ACCESS_KEY_ID --cloud.aws.credentials.secretKey=$AWS_SECRET_ACCESS_KEY > app.log 2>&1 & # 새로 배포된 애플리케이션 실행
EOF