Skip to content

Merge pull request #105 from HowMuchPay/feature/103 #4

Merge pull request #105 from HowMuchPay/feature/103

Merge pull request #105 from HowMuchPay/feature/103 #4

Workflow file for this run

name: CI & CD using Github Actions
on:
push:
branches: [ 'main' ]
pull_request:
branches: [ 'main' ]
permissions:
contents: read
jobs:
build:
## CI
name: CI
runs-on: ubuntu-latest
steps:
- name: checkout java code
uses: actions/checkout@v3
- name: Set up jdk 17 ver
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'temurin'
# Gradle 캐싱 빌드 시간 향상
- 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-
- name: set yaml secret
uses: microsoft/variable-substitution@v1
with:
files: ./src/main/resources/application-datasource.yml
env:
spring.datasource.username: ${{ secrets.DATABASE_USERNAME }}
spring.datasource.password: ${{ secrets.DATABASE_PASSWORD }}
- name: Grant execute Permission for gradlew
run: chmod +x gradlew
- name: Clean & Build With Gradle
run: ./gradlew build -x test
- name: Login to DockerHub
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Docker build & push to Dockerhub
uses: docker/build-push-action@v5
with:
context: .
file: ./Dockerfile
push: true
tags: ${{ secrets.DOCKER_USERNAME }}/howmuchpay_image:latest
deploy:
## CD
name: CD
runs-on: ubuntu-latest
needs: build
steps:
- name: Deploy to EC2
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.HOST_IP }}
username: ${{ secrets.USERNAME }}
port: 22
key: ${{ secrets.PRIVATE_KEY }}
script: |
docker ps
docker stop ${{ secrets.CONTAINER_NAME }}
docker rm $(docker ps -a -q)
docker image rm $(docker images -q)
docker pull ${{ secrets.DOCKER_USERNAME }}/howmuchpay_image:latest
docker run -d -p 8080:8080 --name ${{ secrets.CONTAINER_NAME }} --env-file .env ${{ secrets.DOCKER_USERNAME }}/howmuchpay_image:latest
docker ps