diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 330bbf0..85ccf56 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -1,41 +1,61 @@ -# This is a basic workflow to help you get started with Actions +# 构建 VitePress 站点并将其部署到 GitHub Pages 的示例工作流程 -name: deploy-docs-website +name: Deploy VitePress site to Pages -# Controls when the workflow will run on: - # Triggers the workflow on push or pull request events but only for the "main" branch + # 在针对 `main` 分支的推送上运行 push: - branches: ["main"] - # pull_request: - # branches: [ "main" ] + branches: [main] - # Allows you to run this workflow manually from the Actions tab + # 允许你从 Actions 选项卡手动运行此工作流程 workflow_dispatch: -# A workflow run is made up of one or more jobs that can run sequentially or in parallel +# 设置 GITHUB_TOKEN 的权限,以允许部署到 GitHub Pages +permissions: + contents: read + pages: write + id-token: write + +# 只允许同时进行一次部署,跳过正在运行和最新队列之间的运行队列 +# 但是,不要取消正在进行的运行,因为我们希望允许这些生产部署完成 +concurrency: + group: pages + cancel-in-progress: false + jobs: - # This workflow contains a single job called "build" + # 构建工作 build: - # The type of runner that the job will run on runs-on: ubuntu-latest - - # Steps represent a sequence of tasks that will be executed as part of the job steps: - # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it - - uses: actions/checkout@v2 - - name: install nodejs - uses: actions/setup-node@v3.5.0 + - name: Checkout + uses: actions/checkout@v4 with: - node-version: "16.X" - - name: install deps - run: npm install - - name: build app + fetch-depth: 0 # 如果未启用 lastUpdated,则不需要 + - name: Setup Node + uses: actions/setup-node@v4 + with: + node-version: 20 + cache: npm + - name: Setup Pages + uses: actions/configure-pages@v4 + - name: Install dependencies + run: npm ci + - name: Build with VitePress run: npm run docs:build - - name: Deploy - uses: peaceiris/actions-gh-pages@v3 + - name: Upload artifact + uses: actions/upload-pages-artifact@v3 with: - publish_dir: docs/.vitepress/dist - github_token: ${{ secrets.ACTION_TOKEN }} - commit_message: 自动部署 # 5.部署时的 git 提交信息,自由填写 + path: docs/.vitepress/dist + # 部署工作 + deploy: + environment: + name: github-pages + url: ${{ steps.deployment.outputs.page_url }} + needs: build + runs-on: ubuntu-latest + name: Deploy + steps: + - name: Deploy to GitHub Pages + id: deployment + uses: actions/deploy-pages@v4 diff --git a/.github/workflows/deploy_old.yml b/.github/workflows/deploy_old.yml new file mode 100644 index 0000000..459551d --- /dev/null +++ b/.github/workflows/deploy_old.yml @@ -0,0 +1,41 @@ +# This is a basic workflow to help you get started with Actions + +name: deploy-docs-website + +# Controls when the workflow will run +#on: + # Triggers the workflow on push or pull request events but only for the "main" branch + #push: + # branches: ["main"] + # pull_request: + # branches: [ "main" ] + + # Allows you to run this workflow manually from the Actions tab + workflow_dispatch: + +# A workflow run is made up of one or more jobs that can run sequentially or in parallel +jobs: + # This workflow contains a single job called "build" + build: + # The type of runner that the job will run on + runs-on: ubuntu-latest + + # Steps represent a sequence of tasks that will be executed as part of the job + steps: + # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it + - uses: actions/checkout@v2 + - name: install nodejs + uses: actions/setup-node@v3.5.0 + with: + node-version: "16.X" + - name: install deps + run: npm install + - name: build app + run: npm run docs:build + - name: Deploy + uses: peaceiris/actions-gh-pages@v3 + with: + publish_dir: docs/.vitepress/dist + github_token: ${{ secrets.ACTION_TOKEN }} + commit_message: 自动部署 # 5.部署时的 git 提交信息,自由填写 + diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml index 38edcf3..d467d99 100644 --- a/.github/workflows/pr.yml +++ b/.github/workflows/pr.yml @@ -6,7 +6,7 @@ name: docs-pr on: # Triggers the workflow on push or pull request events but only for the "main" branch pull_request: - branches: [ "main" ] + branches: ["main"] # Allows you to run this workflow manually from the Actions tab workflow_dispatch: @@ -22,11 +22,25 @@ jobs: steps: # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it - uses: actions/checkout@v2 - - name: install nodejs + - name: Install nodejs uses: actions/setup-node@v3.5.0 - with: + with: node-version: "16.X" - - name: install deps + - name: Install deps run: npm install - - name: build app + - name: Build docs run: npm run docs:build + - name: Check Build Status + id: build_status + run: echo "::set-output name=status::success" + + merge: + needs: build + runs-on: ubuntu-latest + + steps: + - name: Merge PR + if: ${{ needs.build.outputs.status == 'success' }} + uses: pullreminders/merge-me-action@v2 + with: + github_token: ${{ secrets.ACTION_TOKEN }} diff --git a/docs/program/Docker/index.md b/docs/program/Docker/index.md index d1bd120..43504b0 100644 --- a/docs/program/Docker/index.md +++ b/docs/program/Docker/index.md @@ -22,6 +22,77 @@ Docker 容器是从镜像中创建的运行实例。当启动容器时,会从 去[官网](https://www.docker.com/)安装即可 +## 实例 + +### 构建镜像 + +首先在项目根目录下创建`Dockerfile`文件。表明要构建得得内容。 + +当前 docs 项目得文件如下,先使用 node 镜像构建项目,在使用 nginx 来提供 web 服务器 + +```dockerfile +# 使用 Node.js 官方提供的 Node 镜像作为基础镜像来构建项目 +FROM node:14-alpine AS build + +# 设置工作目录 +WORKDIR /app + +# 复制 package.json 和 package-lock.json 文件到工作目录 +COPY package*.json ./ + +# 安装依赖 +RUN npm install + +# 复制所有文件到工作目录 +COPY . . + +# 构建项目 +RUN npm run docs:build + +# 使用 Nginx 官方提供的 Nginx 镜像作为基础镜像 +FROM nginx:alpine + +# 将构建后的项目文件复制到 Nginx 默认的静态文件目录 +COPY --from=build /app/docs/.vitepress/dist /usr/share/nginx/html + +# 暴露 Nginx 的默认端口 +EXPOSE 80 + +# 启动 Nginx +CMD ["nginx", "-g", "daemon off;"] +``` + +在当前目录下执行如下命令,-t docs-1 指定镜像名为 `docs-1`,`.` 表示 Dockerfile 得路径,当前就是根目录。执行后 docker 会按照 Dockerfile 文件构建镜像 + +```bash +docker build -t docs-1 . +``` + +### 容器 + +拥有镜像后就可以启动一个容器了,执行下面命令。 `-p 8080:80` 表明将容器得`80`端口映射到主机得`8080`端口上,docs-1 即是刚刚创建得镜像。执行后就可以在浏览器输入`localhost:8080`访问项目 + +```bash +docker run -p 8080:80 docs-1 +``` + +## 忽略文件 + +`.dockerignore`文件时 docker 得忽略文件,在镜像构建可以忽略要 COPY 得文件。书写格式同`gitignore`。 + +```git +node_modules +.temp +cache +dist +``` + +## 分段构建 + +Docker 得分段构建指在 Dockerfile 文件中定义多个构建阶段,每个阶段可以使用不同得基础镜像,同时可以共享文件。这样可以减少最终镜像得大小,在构建过程中丢弃不需要得文件和依赖项。 + +比如上面的 Dockerfile 文件就包含了两个构建阶段,第一阶段使用 node 镜像来构建本项目,第二阶段使用 nginx 镜像来作为 web 服务器。最终构建的镜像就只包含 nginx 和构建后的文件,没有 node 镜像和相关工具,减少了镜像的大小和安全性。 + ::: tip 提示 未完待续 :::