Skip to content

Update ci/cd pipeline to use docker compose instead of docker #3

Update ci/cd pipeline to use docker compose instead of docker

Update ci/cd pipeline to use docker compose instead of docker #3

Workflow file for this run

# github repository actions 페이지에 나타날 이름
name: CI/CD using github actions & docker
# event trigger
# main이나 develop 브랜치에 push가 되었을 때 실행
on:
push:
branches: [ "main", "dev" ]
permissions:
contents: read
jobs:
CI-CD:
runs-on: ubuntu-22.04
steps:
# JDK setting - github actions에서 사용할 JDK 설정 (프로젝트나 AWS의 java 버전과 달라도 무방)
- uses: actions/checkout@v3
- name: Set up JDK 21
uses: actions/setup-java@v3
with:
java-version: '21'
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 파일 생성 - secret, oauth
- name: make application-secret.yml && application-oauth.yml
if: contains(github.ref, 'main')
run: |
cd ./src/main/resources
touch ./application-secret.yml
echo "${{ secrets.APPLICATION_SECRET }}" > ./application-secret.yml
touch ./application-oauth.yml
echo "${{ secrets.APPLICATION_OAUTH }}" > ./application-oauth.yml
shell: bash
# 환경별 yml 파일 생성(3) - deploy
- name: make application-deploy.yml
if: contains(github.ref, 'main')
run: |
cd ./src/main/resources
touch ./application-deploy.yml
echo "${{ secrets.APPLICATION_DEPLOY }}" > ./application-deploy.yml
shell: bash
# gradle build
- name: Build with Gradle
run: ./gradlew build
- name: Upload docker compose file to deploy server
if: contains(github.ref, 'main')
uses: appleboy/scp-action@master
with:
host: ${{ secrets.HOST_DEPLOY }}
username: ubuntu
key: ${{ secrets.EC2_KEYPAIR }}
port: 22
source: "./docker/deploy/*"
target: "/home/ubuntu/workspace/docker/"
- name: Upload docker compose file to dev server
if: contains(github.ref, 'dev')
uses: appleboy/scp-action@master
with:
host: ${{ secrets.HOST_DEV }}
username: ubuntu
key: ${{ secrets.EC2_KEYPAIR }}
port: 22
source: "./docker/dev/*"
target: "/home/ubuntu/workspace/docker/"
# docker build & push to deploy server
- name: Docker build & push to deploy
if: contains(github.ref, 'main')
run: |
docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }}
docker build -f Dockerfile-deploy -t ${{ secrets.DOCKER_USERNAME }}/genti-deploy .
docker push ${{ secrets.DOCKER_USERNAME }}/genti-deploy
# docker build & push to develop
- name: Docker build & push to dev
if: contains(github.ref, 'dev')
run: |
docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }}
docker build -f Dockerfile-dev -t ${{ secrets.DOCKER_USERNAME }}/genti-dev .
docker push ${{ secrets.DOCKER_USERNAME }}/genti-dev
## deploy to deploy server
- name: Deploy to deploy server
uses: appleboy/ssh-action@master
id: deploy-deploy
if: contains(github.ref, 'main')
with:
host: ${{ secrets.HOST_DEPLOY }} # EC2 퍼블릭 IPv4 DNS
username: ubuntu
key: ${{ secrets.EC2_KEYPAIR }}
envs: GITHUB_SHA
script: |
sudo docker ps
sudo cd /home/ubuntu/workspace/docker
sudo pwd
sudo docker pull ${{ secrets.DOCKER_USERNAME }}/genti-deploy
sudo docker compose up -d
sudo docker image prune -f
## deploy to dev server
- name: Deploy to dev server
uses: appleboy/ssh-action@master
id: deploy-dev
if: contains(github.ref, 'develop')
with:
host: ${{ secrets.HOST_DEV }} # EC2 퍼블릭 IPv4 DNS
username: ubuntu
password: ${{ secrets.PASSWORD }}
port: 22
key: ${{ secrets.EC2_KEYPAIR }}
script: |
sudo docker ps
sudo cd /home/ubuntu/workspace/docker
sudo pwd
sudo docker pull ${{ secrets.DOCKER_USERNAME }}/genti-deploy
sudo docker compose up -d
sudo docker image prune -f