Merge pull request #8 from Gongjakso/chore/cicd #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
name: AWS EC2 Deploy | |
on: | |
push: | |
branches: | |
- main | |
jobs: | |
build: | |
# ubuntu 버전 지정 | |
runs-on: ubuntu-22.04 | |
steps: | |
# Checkout 진행 | |
- uses: actions/checkout@v3 | |
# JDK 17 설치 | |
- name: Set up JDK 17 | |
uses: actions/setup-java@v3 | |
with: | |
java-version: '17' | |
distribution: 'corretto' | |
# 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- | |
# Gradle 권한 부여 | |
- name: Grant execute permission for gradlew | |
run: chmod +x gradlew | |
# secret.yml 반영 | |
- name: Make application-secret.yml | |
run: | | |
cd ./src/main/resources | |
touch ./application.yml | |
echo "${{ secrets.APPLICATION_SECRET}}" > ./application-secret.yml | |
shell: bash | |
# release.yml 반영 | |
- name: Make application-release.yml | |
run: | | |
cd ./src/main | |
mkdir resources | |
cd ./resources | |
touch touch ./application.yml | |
echo "${{ secrets.GCP_APPLICATION_RELEASE}}" > ./application.yml | |
shell: bash | |
# Gradle BootJar | |
- name: BootJar with Gradle | |
run: ./gradlew clean bootJar -Dspring.profiles.active=release | |
# Docker Image Push | |
- name: Docker Image push | |
run: | | |
docker login -u ${{ secrets.DOCKERHUB_USERNAME }} -p ${{ secrets.DOCKERHUB_PASSWORD }} | |
docker build -t ${{ secrets.DOCKERHUB_USERNAME}}/${{ secrets.DOCKERHUB_REPOSITORY}} ./ | |
docker push ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_REPOSITORY}} | |
# Docker Compose | |
- name: Docker Compose | |
uses: appleboy/ssh-action@master | |
with: | |
host: ${{ secrets.EC2-SERVER-IP }} | |
username: ${{ secrets.SSH_USERNAME }} | |
key: ${{ secrets.SSH_PRIVATE_KEY }} | |
script: | | |
sudo docker login -u ${{ secrets.DOCKERHUB_USERNAME }} -p ${{ secrets.DOCKERHUB_PASSWORD }} | |
sudo docker-compose stop ${{ secrets.DOCKER_SERVICE_NAME }} | |
sudo docker-compose rm -f ${{ secrets.DOCKER_SERVICE_NAME }} | |
sudo docker pull ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_REPOSITORY}} | |
sudo docker tag ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_REPOSITORY}} ${{ secrets.DOCKER_IMAGE_NAME }} | |
sudo docker-compose up -d |