接上篇
准备
- 服务器
- docker
- dockerHub
- github
- nginx
目标
使用github
的actions-workflows
进行deploy
流程图
graph TD
0.添加配置文件 --> 1.推送代码 --> 2.触发`Action` --> 3.触发`workflow` --> 4.打包镜像 --> 5.推送镜像 --> 6.登录服务器 --> 7.删除原有容器 --> 8.拉取新镜像 --> 9.部署容器 --> 10.使用`nginx`重定向
0.添加配置文件
注册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 |
服务器账号 |
最后
本教程不保证适合所有人,大家可以参考我的来编写,如有问题十分欢迎在评论区指出和交流!!!