Merge pull request #95 from BaeJunH0/dev #40
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 on Merge | |
on: | |
push: | |
branches: | |
- main | |
permissions: | |
contents: read | |
jobs: | |
build-and-deploy: | |
runs-on: ubuntu-latest | |
steps: | |
# 지정한 저장소(현재 REPO)에서 코드를 워크플로우 환경으로 가져오도록 하는 github action | |
- name: Checkout repository | |
uses: actions/checkout@v3 | |
with: | |
fetch-depth: 0 | |
ref: main | |
# open jdk 17 버전 환경을 세팅 | |
- name: Set up JDK 17 | |
uses: actions/setup-java@v3 | |
with: | |
java-version: '17' | |
distribution: 'adopt' | |
# gradle을 통해 소스를 빌드. | |
- name: Build with Gradle | |
run: | | |
chmod +x ./gradlew | |
./gradlew clean build -x test | |
# Docker Login | |
- name: Docker Login Process | |
uses: docker/login-action@v2 | |
with: | |
username: ${{ secrets.DOCKER_USERNAME }} | |
password: ${{ secrets.DOCKER_PASSWORD }} | |
# Build and Push Docker Image | |
- name: Build and Push Docker Image | |
uses: docker/build-push-action@v4 | |
with: | |
context: . | |
push: true | |
tags: ${{ secrets.DOCKER_USERNAME }}/alumni_link | |
build-args: | | |
KEY=${{ secrets.KEY }} | |
TOKEN_EXPIRE_LENGTH=${{ secrets.TOKEN_EXPIRE_LENGTH }} | |
no-cache: true | |
# appleboy/ssh-action@master 액션을 사용하여 지정한 서버에 ssh로 접속하고, script를 실행합니다. | |
# script의 내용은 도커의 기존 프로세스들을 제거하고, docker repo로부터 방금 위에서 push한 내용을 pull 받아 실행하는 것입니다. | |
# 실행 시, docker-compose를 사용합니다. | |
- name: Deploy to server | |
uses: appleboy/ssh-action@master | |
id: deploy | |
with: | |
host: ${{ secrets.SERVER_HOST }} | |
username: ubuntu | |
key: ${{ secrets.SERVER_SSH_KEY }} | |
script: | | |
# 기존 컨테이너들이 있으면 제거 | |
CONTAINERS=$(docker ps -qa) | |
if [ -n "$CONTAINERS" ]; then | |
sudo docker rm -f $CONTAINERS | |
fi | |
sudo docker pull ${{ secrets.DOCKER_USERNAME }}/alumni_link:latest | |
docker-compose up -d | |
docker image prune -f |