Files
MonkeyCode/.github/workflows/backend-ci-cd.yml
2025-08-08 00:04:57 +08:00

150 lines
4.2 KiB
YAML

name: Backend CI/CD
on:
push:
tags:
- "v[0-9]+.[0-9]+.[0-9]+*"
paths:
- 'backend/**'
- '.github/workflows/backend-ci-cd.yml'
pull_request:
branches:
- main
paths:
- 'backend/**'
- '.github/workflows/backend-ci-cd.yml'
env:
REGISTRY: chaitin-registry.cn-hangzhou.cr.aliyuncs.com/monkeycode
jobs:
test:
runs-on: ubuntu-latest
defaults:
run:
working-directory: ./backend
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.23'
- name: Cache Go modules
uses: actions/cache@v4
with:
path: |
~/.cache/go-build
~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- name: Download dependencies
run: go mod download
- name: Generate Swagger.json
run: |
touch docs/swagger.json
- name: Run tests
run: go test -v ./...
- name: Validate go.mod and go.sum
run: |
go mod tidy
go mod verify
if [ -n "$(git status --porcelain)" ]; then
echo "go.mod or go.sum files are not up to date"
git diff
exit 1
fi
build:
needs: test
runs-on: ubuntu-latest
if: github.event_name != 'pull_request'
defaults:
run:
working-directory: ./backend
outputs:
version: ${{ steps.get_version.outputs.VERSION }}
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
lfs: true
- name: Cache Git LFS
uses: actions/cache@v4
with:
path: .git/lfs
key: ${{ runner.os }}-git-lfs-${{ hashFiles('.gitattributes') }}
restore-keys: |
${{ runner.os }}-git-lfs-
- name: Get version
id: get_version
run: |
VERSION=${GITHUB_REF#refs/tags/}
echo "VERSION=${VERSION}" >> $GITHUB_OUTPUT
- name: Get build time
id: get_build_time
run: |
BUILD_TIME=$(date -u +'%Y-%m-%dT%H:%M:%SZ')
echo "BUILD_TIME=${BUILD_TIME}" >> $GITHUB_OUTPUT
- name: Get git commit
id: get_git_commit
run: |
GIT_COMMIT=$(git rev-parse HEAD)
echo "GIT_COMMIT=${GIT_COMMIT}" >> $GITHUB_OUTPUT
- name: Get VSIX
run: |
mkdir -p assets/vsix
VERSION_NO_V=${{ steps.get_version.outputs.VERSION }}
VERSION_NO_V=${VERSION_NO_V#v}
wget -O assets/vsix/monkeycode-${VERSION_NO_V}.vsix https://release.baizhi.cloud/monkeycode/vsixs/monkeycode-${VERSION_NO_V}.vsix
- name: Get SGP
run: |
mkdir -p assets/sgp
wget -O assets/sgp/sgp https://baizhiyun.oss-cn-hangzhou.aliyuncs.com/prod/sgp/x86_64/sgp
wget -O assets/sgp/sgp-rules https://baizhiyun.oss-cn-hangzhou.aliyuncs.com/prod/sgp/x86_64/sgp-rules
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to Aliyun Container Registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ secrets.CT_ALIYUN_USER }}
password: ${{ secrets.CT_ALIYUN_PASS }}
- name: Build and push backend image
uses: docker/build-push-action@v5
with:
context: ./backend
file: ./backend/build/Dockerfile
push: true
platforms: linux/amd64, linux/arm64
tags: |
${{ env.REGISTRY }}/backend:${{ steps.get_version.outputs.VERSION }}
${{ env.REGISTRY }}/backend:latest
build-args: |
GOCACHE=/tmp/go-build
GOMODCACHE=/tmp/go-mod
REPO_COMMIT=${{ github.sha }}
VERSION=${{ steps.get_version.outputs.VERSION }}
BUILD_TIME=${{ steps.get_build_time.outputs.BUILD_TIME }}
GIT_COMMIT=${{ steps.get_git_commit.outputs.GIT_COMMIT }}
cache-from: type=gha
cache-to: type=gha,mode=max