🚀 deploy config update #6
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 Spring Boot to EC2 | |
on: | |
push: | |
branches: | |
- main | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v2 | |
- name: Set up JDK 21 | |
uses: actions/setup-java@v1 | |
with: | |
java-version: '21' | |
- name: Set up Docker credentials | |
run: | | |
echo "DOCKER_USERNAME=${{ secrets.DOCKER_USERNAME }}" >> $GITHUB_ENV | |
echo "DOCKER_PASSWORD=${{ secrets.DOCKER_PASSWORD }}" >> $GITHUB_ENV | |
- name: Build JAR file | |
run: ./gradlew bootJar | |
- name: Build and push Docker image with JIB | |
run: ./gradlew jib -Pprod --image=${{ secrets.DOCKER_USERNAME }}/learnmate:latest | |
deploy: | |
runs-on: ubuntu-latest | |
needs: build | |
steps: | |
- name: Connect to EC2 and deploy | |
uses: appleboy/ssh-action@master | |
with: | |
host: ${{ secrets.EC2_HOST }} | |
username: ec2-user | |
key: ${{ secrets.EC2_PRIVATE_KEY }} | |
script: | | |
# Pull the latest application image | |
docker pull ${{ secrets.DOCKER_USERNAME }}/learnmate:latest | |
# Stop and remove any existing application container | |
docker stop learnmate-container || true | |
docker rm learnmate-container || true | |
# Run the application container with the name learnmate | |
docker run -d -p 8080:8080 --name learnmate-container \ | |
--env-file /path/to/.env \ | |
${{ secrets.DOCKER_USERNAME }}/learnmate:latest |