Merge pull request #27 from Kyuwon-Choi/main #20
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: CI/CD using GitHub Actions & Docker | |
on: | |
push: | |
branches: | |
- master | |
jobs: | |
build-and-deploy: | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v3 | |
- name: Set up JDK 17 | |
uses: actions/setup-java@v3 | |
with: | |
java-version: '17' | |
distribution: 'temurin' | |
- name: Build with Gradle | |
run: | | |
cd paperplane | |
./gradlew build -x test | |
- name: Docker Build & Push | |
run: | | |
cd paperplane | |
docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_TOKEN }} | |
docker build -t ${{ secrets.DOCKER_USERNAME }}/paperplane-prod:latest . | |
docker push ${{ secrets.DOCKER_USERNAME }}/paperplane-prod:latest | |
- name: Deploy to EC2 | |
uses: appleboy/ssh-action@master | |
with: | |
host: ${{ secrets.EC2_HOST }} | |
username: ${{ secrets.EC2_USERNAME }} | |
key: ${{ secrets.SSH_PRIVATE_KEY }} | |
port: 22 | |
script: | | |
echo "Pulling the latest Docker image on EC2" | |
docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_TOKEN }} | |
docker pull ${{ secrets.DOCKER_USERNAME }}/paperplane-prod:latest | |
echo "Stopping existing container (if running)" | |
docker stop paperplane || true && docker rm paperplane || true | |
echo "Starting a new container" | |
docker run -d --name paperplane -p 8080:8080 ${{ secrets.DOCKER_USERNAME }}/paperplane-prod:latest |