-
Notifications
You must be signed in to change notification settings - Fork 1
141 lines (122 loc) · 4.55 KB
/
build-ci.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# 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: ['cicd']
# 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