Skip to content

chore : docker 관련 설정 #16

chore : docker 관련 설정

chore : docker 관련 설정 #16

Workflow file for this run

name: develop branch Build and Deploy to EC2
on:
pull_request:
branches: [ "main" ]
workflow_dispatch:
env:
BUCKET_NAME: hello-consultant
PROJECT_NAME: hello
DEPLOYMENT_GROUP_NAME: hello-consultant
CODE_DEPLOY_APP_NAME: code-deploy
jobs:
# 작업의 이름
build_and_test:
# GitHub Actions 러너의 운영 체제
runs-on: ubuntu-latest
services:
mysql:
image: mysql:8.0.21
env:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: test
ports:
- 3306:3306
mongodb:
image: mongo:latest
env:
MONGO_INITDB_ROOT_USERNAME: admin
MONGO_INITDB_ROOT_PASSWORD: admin
MONGO_INITDB_DATABASE: APP-DATABASE
ports:
- 27017:27017
# options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=10
# 순차적으로 실행될 단계들을 정의하는 섹션
steps:
- uses: actions/checkout@v3
- name: Install mongosh
run: |
sudo apt-get install gnupg
wget -qO- https://www.mongodb.org/static/pgp/server-7.0.asc | sudo tee /etc/apt/trusted.gpg.d/server-7.0.asc
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list
sudo apt-get update
sudo apt-get install -y mongodb-mongosh
mongosh --version
- name: Initalize MongoDB
run: |
mongosh --host localhost:27017 -u admin -p admin --authenticationDatabase admin <<EOF
db = db.getSiblingDB('APP-DB');
db.createUser({ user: 'APP-USERNAME', pwd: 'APP-PASSWORD', roles: [{ role: 'readWrite', db: 'APP-DB' }] })
db.createCollection('APP-COLLECTION');
EOF
# Do whatever you like in here
- name: Set up JDK 17
uses: actions/[email protected]
with:
java-version: '17'
distribution: 'corretto'
- name: Grant execute permission for gradlew
run: chmod +x ./gradlew
- name: Create application.yml
run: |
mkdir -p src/main/resources
mkdir -p src/test/resources
echo "${{ secrets.APPLICATION_YML_CONTENT }}" > src/main/resources/application.yml
echo "${{ secrets.APPLICATION_TEST_YML_CONTENT }}" > src/test/resources/application.yml
echo "${{ secrets.GCP_BUCKET_CONTENT }}" > src/main/resources/meritz-420004-93e56cbdf236.json
- name: Clean with gradle
run: ./gradlew clean
- name: Build with gradle
run: ./gradlew build
- name: Docker build & push to prod
if: contains(github.ref, 'main')
run: |
docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }}
docker build -f Dockerfile -t ${{ secrets.DOCKER_USERNAME }}/hello-counsellor .
docker push ${{ secrets.DOCKER_USERNAME }}/hello-counsellor
- 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 pull ${{ secrets.DOCKER_USERNAME }}/hello-counsellor
sudo docker run -d -p 8080:8080 ${{ secrets.DOCKER_USERNAME }}/hello-counsellor
sudo docker image prune -f