Skip to content

Commit

Permalink
[fix] cicd workflow 간소화
Browse files Browse the repository at this point in the history
  • Loading branch information
ggamD00 committed Jul 24, 2024
1 parent 2acaf0b commit 75a0b9c
Showing 1 changed file with 33 additions and 107 deletions.
140 changes: 33 additions & 107 deletions .github/workflows/gradle.yml
Original file line number Diff line number Diff line change
@@ -1,117 +1,43 @@
name: CI/CD
name: server


# 동작 조건 설정: release 브랜치에 push나 pull_request가 발생할 경우 동작한다.
on:
push:
branches: [ "dev" ]
pull_request:
branches: [ "dev" ]
branches:
- dev

permissions:
contents: read

jobs:
CI-CD:
build:
runs-on: ubuntu-latest
steps:
# JDK setting - github actions에서 사용할 JDK 설정 (aws 과 project의 java 버전과 별도로 관리)
- uses: actions/checkout@v3
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'temurin'

## gradle caching (빌드 시간 줄이기)
- name: Gradle Caching
uses: actions/cache@v3
with:
path: |
~/.gradle/caches
~/.gradle/wrapper
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
restore-keys: |
${{ runner.os }}-gradle-
# 환경별 yml 파일 생성(1) - dev
- name: make application-dev.yml
if: contains(github.ref, 'develop')
run: |
cd ./src/main/resources
touch ./application.yml
echo "${{ secrets.YML }}" > ./application.yml
shell: bash

# 환경별 yml 파일 생성(2) - prod
- name: make application-prod.yml
if: contains(github.ref, 'main')
run: |
cd ./src/main/resources
touch ./application.yml
echo "${{ secrets.YML }}" > ./application.yml
shell: bash

# gradle chmod
- name: Grant execute permission for gradlew
run: chmod +x gradlew

# gradle build
- name: Build with Gradle
run: ./gradlew clean build -x test

# docker login
- name: Docker Hub Login
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}

# docker build & push to production
- name: Docker build & push to prod
if: contains(github.ref, 'main')
run: |
docker build -f Dockerfile -t ${{ secrets.DOCKER_REPO }}/binjumeoniz .
docker push ${{ secrets.DOCKER_REPO }}/binjumeoniz
# docker build & push to develop
- name: Docker build & push to dev
if: contains(github.ref, 'develop')
run: |
docker build -f Dockerfile -t ${{ secrets.DOCKER_REPO }}/binjumeoniz .
docker push ${{ secrets.DOCKER_REPO }}/binjumeoniz
## deploy to production
- name: Deploy to prod
uses: appleboy/ssh-action@master
id: deploy-prod
if: contains(github.ref, 'main')
with:
host: ${{ secrets.HOST_PROD }} # EC2 퍼블릭 IPv4 DNS
username: ubuntu
key: ${{ secrets.PRIVATE_KEY }}
envs: GITHUB_SHA
script: |
sudo docker ps
sudo docker rm -f $(docker ps -qa)
sudo docker pull ${{ secrets.DOCKER_REPO }}/binjumeoniz
sudo docker run -d -p 8080:8080 ${{ secrets.DOCKER_REPO }}/binjumeoniz
sudo docker image prune -f
## ## sudo docker run -d -p 8080:8080 ${{ secrets.DOCKER_REPO }}/binjumeoniz

## deploy to develop
- name: Deploy to dev
uses: appleboy/ssh-action@master
id: deploy-dev
if: contains(github.ref, 'develop')
with:
host: ${{ secrets.HOST_DEV }} # EC2 퍼블릭 IPv4 DNS
username: ${{ secrets.USERNAME }} # ubuntu
password: ${{ secrets.PASSWORD }}
port: 22
key: ${{ secrets.PRIVATE_KEY }}
script: |
sudo docker ps
sudo docker rm -f $(docker ps -qa)
sudo docker pull ${{ secrets.DOCKER_REPO }}/binjumeoniz
sudo docker run -d -p 8080:8080 ${{ secrets.DOCKER_REPO }}/binjumeoniz
sudo docker image prune -f
steps:
- uses: actions/checkoutc

- name: Set up JDK 17
uses: actions/setup-java@v3
with:
distribution: temurin
java-version: 17

- name : injection-yml
run : echo -E "${{ secrets.YML }}" > ./src/main/resources/application.yml

- name: Log in to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}

- name: Execute Gradle build and analyze
run: ./gradlew jib

- name: Run scripts in server
uses: appleboy/ssh-action@master
with:
key: ${{ secrets.PRIVATE_KEY }}
host: ${{ secrets.HOST_DEV }}
username: ${{ secrets.USERNAME }}
script: ${{ secrets.SCRIPT }}

0 comments on commit 75a0b9c

Please sign in to comment.