Skip to content

Commit d94a16f

Browse files
author
jiangwenbin
committed
ci: 改用 docker 方式部署
1 parent 6c90bf1 commit d94a16f

File tree

7 files changed

+2393
-85
lines changed

7 files changed

+2393
-85
lines changed

.dockerignore

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Include any files or directories that you don't want to be copied to your
2+
# container here (e.g., local build artifacts, temporary files, etc.).
3+
#
4+
# For more help, visit the .dockerignore file reference guide at
5+
# https://docs.docker.com/go/build-context-dockerignore/
6+
7+
**/.classpath
8+
**/.dockerignore
9+
**/.git
10+
**/.gitignore
11+
**/.project
12+
**/.settings
13+
**/.toolstarget
14+
**/.vs
15+
**/.vscode
16+
**/.next
17+
**/.cache
18+
**/*.*proj.user
19+
**/*.dbmdl
20+
**/*.jfm
21+
**/charts
22+
**/docker-compose*
23+
**/compose.y*ml
24+
**/Dockerfile*
25+
**/node_modules
26+
**/npm-debug.log
27+
**/obj
28+
**/secrets.dev.yaml
29+
**/values.dev.yaml
30+
**/build
31+
**/dist
32+
LICENSE
33+
README.md

.github/workflows/ci_cd.yml

Lines changed: 15 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
name: ci_cd
22
on:
3-
push:
4-
branches:
5-
- main
3+
release:
4+
types: [created]
5+
66
env:
77
SSH_HOST: ${{secrets.SSH_HOST}}
88
SSH_USERNAME: ${{secrets.SSH_USERNAME}}
@@ -13,54 +13,19 @@ jobs:
1313
runs-on: ubuntu-latest
1414
steps:
1515
- name: Checkout
16-
uses: actions/checkout@v2
16+
uses: actions/checkout@v4
1717

18-
- name: Setup Node
19-
uses: actions/setup-node@v2
20-
with:
21-
node-version: '12.x'
22-
23-
- name: Cache node modules
24-
uses: actions/cache@v2
25-
env:
26-
cache-name: cache-node-modules
27-
with:
28-
# npm cache files are stored in `~/.npm` on Linux/macOS
29-
path: ~/.npm
30-
# 缓存命中
31-
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }}
32-
# 存储key
33-
restore-keys: |
34-
${{ runner.os }}-build-${{ env.cache-name }}-
35-
${{ runner.os }}-build-
36-
${{ runner.os }}-
37-
- name: Install Dependency
18+
- name: Build & Push Image
3819
run: |
39-
yarn config set registry https://registry.npmmirror.com
40-
yarn
41-
42-
- name: Build
43-
run: yarn build
44-
45-
- name: Archive production artifacts
46-
uses: actions/upload-artifact@v2
47-
with:
48-
#产物名称
49-
name: artifact
50-
#产物路径
51-
path: dist
20+
docker login --username=${{secrets.DOCKER_USERNAME}} --password=${{secrets.DOCKER_PASSWORD}} ${{secrets.DOCKER_REGISTRY}}
21+
docker build -t ${{secrets.DOCKER_REGISTRY}}/${{secrets.DOCKER_NAMESPACE}}/${{secrets.DOCKER_REPOSITORY}}:${{github.ref_name}} .
22+
docker push ${{secrets.DOCKER_REGISTRY}}/${{secrets.DOCKER_NAMESPACE}}/${{secrets.DOCKER_REPOSITORY}}:${{github.ref_name}}
5223
5324
deploy:
5425
needs: build
5526
runs-on: ubuntu-latest
5627
steps:
57-
- name: Download production artifacts
58-
uses: actions/download-artifact@v2
59-
with:
60-
name: artifact
61-
path: dist
62-
63-
- name: SSH Auth && Deploy To the Server
28+
- name: SSH Auth && Pull Image
6429
run: |
6530
ssh -V
6631
eval $(ssh-agent -s)
@@ -69,8 +34,9 @@ jobs:
6934
chmod 700 ~/.ssh
7035
echo "PubkeyAcceptedKeyTypes +ssh-rsa" > ~/.ssh/config
7136
ssh-keyscan $SSH_HOST >> ~/.ssh/known_hosts
72-
chmod 644 ~/.ssh/known_hosts
73-
cat ~/.ssh/known_hosts
74-
cat ~/.ssh/config
75-
ls dist
76-
scp -r dist/* $SSH_USERNAME@$SSH_HOST:$DEPLOY_DIR
37+
chmod 644 ~/.ssh/
38+
docker login --username=${{secrets.DOCKER_USERNAME}} --password=${{secrets.DOCKER_PASSWORD}} ${{secrets.DOCKER_REGISTRY}}
39+
docker pull ${{secrets.DOCKER_REGISTRY}}/${{secrets.DOCKER_NAMESPACE}}/${{secrets.DOCKER_REPOSITORY}}:${{github.ref_name}}
40+
docker stop ${{secrets.DOCKER_CONTAINER_NAME}}
41+
docker rm ${{secrets.DOCKER_CONTAINER_NAME}}
42+
docker run -dp ${{secrets.HOST_PORT}}:${{secrets.CONTAINER_PORT}} --name ${{secrets.DOCKER_CONTAINER_NAME}} ${{secrets.DOCKER_NAMESPACE}}/${{secrets.DOCKER_REPOSITORY}}:${{github.ref_name}}

.release-it.cjs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const options = {
2+
git: {
3+
// 具体参考源码 https://github.com/release-it/release-it/blob/master/lib/plugin/git/Git.js
4+
addUntrackedFiles: true,
5+
commitMessage: 'chore: release v${version}',
6+
tagMatch: "[0-9]*"
7+
},
8+
// 默认需要 GITHUB_TOKEN 环境变量,可以通过 tokenRef 定制
9+
github: {
10+
release: true,
11+
},
12+
npm: {
13+
// 不做 npm publish 操作
14+
publish: false,
15+
},
16+
plugins: {
17+
'@release-it/conventional-changelog': {
18+
preset: 'angular',
19+
infile: 'CHANGELOG.md',
20+
},
21+
},
22+
hooks: {
23+
'after:release': 'echo Successfully released ${name} v${version} to ${repo.repository}.',
24+
},
25+
}
26+
27+
module.exports = options

Dockerfile

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# 使用官方Node.js作为基础镜像
2+
FROM node:16 as builder
3+
4+
# 设置工作目录
5+
WORKDIR /app
6+
7+
# 首先复制项目的依赖配置文件
8+
COPY package.json yarn.lock ./
9+
10+
# 安装项目依赖,这一步会生成一个独立的层,并且只有在package.json变化时才会重新执行
11+
RUN yarn install
12+
13+
# 接着复制项目所有文件,这一步会生成一个新的层
14+
COPY . .
15+
16+
# 构建项目,这一步也会生成一个新的层
17+
RUN yarn build
18+
19+
# 使用Nginx镜像来运行构建好的项目
20+
FROM nginx:latest
21+
22+
# 将构建好的项目复制到Nginx镜像的/usr/share/nginx/html目录下
23+
COPY --from=builder /app/dist/ /usr/share/nginx/html
24+
25+
# 复制自定义的Nginx配置到镜像中,覆盖默认配置
26+
COPY nginx/default.conf /etc/nginx/conf.d/default.conf
27+
28+
# 暴露端口
29+
EXPOSE 80

nginx/default.conf

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
server {
2+
listen 80;
3+
root /usr/share/nginx/html;
4+
index index.html;
5+
6+
location / {
7+
try_files $uri $uri/ /index.html;
8+
}
9+
}

package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
"webpack-output": "vue inspect > output.js",
1212
"version": "conventional-changelog -p angular -i CHANGELOG.md -s && git add CHANGELOG.md",
1313
"postinstall": "husky install",
14-
"prepare": "husky install"
14+
"prepare": "husky install",
15+
"create-release": "release-it --ci",
16+
"sync-remote-tags": "git tag -l | xargs git tag -d && git pull --tags"
1517
},
1618
"repository": {
1719
"type": "git",
@@ -45,6 +47,7 @@
4547
"devDependencies": {
4648
"@commitlint/cli": "^13.1.0",
4749
"@commitlint/config-conventional": "^13.1.0",
50+
"@release-it/conventional-changelog": "7.0.2",
4851
"@types/dompurify": "^2.2.2",
4952
"@types/jest": "^24.0.19",
5053
"@types/js-cookie": "^2.2.6",
@@ -77,6 +80,7 @@
7780
"less-loader": "^7.3.0",
7881
"lint-staged": "^9.5.0",
7982
"prettier": "^2.2.1",
83+
"release-it": "16.3.0",
8084
"sass": "^1.26.5",
8185
"sass-loader": "^8.0.2",
8286
"sass-resources-loader": "^2.2.1",
@@ -102,4 +106,4 @@
102106
"path": "./node_modules/cz-conventional-changelog"
103107
}
104108
}
105-
}
109+
}

0 commit comments

Comments
 (0)