Github Actions 构建项目记录
前端项目
一般都是vue或者react, 构建的话 都是 npm install
npm run build
首先 设置仓库环境变量
然后就是整理 workflows/xxx.yaml 的执行流程
name: Deploy to Server
on:
push:
branches:
- master # 修改成你的主分支名称
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Install dependencies and build
run: |
npm install --force # 这里假设你的项目是基于 npm 的
npm run build # 根据你的项目配置打包命令
- name: Deploy to server
uses: easingthemes/ssh-deploy@main
with:
SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }}
SOURCE: "dist/"
REMOTE_HOST: ${{ secrets.REMOTE_HOST }}
REMOTE_USER: ${{ secrets.REMOTE_USER }}
TARGET: /opt/xxx/sssxxx
修改之后,查看actions的执行情况
后端Dockerfile构建打包推送更新
后端用的golang项目,项目也很简单,所以直接网上抄了个构建示例
###############################################################################
# 使用第一个镜像打包
###############################################################################
FROM golang:latest as builder
ENV CGO_ENABLED 0
ENV GOPROXY https://goproxy.cn,direct
# 设置固定的打包路径
WORKDIR /build
ADD go.mod .
RUN go mod download
COPY . .
# CGO_ENABLED禁用cgo 然后指定OS等,并go build
RUN go mod tidy
# 依赖 CGO, 修改: CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go build -ldflags '-extldflags "-static"' -o main main.go
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-s -w" -o runner main.go
###############################################################################
# 运行镜像: 从构建镜像中复制构建好的文件
###############################################################################
# 使用空镜像 scratch
# FROM scratch
FROM alpine
# 如果使用 scratch 镜像, 把 RUN 相关的全部移除掉
###############################################################################
# 使用国内 alpine 源
RUN echo https://mirrors.aliyun.com/alpine/v3.8/main/ > /etc/apk/repositories
# 设置系统时区 - +8时区
RUN apk update && apk add tzdata bash
RUN rm -rf /etc/localtime && cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
RUN echo "Asia/Shanghai" > /etc/timezone
# 设置固定的项目路径
ENV WORKDIR /app
# 添加应用可执行文件,并设置执行权限
COPY --from=builder /build/runner $WORKDIR/runner
# 添加I18N多语言文件、静态文件、配置文件、模板文件
COPY --from=builder /build/resource $WORKDIR/resource
COPY --from=builder /build/con.yaml $WORKDIR/con.yaml
# 指定暴露端口, 如果有的话
EXPOSE 8808
###############################################################################
# 运行
###############################################################################
WORKDIR $WORKDIR
CMD ["./runner"]
然后本地构建镜像没问题之后,进行推送
我用的腾讯云免费的个人镜像仓库
docker build -t runner:latest .
docker login ccr.ccs.tencentyun.com --username=xxxxxx
docker tag runner:latest ccr.ccs.tencentyun.com/xxxx/runner:latest
docker push ccr.ccs.tencentyun.com/xxxx/runner:latest
# 登录服务器 拉取
docker pull ccr.ccs.tencentyun.com/xxxxx/runner:latest
# 启动
docker run -d --name bus -p 8808:8808 ccr.ccs.tencentyun.com/xxxx/runner:latest
成功之后,接下来就非常简单了,就是把这些操作转移到github actions上
# This is a basic workflow to help you get started with Actions
name: build docker image
# Controls when the action will run.
on:
push:
branches:
- main
# Allows you to run this workflow manually from the Actions tab
# 可以手动触发
workflow_dispatch:
inputs:
logLevel:
description: "Log level"
required: true
default: "warning"
tags:
description: "Test scenario tags"
jobs:
buildx:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Get current date
id: date
run: echo "::set-output name=today::$(date +'%Y-%m-%d_%H-%M')"
- name: Set up QEMU
uses: docker/setup-qemu-action@v1
- name: Set up Docker Buildx
id: buildx
uses: docker/setup-buildx-action@v1
- name: Available platforms
run: echo ${{ steps.buildx.outputs.platforms }}
- name: Login to Registry
uses: docker/login-action@v1
with:
registry: ${{ secrets.REGISTRY_URL }}
username: ${{ secrets.REGISTRY_USERNAME }}
password: ${{ secrets.REGISTRY_TOKEN }}
- name: Build and push
uses: docker/build-push-action@v2
with:
context: .
file: ./Dockerfile
# 所需要的体系结构,可以在 Available platforms 步骤中获取所有的可用架构
platforms: linux/amd64,linux/arm64/v8
# 镜像推送时间
push: true
# 给清单打上多个标签
tags: |
ccr.ccs.tencentyun.com/xxxxx/runner:${{ steps.date.outputs.today }}
ccr.ccs.tencentyun.com/xxxxx/runner:latest
- name: Docker Pull
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.REMOTE_HOST }}
username: ${{ secrets.REMOTE_USER }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
script: docker pull ccr.ccs.tencentyun.com/xxxxxx/runner:latest && docker restart bus
最后大功告成
唯一美中不足的是,后端的构建时间太久了,有时间再优化吧