Skip to content

Commit

Permalink
prod: docker ci/cd 구성
Browse files Browse the repository at this point in the history
  • Loading branch information
gengminy committed Nov 30, 2023
1 parent 2f26b5a commit 24d457c
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## 작업사항
- 내용을 적어주세요.

## 변경로직
- 내용을 적어주세요.
76 changes: 76 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
name: Build And Deploy for Api Server

on:
push:
branches: ['dev']

env:
ACTIVE_PROFILE: "dev"
DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}
DOCKERHUB_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }}
DOCKERHUB_API_IMAGE_NAME: ${{ secrets.DOCKERHUB_API_IMAGE_NAME }}

jobs:
build-and-push:
runs-on: ubuntu-latest
strategy:
matrix:
java-version: [ 17 ]
outputs:
image_tag: ${{ steps.image_tag.outputs.image_tag }}

steps:
- name: Check Out The Repository
uses: actions/checkout@v3

- name: Set up Java
uses: actions/setup-java@v3
with:
java-version: ${{ matrix.java-version }}
distribution: 'corretto'

- name: Grant execute permission for gradlew
run: chmod +x ./gradlew

- name: Execute Gradle build
run: |
./gradlew build --no-daemon
- name: Make image tag
run: echo "IMAGE_TAG=$ACTIVE_PROFILE-${GITHUB_SHA::7}" >> $GITHUB_ENV

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

- name: Build docker image and push image
run: |
docker login -u $DOCKERHUB_USERNAME -p $DOCKERHUB_TOKEN
docker build --build-arg PROFILE=$ACTIVE_PROFILE -t $DOCKERHUB_USERNAME/$DOCKERHUB_API_IMAGE_NAME:${{env.IMAGE_TAG}} .
docker push $DOCKERHUB_USERNAME/$DOCKERHUB_API_IMAGE_NAME:${{env.IMAGE_TAG}}
- name: Pass Image tag value
id: image_tag
run: echo "image_tag=${{env.IMAGE_TAG}}" >> $GITHUB_OUTPUT

deploy:
needs: build-and-push
runs-on: ubuntu-latest
steps:
- name: Set image tag from Output
run: echo "IMAGE_TAG=${{ needs.build-and-push.outputs.image_tag }}" >> $GITHUB_ENV

- name: SSH remote and Run Docker container
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.REMOTE_IP }}
username: ${{ secrets.REMOTE_USERNAME }}
key: ${{ secrets.REMOTE_SSH_KEY }}
port: ${{ secrets.REMOTE_SSH_PORT }}
script: |
sudo touch .env
echo "${{ secrets.ENV_VARS }}" | sudo tee .env > /dev/null
docker pull ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_API_IMAGE_NAME }}:${{env.IMAGE_TAG}}
docker stop $(docker ps -a -q)
docker run --restart=unless-stopped -d -p 8080:8080 --env-file .env --add-host=host.docker.internal:host-gateway ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_API_IMAGE_NAME }}:${{env.IMAGE_TAG}}
docker rm $(docker ps --filter 'status=exited' -a -q)
docker image prune -a -f
7 changes: 1 addition & 6 deletions src/test/java/com/testcar/car/CarApplicationTests.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
package com.testcar.car;


import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class CarApplicationTests {

@Test
void contextLoads() {}
}
class CarApplicationTests {}

0 comments on commit 24d457c

Please sign in to comment.