Merge pull request #36 from mark-cos/35-seo #12
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
# Sample workflow for building and deploying a Next.js site to GitHub Pages | |
# | |
# To get started with Next.js see: https://nextjs.org/docs/getting-started | |
# | |
name: NextJs build | |
on: | |
# Runs on pushes targeting the default branch | |
push: | |
branches: ['main'] | |
# Allows you to run this workflow manually from the Actions tab | |
workflow_dispatch: | |
env: | |
#필드 시 사용하여 환경변수에 설정 필요 | |
MONGODB_URL: ${{ secrets.MONGODB_URL }} | |
#아래에서 사용하는 DOCKER_BUILD_PATH 경로도 서버에 꼭 만들어야함! | |
# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages | |
permissions: | |
contents: read | |
pages: write | |
id-token: write | |
jobs: | |
# Build job | |
build: | |
runs-on: [self-hosted] | |
container: | |
image: node:20.11-slim | |
options: --user root | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v4 | |
- name: Detect package manager | |
id: detect-package-manager | |
run: | | |
echo ${{ vars.DOCKER_BUILD_PATH }} | |
echo "manager=pnpm" >> $GITHUB_OUTPUT | |
echo "command=install" >> $GITHUB_OUTPUT | |
echo "runner=pnpm" >> $GITHUB_OUTPUT | |
exit 0 | |
- name: Setup pnpm | |
uses: pnpm/action-setup@v2 | |
with: | |
version: 8 | |
- name: Restore cache | |
uses: actions/cache@v4 | |
with: | |
path: | | |
.next/cache | |
# Generate a new cache whenever packages or source files change. | |
key: ${{ runner.os }}-nextjs-${{ hashFiles('**/pnpm-lock.yaml') }}-${{ hashFiles('**.[jt]s', '**.[jt]sx') }} | |
# If source files changed but packages didn't, rebuild from a prior cache. | |
restore-keys: | | |
${{ runner.os }}-nextjs-${{ hashFiles('**/pnpm-lock.yaml') }}- | |
- name: Install dependencies | |
run: ${{ steps.detect-package-manager.outputs.manager }} ${{ steps.detect-package-manager.outputs.command }} | |
- name: Build with Next.js | |
run: | | |
${{ steps.detect-package-manager.outputs.manager }} build | |
docker-build: | |
needs: build | |
runs-on: [self-hosted] | |
permissions: | |
contents: read | |
packages: write | |
steps: | |
- name: Docker Pakaging directory setting | |
run: | | |
rm -rf ${{ vars.DOCKER_BUILD_PATH }}/* | |
cp -R ${{ github.workspace }}/Dockerfile ${{ vars.DOCKER_BUILD_PATH }}/ | |
cp -R ${{ github.workspace }}/.next/* ${{ vars.DOCKER_BUILD_PATH }}/ | |
cp -R ${{ github.workspace }}/.env ${{ vars.DOCKER_BUILD_PATH }}/ | |
mv ${{ vars.DOCKER_BUILD_PATH }}/static ${{ vars.DOCKER_BUILD_PATH }}/standalone/.next/ | |
- name: Log in to the Container registry | |
uses: docker/login-action@v3 | |
with: | |
registry: ghcr.io | |
username: ${{ github.actor }} | |
password: ${{ github.token}} | |
- name: Added environment variable for version. | |
id: version | |
run: | | |
echo "GIT_SHORT_COMMIT_ID=$(git rev-parse —short HEAD)" >> $GITHUB_OUTPUT | |
echo "BUILD_TIME=$(date +'%Y-%m-%d_%H:%M')" >> $GITHUB_OUTPUT | |
- name: Extract metadata (tags, labels) for Docker | |
id: meta | |
uses: docker/metadata-action@v5 | |
with: | |
images: ghcr.io/${{ github.repository }} | |
tags: | | |
type=ref,event=branch | |
type=ref,event=pr | |
type=semver,pattern={{version}} | |
type=semver,pattern={{major}}.{{minor}} | |
- name: Build and push Docker image | |
uses: docker/build-push-action@v5 | |
with: | |
context: ${{ vars.DOCKER_BUILD_PATH }} | |
file: Dockerfile | |
push: true | |
tags: ${{ steps.meta.outputs.tags }} | |
labels: ${{ steps.meta.outputs.labels }} | |
build-args: | | |
NEXTAUTH_SECRET=${{ secrets.NEXTAUTH_SECRET }} | |
MONGODB_URL=${{ secrets.MONGODB_URL }} | |
GOOGLE_CLIENT_SECRET=${{ secrets.GOOGLE_CLIENT_SECRET }} | |
deploy: | |
needs: docker-build | |
runs-on: [self-hosted] | |
steps: | |
- name: Deploy | |
uses: appleboy/ssh-action@master | |
with: | |
host: ${{ secrets.SERVER_HOST }} | |
username: todo | |
port: 30022 | |
key: ${{ secrets.PRIVATE_KEY }} | |
envs: GITHUB_SHA | |
script: | | |
docker stop todo-frontend | |
docker rm todo-frontend | |
docker pull ghcr.io/${{ github.repository }}:main | |
docker run -d -p 13000:3000 --name todo-frontend ghcr.io/${{ github.repository }}:main | |
docker image prune -f |