docs: README.md 수정 #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: 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 |