Skip to content

Commit

Permalink
Branch update에 따라 자동 배포 구현
Browse files Browse the repository at this point in the history
Branch update에 따라 자동 배포 구현
  • Loading branch information
pyo-sh authored Aug 11, 2023
2 parents f1eb865 + 9209eb5 commit 1b66498
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
/.git
/.github
/dist
78 changes: 78 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
name: deploy

on:
push:
branches: [deploy]
pull_request:
branches: [deploy]

jobs:
build:
runs-on: ubuntu-latest
outputs:
version: ${{ steps.meta.outputs.version }}

steps:
- uses: actions/checkout@v2

- name: Create .env file
run: |
jq -r 'to_entries|map("\(.key)=\(.value|tostring)")|.[]' <<< "$SECRETS_CONTEXT" > .env
env:
SECRETS_CONTEXT: ${{ toJson(secrets) }}

- name: Docker Hub Login
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKER_HUB_USERNAME }}
password: ${{secrets.DOCKER_HUB_TOKEN}}

- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v4
with:
images: ${{ secrets.DOCKER_HUB_NAMESPACE }}/${{ secrets.DOCKER_HUB_REPO }}

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

- name: Build and push
uses: docker/build-push-action@v4
with:
context: .
push: true
platforms: ${{ vars.DOCKER_SERVER_PLATFORM }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}

deploy:
needs: build
runs-on: self-hosted

steps:
- name: Stop docker container
continue-on-error: true
run: |
docker stop plandar-server-container
docker rm plandar-server-container
- name: Delete docker images
continue-on-error: true
env:
NAME: ${{ secrets.DOCKER_HUB_NAMESPACE }}
REPO: ${{ secrets.DOCKER_HUB_REPO }}
run: docker images | grep $NAME/$REPO | tr -s ' ' | cut -d ' ' -f 2 | xargs -I {} docker rmi $NAME/$REPO:{}

- name: Pull docker image
env:
NAME: ${{ secrets.DOCKER_HUB_NAMESPACE }}
REPO: ${{ secrets.DOCKER_HUB_REPO }}
VERS: ${{ needs.build.outputs.version }}
run: docker pull $NAME/$REPO:$VERS

- name: Run docker container
env:
NAME: ${{ secrets.DOCKER_HUB_NAMESPACE }}
REPO: ${{ secrets.DOCKER_HUB_REPO }}
VERS: ${{ needs.build.outputs.version }}
run: docker run --name plandar-server-container -p 4000:4000 -d --link plandar-db-container $NAME/$REPO:$VERS
25 changes: 25 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
FROM node AS build

WORKDIR /app

COPY ["package.json", "package-lock.json", "./"]
RUN ["npm", "install"]

COPY ["tsconfig.build.json", "tsconfig.json", "./"]
COPY ["nest-cli.json", "./"]
COPY ["src/", "./src/"]
COPY [".env", "./"]
RUN ["npm", "run", "build"]

RUN ["/bin/sh", "-c", "find . ! -name dist ! -name node_modules ! -name .env -maxdepth 1 -mindepth 1 -exec rm -rf {} \\;"]

FROM node

WORKDIR /app

COPY --from=build /app .

ENV NODE_ENV production
EXPOSE 4000/tcp

ENTRYPOINT ["node", "./dist/main"]

0 comments on commit 1b66498

Please sign in to comment.