81 lines
2.5 KiB
YAML
81 lines
2.5 KiB
YAML
name: ghcr镜像构建
|
|
on:
|
|
workflow_dispatch:
|
|
inputs:
|
|
version:
|
|
description: 'Version number'
|
|
required: true
|
|
default: 'latest'
|
|
|
|
jobs:
|
|
build:
|
|
runs-on: ubuntu-latest
|
|
permissions:
|
|
contents: write
|
|
packages: write
|
|
steps:
|
|
- name: Checkout repository
|
|
uses: actions/checkout@v4
|
|
|
|
- name: 设置Go环境
|
|
uses: actions/setup-go@v5
|
|
with:
|
|
go-version-file: "src/go.mod"
|
|
cache-dependency-path: "src/go.sum"
|
|
|
|
- name: 安装 UPX
|
|
uses: crazy-max/ghaction-upx@v3
|
|
with:
|
|
install-only: true
|
|
|
|
- name: 编译二进制文件
|
|
run: |
|
|
cd src
|
|
|
|
# Linux AMD64
|
|
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-s -w" -o ../build/hubproxy/hubproxy-linux-amd64 .
|
|
|
|
# Linux ARM64
|
|
CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -ldflags="-s -w" -o ../build/hubproxy/hubproxy-linux-arm64 .
|
|
|
|
# 压缩二进制文件
|
|
upx -9 ../build/hubproxy/hubproxy-linux-amd64
|
|
upx -9 ../build/hubproxy/hubproxy-linux-arm64
|
|
|
|
- name: Set up Docker Buildx
|
|
uses: docker/setup-buildx-action@v3
|
|
|
|
- name: Cache Docker layers
|
|
uses: actions/cache@v4
|
|
with:
|
|
path: /tmp/.buildx-cache
|
|
key: ${{ runner.os }}-buildx-${{ github.sha }}
|
|
restore-keys: |
|
|
${{ runner.os }}-buildx-
|
|
|
|
- name: Log in to GitHub Docker Registry
|
|
uses: docker/login-action@v3
|
|
with:
|
|
registry: ghcr.io
|
|
username: ${{ github.actor }}
|
|
password: ${{ secrets.GITHUB_TOKEN }}
|
|
|
|
- name: Set version from input
|
|
run: echo "VERSION=${{ github.event.inputs.version }}" >> $GITHUB_ENV
|
|
|
|
- name: Convert repository name to lowercase
|
|
run: |
|
|
# 将 github.repository 整体转换为小写
|
|
REPO_LOWER=$(echo "${{ github.repository }}" | tr '[:upper:]' '[:lower:]')
|
|
echo "REPO_LOWER=$REPO_LOWER" >> $GITHUB_ENV
|
|
|
|
- name: Build and push Docker image
|
|
run: |
|
|
docker buildx build --push \
|
|
--platform linux/amd64,linux/arm64 \
|
|
--tag ghcr.io/${{ env.REPO_LOWER }}:${{ env.VERSION }} \
|
|
--tag ghcr.io/${{ env.REPO_LOWER }}:latest \
|
|
--build-arg VERSION=${{ env.VERSION }} \
|
|
-f Dockerfile .
|
|
env:
|
|
GHCR_PUBLIC: true # 将镜像设置为公开 |