Create deploy.yml (#45) #1
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: Backend CD | |
on: | |
push: | |
branches: [ "main" ] | |
jobs: | |
build-and-push: | |
runs-on: ubuntu-latest | |
# 가장 최신의 Ubuntu 러너를 사용합니다. | |
steps: | |
- name: Checkout Repository | |
uses: actions/checkout@v2 | |
# 현재 리포지토리를 체크아웃합니다. | |
- name: JDK 17을 설치합니다 | |
uses: actions/setup-java@v4 | |
with: | |
java-version: '17' | |
distribution: 'corretto' | |
- name: Install redis | |
run: sudo apt-get install -y redis-tools redis-server | |
- name: Verify that redis is up | |
run: redis-cli ping | |
- name: Gradle 명령 실행을 위한 권한을 부여합니다 | |
run: chmod +x gradlew | |
- name: Gradle build를 수행합니다 | |
run: ./gradlew build | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v1 | |
# Docker Buildx를 설정합니다. | |
- name: Login to Docker Hub | |
uses: docker/login-action@v1 | |
with: | |
username: ${{ secrets.DOCKERHUB_USERNAME }} | |
# GitHub Secret에서 Docker Hub 사용자 이름을 가져옵니다. | |
password: ${{ secrets.DOCKERHUB_TOKEN }} | |
# GitHub Secret에서 Docker Hub 액세스 토큰을 가져옵니다. | |
- name: Build and Push | |
uses: docker/build-push-action@v2 | |
with: | |
context: . | |
# Dockerfile이 있는 위치입니다. | |
file: .Dockerfile | |
# Dockerfile의 경로입니다. | |
push: true # 이미지를 레지스트리에 푸시합니다. | |
tags: ${{ secrets.DOCKERHUB_USERNAME }}/pct-backend:${{ github.sha }} | |
platforms: linux/amd64 | |
deploy-to-ec2: | |
runs-on: ubuntu-latest | |
needs: build-and-push | |
steps: | |
- name: Install SSH | |
run: sudo apt update && sudo apt install -y openssh-client | |
- name: SSH into EC2 and run Backend Container | |
uses: appleboy/ssh-action@master | |
with: | |
host: ${{ secrets.EC2_HOST }} | |
username: ${{ secrets.EC2_USERNAME }} | |
key: ${{ secrets.EC2_PRIVATE_KEY }} | |
script: | | |
./deploy.sh | |