[Deploy] 배포 #50
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: CD | |
on: | |
pull_request: | |
types: [closed] | |
branches: ["main"] | |
permissions: | |
contents: write | |
jobs: | |
deploy: | |
if: github.event.pull_request.merged == true | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v4 | |
- name: Set up JDK | |
uses: actions/setup-java@v4 | |
with: | |
java-version: '21' | |
distribution: 'adopt' | |
- name: Cache Gradle packages | |
uses: actions/cache@v4 | |
with: | |
path: | | |
~/.gradle/caches | |
~/.gradle/wrapper | |
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }} | |
restore-keys: | | |
${{ runner.os }}-gradle- | |
- name: Build with Gradle and run restDocsTest | |
uses: gradle/gradle-build-action@v3 | |
with: | |
arguments: restDocsTest | |
build-root-directory: . | |
- name: List entire directory after restDocsTest | |
run: | | |
echo "Listing entire directory structure after restDocsTest:" | |
find . -type f | |
- name: Build with Gradle and run asciidoctor | |
uses: gradle/gradle-build-action@v3 | |
with: | |
arguments: asciidoctor | |
build-root-directory: . | |
- name: List entire directory after asciidoctor | |
run: | | |
echo "Listing entire directory structure after asciidoctor:" | |
find . -type f | |
- name: Build with Gradle and copy API Documents | |
uses: gradle/gradle-build-action@v3 | |
with: | |
arguments: copyApiDocument | |
build-root-directory: . | |
- name: List entire directory after copyApiDocument | |
run: | | |
echo "Listing entire directory structure after copyApiDocument:" | |
find . -type f | |
- name: Build with Gradle | |
uses: gradle/gradle-build-action@v3 | |
with: | |
arguments: build | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v2 | |
- name: Login to DockerHub | |
uses: docker/login-action@v2 | |
with: | |
username: ${{ secrets.DOCKER_USERNAME }} | |
password: ${{ secrets.DOCKER_PASSWORD }} | |
- name: Build and push Docker image | |
uses: docker/build-push-action@v3 | |
with: | |
context: . | |
file: ./Dockerfile | |
push: true | |
tags: ${{ secrets.DOCKER_USERNAME }}/dev:latest | |
- name: Add SSH key | |
uses: webfactory/[email protected] | |
with: | |
ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }} | |
- name: Add known_hosts | |
run: echo "${{ secrets.SSH_KNOWN_HOSTS }}" >> ~/.ssh/known_hosts | |
- name: Deploy to server | |
run: | | |
ssh ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }} << 'EOF' | |
docker pull ${{ secrets.DOCKER_USERNAME }}/dev:latest | |
docker stop app || true | |
docker rm app || true | |
docker run -d --name app --network monitoring -p 8080:8080 \ | |
--env-file /home/ubuntu/.env \ | |
-v /home/ubuntu/cert:/cert \ | |
-v /var/logs:/logs \ | |
-e TZ=Asia/Seoul \ | |
${{ secrets.DOCKER_USERNAME }}/dev:latest | |
docker ps -a | |
docker logs app | |
EOF |