Create heypets-cicd.yml #1
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: heypets CI/CD | |
# event trigger | |
on: | |
push: | |
branches: | |
- dev | |
- main | |
permissions: | |
contents: read | |
jobs: | |
CI-CD: | |
runs-on: ubuntu-latest | |
steps: | |
## jdk setting | |
- uses: actions/checkout@v3 | |
- name: Set up JDK 17 | |
uses: actions/setup-java@v3 | |
with: | |
java-version: '17' | |
distribution: 'temurin' # https://github.com/actions/setup-java | |
## 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- | |
## gradle build | |
- name: Change gradlew permissions | |
run: chmod +x ./gradlew | |
- name: Build with Gradle | |
run: ./gradlew build | |
## docker build & push to production | |
- name: Docker build & push | |
if: contains(github.ref, 'dev') | |
run: | | |
sudo docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }} | |
sudo docker build --platform linux/amd64 -t ${{ secrets.DOCKER_REPO }} . | |
sudo docker push ${{ secrets.DOCKER_REPO }} | |
# ## deploy to production | |
- name: Deploy to prod | |
uses: appleboy/ssh-action@master | |
id: deploy-prod | |
if: contains(github.ref, 'main') | |
with: | |
host: ${{ secrets.HEYPETS_SERVER_HOST }} | |
username: ${{ secrets.HEYPETS_SERVER_USERNAME }} | |
port: ${{ secrets.HEYPETS_SERVER_PORT }} | |
key: ${{ secrets.HEYPETS_PRIVATE_KEY }} | |
script: | | |
sudo apt-get update | |
sudo docker compose down | |
sudo docker rmi $(sudo docker images -q) | |
sudo docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }} | |
sudo docker pull ${{ secrets.DOCKER_REPO }} | |
sudo docker image prune -f | |
sudo docker compose up -d |