Skip to content

Update cicd.yml

Update cicd.yml #20

Workflow file for this run

name: ci-cd
on:
push:
branches: [infra/#8-cicd, release]
permissions:
contents: read
jobs:
build-image:
runs-on: ubuntu-latest
environment: release
defaults:
run:
shell: bash
working-directory: ./be/carrot
steps:
- uses: actions/checkout@v3
## JDK Setting
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'adopt'
# gradle caching - 빌드 시간 향상
- name: Gradle Caching
uses: actions/cache@v3
with:
# cache directory setting
path:
~/.gradle/caches
~/.gradle/wrapper
# separate cache key setting
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
# 이전에 생성된 캐시 복원에 사용할 키 지정
# 캐시가 없거나 만료되었을 때, 이 키를 기반으로 이전에 생성된 캐시를 찾아 복원
restore-keys: |
${{ runner.os }}-gradle-
# 환경별 yml 파일 생성 - application.yml
- name: make application.yml
if: |
contains(github.ref, 'release') ||
contains(github.ref, 'infra/#8-cicd')
run: |
mkdir ./src/main/resources
cd ./src/main/resources
touch ./application.yml
echo "${{ secrets.APPLICATION_RELEASE }}" > ./applicaiton.yml
shell: bash
# gradlew 실행을 위한 실행 권한 부여
- name: Grant execute permission for gradlew
run: sudo chmod +x ./gradlew
# Clean Gradle Build
- name: Clean with Gradle
run: sudo ./gradlew clean
# Gradle을 이용하여 빌드 수행
- name: Build with Gradle
run: sudo ./gradlew build -x test
# docker build & push to devleop
- name: Docker build & push to release
if: contains(github.ref, 'release') ||
contains(github.ref, 'infra/#8-cicd')
run: |
docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }}
docker build -f Dockerfile-release -t ${{ secrets.DOCKER_USERNAME }}/bee_market_app_release .
docker push ${{ secrets.DOCKER_USERNAME }}/bee_market_app_release
# docker build & push to release
- name: Docker build & push to release
if: contains(github.ref, 'release')
run: |
docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }}
docker build -f Dockerfile-release -t ${{ secrets.DOCKER_USERNAME }}/bee_market_app_release .
docker push ${{ secrets.DOCKER_USERNAME }}/bee_market_app_release
build-react-image:
runs-on: ubuntu-latest
environment: release
defaults:
run:
shell: bash
working-directory: ./fe
strategy:
matrix:
node-version: [ 18.x ]
steps:
- uses: actions/checkout@v3
- name: Docker build to release
if: contains(github.ref, 'release') ||
contains(github.ref, 'infra/#8-cicd')
run: |
docker login -u ${{ secrets.DOCKER_USERNAME }} -p & {{ secrets.DOCKER_PASSWORD }}
docker build -f Dockerfile-release -t ${{ secrets.DOCKER_USERNAME }}/bee_market_web_release .
docker push ${{ secrets.DOCKER_USERNAME }}/bee_market_web_release
- name: Docker build to release
if: contains(github.ref, 'release')
run: |
docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }}
docker build -f Dockerfile-release -t ${{ secrets.DOCKER_USERNAME }}/bee_market_web_release .
docker push ${{ secrets.DOCKER_USERNAME }}/bee_market_web_release
cd-pipeline:
needs:
- build-image
- build-react-image
name: continuos deploy
runs-on: ubuntu-latest
environment: release
steps:
- uses: actions/checkout@master
# nginx-release.conf 파일 EC2에 업로드
- name: copy nginx-release.conf file via ssh password
uses: appleboy/scp-action@master
if: contains(github.ref, 'release')
with:
host: ${{ secrets.HOST }}
username: ec2-user
key: ${{ secrets.PRIVATE_KEY }}
port: 22
source: "./nginx/nginx-release.conf"
target: "/home/ec2-user/"
# docker-compose-release.yml 파일 EC2에 업로드
- name: copy docker-compose-release file via ssh password
uses: appleboy/scp-action@master
if: contains(github.ref, 'release')
with:
host: ${{ secrets.HOST }}
username: ec2-user
key: ${{ secrets.PRIVATE_KEY }}
port: 22
source: "docker-compose-release.yml"
target: "/home/ec2-user/"
# docker-compose-release 실행
- name: Deploy to release
uses: appleboy/ssh-action@master
if: contains(github.ref, 'release')
with:
host: ${{ secrets.HOST }} # EC2 인스턴스 퍼블릭 DNS
username: ec2-user
key: ${{ secrets.PRIVATE_KEY }} # pem 키
port: 22
script: |
sudo chmod 666 /var/run/docker.sock
docker-compose -f docker-compose-release.yml down -v
docker-compose -f docker-compose-release.yml pull
docker-compose -f docker-compose-release.yml up -d
docker image prune -f