test : CICD 작동 확인 #85
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
# github repository actions 페이지에 나타날 이름 | |
name: CI/CD using github actions & docker | |
# event trigger | |
on: | |
push : | |
branches : ["master","dev"] | |
permissions: | |
contents: read | |
jobs: | |
CI_CD: | |
runs-on: ubuntu-latest | |
steps: | |
# JDK setting - github actions에서 사용할 JDK 설정 (프로젝트나 AWS의 java 버전과 달라도 무방) | |
- uses: actions/checkout@v4 | |
- name: Set up JDK 17 | |
uses: actions/setup-java@v4 | |
with: | |
java-version: '17' | |
distribution: 'temurin' | |
# gradle caching - 빌드 시간 향상 | |
- name: Gradle Caching | |
uses: actions/cache@v4 | |
with: | |
path: | | |
~/.gradle/caches | |
~/.gradle/wrapper | |
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }} | |
restore-keys: | | |
${{ runner.os }}-gradle- | |
# Github Submodule에서 환경변수 가져오기 | |
- name: Checkout repo | |
uses: actions/checkout@v4 | |
with: | |
token: ${{secrets.ACTION_TOKEN}} | |
submodules: true | |
# gradle build | |
- name: Build with Gradle | |
run: | | |
echo "Files in ./habit_secret directory:" | |
ls -al ./habit_secret | |
chmod +x ./gradlew | |
./gradlew copyPrivate build -x test | |
# Docker build & push to prod | |
- name: Docker build & push to prod | |
if: contains(github.ref, 'master') | |
run: | | |
docker login -u ${{secrets.DOCKER_USER}} -p ${{secrets.DOCKER_PASSWORD}} | |
docker build -t ${{secrets.DOCKER_REPOSITORY}} . | |
docker push ${{secrets.DOCKER_REPOSITORY}} | |
# Deploy to prod | |
- name: Deploy to prod | |
uses: appleboy/ssh-action@master | |
id: deploy-prod | |
if: contains(github.ref, 'master') | |
with: | |
host: ${{secrets.HOST}} | |
username: root | |
password: ${{secrets.SSH_PASSWORD}} | |
port: 2223 | |
key: ${{secrets.PRIVATE_KEY}} | |
envs: GITHUB_SHA | |
script: | | |
sudo docker ps | |
sudo docker stop springboot | |
sudo docker rm -f springboot | |
sudo docker pull ${{secrets.DOCKER_REPOSITORY}} | |
sudo docker run -d --network host --name springboot ${{secrets.DOCKER_REPOSITORY}} | |
sudo docker image prune -f |