Merge pull request #21 from CineNorte-WEB/develop #23
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: Deploy to EC2 | |
on: | |
push: | |
branches: | |
- main # develop_action 브랜치에 push할 때 트리거 | |
jobs: | |
deploy: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v3 | |
- name: Set up JDK 17 | |
uses: actions/setup-java@v3 | |
with: | |
java-version: '17' | |
distribution: 'temurin' | |
- name: Build JAR file | |
run: ./gradlew clean build -x test | |
- name: Log in to DockerHub | |
uses: docker/login-action@v2 | |
with: | |
username: ${{ secrets.DOCKER_USERNAME }} | |
password: ${{ secrets.DOCKER_PASSWORD }} | |
- name: Build and push Docker image | |
run: | | |
docker buildx create --use | |
docker buildx build --platform linux/amd64 -t tjdgus3488/camchelin:latest . --push | |
- name: Set permissions for SSH key | |
run: | | |
mkdir -p ~/.ssh | |
echo "${{ secrets.EC2_SSH_KEY }}" > ~/.ssh/camchelin_ec2.pem | |
chmod 400 ~/.ssh/camchelin_ec2.pem | |
- name: Add Host to Known Hosts | |
run: | | |
ssh-keyscan -H ${{ secrets.EC2_HOST }} >> ~/.ssh/known_hosts | |
- name: SSH to EC2 and deploy | |
uses: appleboy/ssh-action@master | |
with: | |
host: ${{ secrets.EC2_HOST }} | |
username: ${{ secrets.EC2_USER }} | |
key: ${{ secrets.EC2_SSH_KEY }} | |
script: | | |
set -e | |
echo "Creating application.yml..." | |
mkdir -p ~/camchelin/config | |
echo "${{ secrets.APPLICATION_YML }}" > ~/camchelin/config/application.yml | |
echo "application.yml created:" | |
cat ~/camchelin/config/application.yml | |
echo "Stopping and removing existing Docker container..." | |
docker stop camchelin || echo "No container to stop" | |
docker rm camchelin || echo "No container to remove" | |
echo "Pulling latest Docker image..." | |
docker pull tjdgus3488/camchelin:latest | |
echo "Running new Docker container..." | |
docker run -d --name camchelin -p 8080:8080 \ | |
-v ~/camchelin/config/application.yml:/app/config/application.yml \ | |
tjdgus3488/camchelin:latest | |
echo "Verifying application.yml in container..." | |
docker exec camchelin ls /app/config/application.yml || echo "application.yml not found in container" | |
docker exec camchelin cat /app/config/application.yml || echo "Failed to read application.yml from container" | |
echo "Checking running container status..." | |
docker ps |