alswp006 님이 안전한 merge을 위해 test를 하고 있습니다! 🚀 #33
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Bami Backend CI/CD | |
run-name: ${{ github.actor }} 님이 안전한 merge을 위해 test를 하고 있습니다! 🚀 | |
on: | |
push: | |
branches: [ "develop" ] | |
pull_request: | |
branches: [ "develop" ] | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
permissions: | |
contents: read | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Set up JDK 17 | |
uses: actions/setup-java@v4 | |
with: | |
java-version: '17' | |
distribution: 'temurin' | |
- name: Download API key from private repository | |
uses: actions/checkout@v2 | |
with: | |
repository: ktb1-eight/api-key | |
token: ${{ secrets.TOKEN_GITHUB }} | |
path: api-key | |
fetch-depth: 1 #최신 버전 커밋을 가져오기 위해 추가 | |
- name: Copy application-dev.yml | |
run: | | |
cp -f api-key/application-dev.yml src/main/resources/ | |
- name: Setup Gradle | |
uses: gradle/actions/setup-gradle@af1da67850ed9a4cedd57bfd976089dd991e2582 # v4.0.0 | |
- name: Run chmod to make gradlew executable | |
run: chmod +x ./gradlew | |
- name: Build Docker image | |
run: docker build -t bami-backend:latest . | |
- name: Save Docker image as tar | |
run: docker save -o bami-backend.tar bami-backend:latest | |
- name: Upload Docker image tar to S3 | |
env: | |
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_BACKEND }} | |
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY_BACKEND }} | |
run: aws s3 cp bami-backend.tar s3://bami-practice-s3/path/to/bami-backend.tar --region ap-northeast-2 | |
- name: Add SSH key | |
uses: webfactory/[email protected] | |
with: | |
ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }} | |
- name: Test SSH connection | |
run: ssh -T [email protected] || exit 0 | |
- name: Deploy to EC2 | |
env: | |
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_BACKEND }} | |
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY_BACKEND }} | |
run: | | |
ssh -o StrictHostKeyChecking=no ${{ secrets.SERVER_USER }}@${{ secrets.SERVER_IP }} << 'EOF' | |
set -e # Exit immediately if a command exits with a non-zero status | |
echo "Downloading Docker image tar from S3..." | |
export AWS_ACCESS_KEY_ID=${{ secrets.AWS_ACCESS_KEY_BACKEND }} | |
export AWS_SECRET_ACCESS_KEY=${{ secrets.AWS_SECRET_ACCESS_KEY_BACKEND }} | |
aws s3 cp s3://bami-practice-s3/path/to/bami-backend.tar /home/ec2-user/bami-backend.tar --region ap-northeast-2 | |
echo "Loading Docker image..." | |
docker load -i /home/ec2-user/bami-backend.tar | |
echo "Stopping existing Docker container if it exists..." | |
docker stop bami-backend || true | |
docker rm bami-backend || true | |
echo "Running new Docker container..." | |
docker run -d -p 8080:8080 --name bami-backend \ | |
-e DB_URL=$(aws ssm get-parameter --name "/bami/DB_URL" --with-decryption --query "Parameter.Value" --output text) \ | |
-e DB_USERNAME=$(aws ssm get-parameter --name "/bami/DB_USERNAME" --query "Parameter.Value" --output text) \ | |
-e DB_PASSWORD=$(aws ssm get-parameter --name "/bami/DB_PASSWORD" --with-decryption --query "Parameter.Value" --output text) \ | |
-e EC2_PUBLIC_DNS=$(aws ssm get-parameter --name "/bami/PUBLIC_DNS_ADDR" --query "Parameter.Value" --output text) \ | |
bami-backend:latest | |
echo "Deployment completed successfully." | |
EOF | |
- name: Build with Gradle | |
run: ./gradlew build -x test | |
shell: bash |