chore: update yaml file prod-CD #15
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: prod CI&CD to Cloud | |
on: | |
pull_request: | |
branches: | |
- main | |
push: | |
branches: | |
- prod | |
env: | |
DOCKERHUB_REPO: ${{ secrets.DOCKERHUB_PROD_REPO }} | |
DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }} | |
DOCKERHUB_PASSWORD: ${{ secrets.DOCKERHUB_PASSWORD }} | |
jobs: | |
build: | |
name: CI & CD | |
runs-on: ubuntu-latest | |
steps: | |
# (0) 시간 출력 | |
- name: Get Current Time | |
uses: 1466587594/get-current-time@v2 | |
id: current-time | |
with: | |
format: YYYY-MM-DDTHH:mm:ss | |
timezone: 'Asia/Seoul' | |
- name: Print Current Time | |
run: echo "Current Time=${{steps.current-time.outputs.formattedTime}}" | |
shell: bash | |
# (1) 기본 체크아웃 | |
- name: Checkout | |
uses: actions/checkout@v3 | |
# (2) JDK 11 세팅 | |
- name: Set up JDK 11 | |
uses: actions/setup-java@v3 | |
with: | |
java-version: 11 | |
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- | |
# application prod yml 파일 생성 | |
- name: make application-prod.yml | |
if: contains(github.ref, 'prod') | |
run: | | |
cd ./src/main/resources | |
touch ./application-prod.yml | |
echo "${{ secrets.YML_PROD }}" > ./application-prod.yml | |
shell: bash | |
# gradle build 테스트 제거 | |
- name: Build with Gradle | |
run: ./gradlew build -x test | |
# (6) Docker 이미지 빌드 | |
- name: Docker build & push to docker repo | |
if: contains(github.ref, 'prod') | |
run: | | |
docker login -u $DOCKERHUB_USERNAME -p $DOCKERHUB_PASSWORD | |
docker build -f Dockerfile -t $DOCKERHUB_REPO/ssafsound-app . | |
docker push $DOCKERHUB_REPO/ssafsound-app | |
# (7) ssh 접속 후 docker compose up | |
- name: Deploy to server | |
uses: appleboy/ssh-action@master | |
id: deploy_to_prod | |
if: contains(github.ref, 'prod') | |
with: | |
host: ${{ secrets.AWS_PROD_HOST }} | |
username: ubuntu | |
port: 22 | |
key: ${{ secrets.AWS_PROD_KEY }} | |
script: | | |
sudo docker rm -f $(docker ps -qa) | |
sudo docker pull $DOCKERHUB_REPO/ssafsound-app | |
docker-compose up -d | |
docker image prune -f |