Skip to content

Merge pull request #317 from depromeet/test/#296-comment #211

Merge pull request #317 from depromeet/test/#296-comment

Merge pull request #317 from depromeet/test/#296-comment #211

name: Develop Build & Deploy
on:
push:
branches: [ "develop" ]
env:
DOCKERHUB_IMAGE_NAME: walwal-server
jobs:
build-deploy:
runs-on: ubuntu-latest
environment: DEV
strategy:
matrix:
java-version: [ 17 ]
distribution: [ 'temurin' ]
steps:
- name: Checkout code
uses: actions/checkout@v4
# JDK를 17 버전으로 세팅
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: ${{ matrix.java-version }}
distribution: ${{ matrix.distribution }}
# test 활용 목적의 container 실행
- name: Start test docker container
run: docker compose -f ./docker-compose-test.yaml up -d
- name: Grant execute permission for gradlew
run: chmod +x ./gradlew
- name: Build with Gradle
id: gradle
uses: gradle/gradle-build-action@v3
with:
arguments: |
build
--scan
cache-read-only: ${{ github.ref != 'refs/heads/main' && github.ref != 'refs/heads/develop' }}
# Dockerhub 로그인
- name: Login to Dockerhub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
# Docker 메타데이터 추출
- name: Extract Docker metadata
id: metadata
uses: docker/[email protected]
env:
DOCKERHUB_IMAGE_FULL_NAME: ${{ secrets.DOCKERHUB_USERNAME }}/${{ env.DOCKERHUB_IMAGE_NAME }}
with:
images: ${{ env.DOCKERHUB_IMAGE_FULL_NAME }}
tags: |
type=sha,prefix=
# Docker 이미지 빌드 및 도커허브로 푸시
- name: Docker Build and Push
uses: docker/build-push-action@v5
with:
context: .
push: true
tags: ${{ steps.metadata.outputs.tags }}
# 서버로 docker-compose 파일 전송
- name: Copy docker-compose file to EC2
uses: burnett01/[email protected]
with:
switches: -avzr --delete
remote_host: ${{ secrets.EC2_HOST }}
remote_user: ${{ secrets.EC2_USERNAME }}
remote_key: ${{ secrets.EC2_PRIVATE_KEY }}
path: docker-compose.yaml
remote_path: /home/ec2-user/
- name: Copy default.conf to EC2
uses: burnett01/[email protected]
with:
switches: -avzr --delete
remote_host: ${{ secrets.EC2_HOST }}
remote_user: ${{ secrets.EC2_USERNAME }}
remote_key: ${{ secrets.EC2_PRIVATE_KEY }}
path: ./nginx
remote_path: /home/ec2-user/
# EC2로 배포
- name: Deploy to EC2 Server
uses: appleboy/[email protected]
env:
IMAGE_FULL_URL: ${{ steps.metadata.outputs.tags }}
DOCKERHUB_IMAGE_NAME: ${{ env.DOCKERHUB_IMAGE_NAME }}
with:
host: ${{ secrets.EC2_HOST }}
username: ${{ secrets.EC2_USERNAME }}
key: ${{ secrets.EC2_PRIVATE_KEY }}
envs: IMAGE_FULL_URL, DOCKERHUB_IMAGE_NAME # docker-compose.yml 에서 사용할 환경 변수
debug: true
script: |
echo "${{ secrets.DOCKERHUB_TOKEN }}" | docker login -u "${{ secrets.DOCKERHUB_USERNAME }}" --password-stdin
docker compose up -d
docker exec -d nginx nginx -s reload
docker image prune -a -f
## Slack
- name: Slack Alarm
uses: 8398a7/action-slack@v3
with:
status: ${{ job.status }}
author_name: GitHub-Actions CI/CD
fields: repo,message,commit,author,ref,job,took
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} # required
if: always() # Pick up events even if the job fails or is canceled.