?我的博客是如何进行持续部署的:

接上篇

?如何开发个人博客:

准备

  • 服务器
  • docker
  • dockerHub
  • github
  • nginx

目标

使用githubactions-workflows进行deploy

流程图

graph TD 0.添加配置文件 --> 1.推送代码 --> 2.触发`Action` --> 3.触发`workflow` --> 4.打包镜像 --> 5.推送镜像 --> 6.登录服务器 --> 7.删除原有容器 --> 8.拉取新镜像 --> 9.部署容器 --> 10.使用`nginx`重定向

0.添加配置文件

注册dockerHub 账号

dockerhub

在项目根目录创建.github文件夹

.github文件夹下创建wrokflows文件夹

wrokflows文件夹下创建deploy.yml (deploy名称可自行配置,不与其他冲突即可)

yml 复制代码
# 流程名称
name: Docker Image CI

根据下面的步骤慢慢补充部署文件

Dockerfile配置

如果你也是NextJs 14^可以参考我的配置

yml 复制代码
FROM node:18-alpine AS base

# Install dependencies only when needed
FROM base AS deps
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
# RUN apk add --no-cache libc6-compat
WORKDIR /app

# Install dependencies based on the preferred package manager
COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./
RUN \
  if [ -f yarn.lock ]; then yarn --frozen-lockfile; \
  elif [ -f package-lock.json ]; then npm ci; \
  elif [ -f pnpm-lock.yaml ]; then yarn global add pnpm && pnpm i --frozen-lockfile; \
  else echo "Lockfile not found." && exit 1; \
  fi


# Rebuild the source code only when needed
FROM base AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .

# Next.js collects completely anonymous telemetry data about general usage.
# Learn more here: https://nextjs.org/telemetry
# Uncomment the following line in case you want to disable telemetry during the build.
# ENV NEXT_TELEMETRY_DISABLED 1

RUN yarn build

# If using npm comment out above and use below instead
# RUN npm run build

# Production image, copy all the files and run next
FROM base AS runner
WORKDIR /app

ENV NODE_ENV production
# Uncomment the following line in case you want to disable telemetry during runtime.
# ENV NEXT_TELEMETRY_DISABLED 1

RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs

COPY --from=builder /app/public ./public

# Set the correct permission for prerender cache
RUN mkdir .next
RUN chown nextjs:nodejs .next

# Automatically leverage output traces to reduce image size
# https://nextjs.org/docs/advanced-features/output-file-tracing
#COPY --from=builder --chown=nextjs:nodejs /app/.next ./_next
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./public/_next

USER nextjs

EXPOSE 3000

ENV PORT 3000
# set hostname to localhost
ENV HOSTNAME "0.0.0.0"

CMD ["node", "server.js"]

1.推送代码

基本操作,不做赘述

bash 复制代码
git fetch
git add -A
git commit -m "feat: xxx"
git push

2.触发Action

deploy.yml添加如下代码

yml 复制代码
on:
  push: # push 时触发ci
    branches: [master] # 作用于master分支

检测到 push 操作会触发Action作用于master分支

3.触发workflow

添加环境变量

yml 复制代码
env:
  # 环境变量
  # 镜像名称 可自定义
  IMAGE_NAME: my_blog
  # 容器名称 可自定义
  DOCKER_NAME: myBlog

添加任务

yml 复制代码
# 添加任务
jobs:
    #使用ubuntu环境构建
  build:
    runs-on: ubuntu-latest

4.打包镜像

yml 复制代码
  # 步骤
    steps:
      # 拉取main分支代码
      - name: Checkout
        uses: actions/checkout@v3
      # 制作镜像
      - name: Build the Docker image
        run: docker build . --file Dockerfile --tag ${{env.IMAGE_NAME}}

5.推送镜像

yml 复制代码
      # 登录dockerHub
      - name: Log into registry
        if: github.event_name != 'pull_request'
        uses: docker/login-action@v2
        with:
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_PASSWORD }}
          logout: false
      - name: Extract Docker metadata
        id: meta
        uses: docker/metadata-action@v4
        with:
          images: ${{ env.IMAGE_NAME }}
          tags: |
            type=raw,value=latest,enable={{is_default_branch}}
            type=ref,enable=true,priority=600,prefix=,suffix=,event=tag
      # 推送镜像
      - name: Build and push Docker image
        uses: docker/build-push-action@v3
        with:
          context: .
          push: ${{ github.event_name != 'pull_request' }}
          tags: ${{ secrets.DOCKER_USERNAME }}/${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}

6.登录服务器

yml 复制代码
      # 登录远程服务器,拉取镜像,制作并重启容器
      # https://github.com/marketplace/actions/remote-ssh-commands
      - name: ssh remote deploy
        uses: fifsky/ssh-action@master
        with:
          command: |
            cd /
            echo -e "1.docker login start==>"
            echo ${{ secrets.DOCKER_PASSWORD }} | docker login --username ${{ secrets.DOCKER_USERNAME }} --password-stdin
            
          # --下面填充更多步骤---
          
          host: ${{ secrets.HOST }}
          user: ${{ secrets.USER }}
          pass: ${{ secrets.PASSWORD }}

7.删除原有容器

yml 复制代码
            # 停止容器
            echo -e "2.docker stop container==>"
            docker container stop ${{ env.DOCKER_NAME }}
            
            # 删除容器
            echo -e "3.docker conatainer rm==>"
            docker container rm ${{ env.DOCKER_NAME }}
            
            # 删除原镜像
            echo -e "4.docker image rm==>"
            docker image rm ${{ secrets.DOCKER_USERNAME }}/${{ steps.meta.outputs.tags }}

8.拉取新镜像

yml 复制代码
            # 拉取新镜像
            echo -e "5.docker pull==>"
            docker pull ${{ secrets.DOCKER_USERNAME }}/${{ steps.meta.outputs.tags }}

9.部署容器

yml 复制代码
            # 部署容器
            echo -e "6.docker container create and start==>"
            docker container run -d -p 3000:3000 --name ${{ env.DOCKER_NAME }} ${{ secrets.DOCKER_USERNAME }}/${{ steps.meta.outputs.tags }}

登出docker

yml 复制代码
            # 登出docker
            echo -e "7.docker logout==>"
            docker logout

10.使用nginx重定向

yml 复制代码
server
{
    listen 80;
    server_name your.doman.xx;
    # ...
    location / {
        proxy_pass http://127.0.0.1:3000; # 容器地址
        # ...
    }
    # ...
}

完整deploy.yml

yml 复制代码
# 流程名称
name: Docker Image CI

on:
  push: # push 时触发ci
    branches: [master] # 作用于master分支
env:
  # 环境变量
  # 镜像名称
  IMAGE_NAME: my_blog
  # 容器名称
  DOCKER_NAME: myBlog
# 然后添加任务
jobs:
    #使用ubuntu环境构建
  build:
    runs-on: ubuntu-latest
      # 步骤
    steps:
      # 拉取main分支代码
      - name: Checkout
        uses: actions/checkout@v3
      # 制作镜像
      - name: Build the Docker image
        run: docker build . --file Dockerfile --tag ${{env.IMAGE_NAME}}
      # 登录dockerHub
      - name: Log into registry
        if: github.event_name != 'pull_request'
        uses: docker/login-action@v2
        with:
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_PASSWORD }}
          logout: false
      - name: Extract Docker metadata
        id: meta
        uses: docker/metadata-action@v4
        with:
          images: ${{ env.IMAGE_NAME }}
          tags: |
            type=raw,value=latest,enable={{is_default_branch}}
            type=ref,enable=true,priority=600,prefix=,suffix=,event=tag
      # 推送镜像
      - name: Build and push Docker image
        uses: docker/build-push-action@v3
        with:
          context: .
          push: ${{ github.event_name != 'pull_request' }}
          tags: ${{ secrets.DOCKER_USERNAME }}/${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
      # 登录远程服务器,拉取镜像,制作并重启容器
      # https://github.com/marketplace/actions/remote-ssh-commands
      - name: ssh remote deploy
        uses: fifsky/ssh-action@master
        with:
          command: |
            cd /
            echo -e "1.docker login start==>"
            echo ${{ secrets.DOCKER_PASSWORD }} | docker login --username ${{ secrets.DOCKER_USERNAME }} --password-stdin

            echo -e "2.docker stop container==>"
            docker container stop ${{ env.DOCKER_NAME }}

            echo -e "3.docker conatainer rm==>"
            docker container rm ${{ env.DOCKER_NAME }}

            echo -e "4.docker image rm==>"
            docker image rm ${{ secrets.DOCKER_USERNAME }}/${{ steps.meta.outputs.tags }}

            echo -e "5.docker pull==>"
            docker pull ${{ secrets.DOCKER_USERNAME }}/${{ steps.meta.outputs.tags }}

            echo -e "6.docker container create and start==>"
            docker container run -d -p 3000:3000 --name ${{ env.DOCKER_NAME }} ${{ secrets.DOCKER_USERNAME }}/${{ steps.meta.outputs.tags }}

            echo -e "7.docker logout==>"
            docker logout
          host: ${{ secrets.HOST }}
          user: ${{ secrets.USER }}
          pass: ${{ secrets.PASSWORD }}

注意 ${{ secrets.XXX }} 是需要在github里配置的

配置 Secrets

Secrets 描述
DOCKER_PASSWORD dockerhub密码
DOCKER_USERNAME dockerhub账号
HOST 服务器host
PASSWORD 服务器密码
USER 服务器账号

最后

本教程不保证适合所有人,大家可以参考我的来编写,如有问题十分欢迎在评论区指出和交流!!!

相关推荐
菠萝崽.1 小时前
RabbitMQ高级篇-MQ的可靠性
java·分布式·后端·消息队列·rabbitmq·异步编程
守城小轩2 小时前
JavaScript vs Python 用于 Web Scraping(2025):终极对比指南
前端·chrome·chrome devtools·指纹浏览器·浏览器开发·超级浏览器
风逸hhh5 小时前
python打卡day29@浙大疏锦行
开发语言·前端·python
LuckyLay5 小时前
Vue百日学习计划Day33-35天详细计划-Gemini版
前端·vue.js·学习
ᖰ・◡・ᖳ5 小时前
JavaScript:PC端特效--缓动动画
开发语言·前端·javascript·css·学习·html5
键盘客5 小时前
Spring Boot 配置明文密码加密,防泄漏
java·spring boot·后端·spring
程序员爱钓鱼6 小时前
defer关键字:延迟调用机制-《Go语言实战指南》
开发语言·后端·golang
会飞的鱼先生6 小时前
vue2、vue3项目打包生成txt文件-自动记录打包日期:git版本、当前分支、提交人姓名、提交日期、提交描述等信息 和 前端项目的版本号json文件
前端·vue.js·git·json
!win !6 小时前
uni-app项目从0-1基础架构搭建全流程
前端·uni-app
c_zyer7 小时前
使用 nvm 管理 Node.js 和 npm 版本
前端·npm·node.js