docker deploy #32
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
on: | |
push: | |
branches: | |
- main | |
name: Deploy Spring boot to Amazon EC2 | |
env: | |
PROJECT_NAME: board | |
permissions: | |
contents: read | |
jobs: | |
deploy: | |
name: DEPLOY | |
runs-on: ubuntu-22.04 | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v2 | |
- name: Set up JDK 17 | |
uses: actions/setup-java@v1 | |
with: | |
java-version: 17 | |
- name: Set Yaml | |
uses: microsoft/variable-substitution@v1 | |
with: | |
files: ./src/main/resources/application-prod.yml | |
env: | |
spring.datasource.url: ${{ secrets.DB_URL }} | |
spring.datasource.username: ${{ secrets.DB_USER_NAME }} | |
spring.datasource.password: ${{ secrets.DB_PASSWORD }} | |
- name: Grant execute permission for gradlew | |
run: chmod +x gradlew | |
shell: bash | |
- name: Build with Gradle | |
run: ./gradlew build | |
shell: bash | |
# dockerfile을 통해 이미지를 빌드하고, 이를 docker repo로 push 합니다. | |
# 이 때 사용되는 ${{ secrets.DOCKER_REPO }}/directors-dev 가 위에서 만든 도커 repository 입니다. | |
- name: Docker build & push to docker repo | |
run: | | |
docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }} | |
docker build -f Dockerfile -t ${{ secrets.DOCKER_REPO }}/directors-dev . | |
docker push ${{ secrets.DOCKER_REPO }}/directors-dev | |
# appleboy/ssh-action@master 액션을 사용하여 지정한 서버에 ssh로 접속하고, script를 실행합니다. | |
# script의 내용은 도커의 기존 프로세스들을 제거하고, docker repo로부터 방금 위에서 push한 내용을 pull 받아 실행하는 것입니다. | |
# 실행 시, docker-compose를 사용합니다. | |
- name: Deploy to server | |
uses: appleboy/ssh-action@master | |
id: deploy | |
with: | |
host: ${{ secrets.HOST }} | |
username: ubuntu | |
key: ${{ secrets.KEY }} | |
envs: GITHUB_SHA | |
script: | | |
sudo docker rm -f $(docker ps -qa) | |
sudo docker pull ${{ secrets.DOCKER_REPO }}/directors-dev | |
docker-compose up -d | |
docker image prune -f |