Skip to content

Merge pull request #109 from wafflestudio/dev #63

Merge pull request #109 from wafflestudio/dev

Merge pull request #109 from wafflestudio/dev #63

Workflow file for this run

name: Deploy to AWS EC2 using Docker
on:
push:
branches:
- main
env:
# Should be filled in with
# The name for the Docker image
# You are building/pushing to Docker Hub.
IMAGE_NAME: waffleteam6airbnb
# EC2 server SSH username
EC2_SSH_USER: ubuntu
jobs:
build-and-push-docker:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'temurin'
# 민감한 정보(ex. DB url, username, password) 는 따로 github secrets에 저장
- name: Set up application.yaml
run: |
echo "${{ secrets.APPLICATION }}" > ./src/main/resources/application.yaml
echo "${{ secrets.P12_BASE64 }}" | base64 --decode > ./src/main/resources/test.p12
- name: Build with Gradle
run: ./gradlew build -x test
- name: Log in to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_HUB_USERNAME }}
password: ${{ secrets.DOCKER_HUB_TOKEN }}
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ secrets.DOCKER_HUB_USERNAME }}/${{ env.IMAGE_NAME }}
flavor: |
latest=true
- name: Build and push Docker image
id: push
uses: docker/build-push-action@v6
with:
context: .
file: ./Dockerfile
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
# appleboy/ssh-action@master 액션을 사용하여 지정한 서버에 ssh로 접속하고, script를 실행합니다.
# script의 내용은 도커의 기존 프로세스들을 제거하고, docker repo로부터 방금 위에서 push한 내용을 pull 받아 실행하는 것입니다.
# 실행 시, docker-compose를 사용합니다.
deploy-to-ec2:
needs: build-and-push-docker
runs-on: ubuntu-latest
steps:
- name: Deploy to EC2
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.EC2_HOST }}
username: ${{ env.EC2_SSH_USER }}
key: ${{ secrets.EC2_SSH_PRIVATE_KEY }}
script: |
echo "${{ secrets.DOCKER_HUB_TOKEN }}" | docker login -u "${{ secrets.DOCKER_HUB_USERNAME }}" --password-stdin
docker rm -f $(docker ps -qa)
docker pull ${{ secrets.DOCKER_HUB_USERNAME }}/${{ env.IMAGE_NAME }}
docker-compose up -d
docker image prune -f
echo "${{ secrets.APPLICATION }}" > application.yaml