initial setting: connecting db #27
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 Docker | |
# 트리거를 수행할 브랜치를 지정합니다. | |
on: | |
push: | |
branches: [ main ] | |
permissions: | |
contents: read | |
packages: write | |
# 환경설정 | |
env: | |
DOCKER_IMAGE: ghcr.io/${{ github.repository_owner }}/ruty-server-deploy | |
VERSION: ${{ github.sha }} | |
NAME: ruty-server | |
GCP_USER: gcptest0914 | |
jobs: | |
# 빌드 Job | |
build: | |
name: Build | |
runs-on: ubuntu-latest | |
steps: | |
# github repository에서 checkout | |
- uses: actions/checkout@v2 | |
# Set up JDK | |
- name: Set up JDK | |
uses: actions/setup-java@v2 | |
with: | |
java-version: 17 | |
distribution: 'temurin' | |
# Build JAR file with Gradle | |
- name: Build with Gradle | |
run: ./gradlew clean build | |
# 디버깅: JAR 파일 확인 | |
- name: Check JAR file | |
run: ls -al build/libs/ | |
# docker build 수행 | |
- name: Set up docker buildx | |
id: buildx | |
uses: docker/setup-buildx-action@v1 | |
- name: Set Docker Image Name | |
run: echo "DOCKER_IMAGE=ghcr.io/$(echo '${{ github.repository_owner }}' | tr '[:upper:]' '[:lower:]')/ruty-server-deploy" >> $GITHUB_ENV | |
- name: Verify DOCKER_IMAGE | |
run: echo ${{ env.DOCKER_IMAGE }} | |
- name: Cache docker layers | |
uses: actions/cache@v2 | |
with: | |
path: /tmp/.buildx-cache | |
key: ${{ runner.os }}-buildx-${{ env.VERSION }} | |
restore-keys: | | |
${{ runner.os }}-buildx- | |
# GitHub 컨테이너 레지스트리에 로그인 후 빌드 & 푸시 | |
- name: Login to ghcr | |
uses: docker/login-action@v1 | |
with: | |
registry: ghcr.io | |
username: ${{ github.actor }} | |
password: ${{ secrets.CICD_TOKEN }} | |
- name: Build and push | |
id: docker_build | |
uses: docker/build-push-action@v2 | |
with: | |
builder: ${{ steps.buildx.outputs.name }} | |
push: true | |
tags: ${{ env.DOCKER_IMAGE }}:latest | |
context: . | |
# 배포 Job | |
deploy: | |
needs: build # build 후에 실행되도록 정의 | |
name: Deploy | |
runs-on: [ self-hosted, label-go ] # AWS ./configure에서 사용할 label명 | |
steps: | |
- name: Set Docker Image Name | |
run: echo "DOCKER_IMAGE=ghcr.io/$(echo '${{ github.repository_owner }}' | tr '[:upper:]' '[:lower:]')/ruty-server-deploy" >> $GITHUB_ENV | |
- name: Verify DOCKER_IMAGE | |
run: echo ${{ env.DOCKER_IMAGE }} | |
- name: Login to ghcr | |
uses: docker/login-action@v1 | |
with: | |
registry: ghcr.io | |
username: ${{ github.actor }} | |
password: ${{ secrets.CICD_TOKEN }} | |
# SSH Key 설정 | |
- name: Set up SSH Key | |
run: | | |
echo "${{ secrets.GCP_PRIVATE_KEY }}" > ~/.ssh/ruty-ssh | |
chmod 600 ~/.ssh/ruty-ssh | |
# docker-compose 파일을 VM으로 복사 | |
- name: Copy docker-compose.yml to VM | |
run: | | |
scp -i ~/.ssh/ruty-ssh ./docker-compose.yaml ${{ env.GCP_USER }}@${{ secrets.GCP_HOST }}:/home/${{ env.GCP_USER }}/ | |
# 원격에서 Docker Compose 실행 | |
- name: Run Docker Compose on VM | |
run: | | |
ssh -i ~/.ssh/ruty-ssh ${{ env.GCP_USER }}@${{ secrets.GCP_HOST }} << EOF | |
cd /home/${{ env.GCP_USER }} | |
docker-compose down --rmi all || true | |
docker-compose pull || true | |
docker-compose up -d || true | |
docker-compose ps | |
EOF |