From 5de311206edcaa02dc64f38953a4bc7d0c1e9cd0 Mon Sep 17 00:00:00 2001 From: jklolixxs Date: Sat, 30 Nov 2024 06:31:35 +0000 Subject: [PATCH] Build to v1.11.0-beta.4 by Github Actions, 2024/11/30 --- .github/workflows/build.yml | 405 ++++++++++++++++++++++++++++++++++++ Dockerfile | 26 +++ README.md | 38 ++++ 3 files changed, 469 insertions(+) create mode 100644 .github/workflows/build.yml create mode 100644 Dockerfile create mode 100644 README.md diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..44e6da5 --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,405 @@ +name: "Build main-next and dev-next images & releases" + +on: + workflow_dispatch: + schedule: + - cron: "0 */1 * * *" + +permissions: + contents: write + packages: write + actions: write + +jobs: + Check-version: + runs-on: ubuntu-latest + name: "Check version" + outputs: + main_next_now: ${{ steps.get-version.outputs.main_next_now }} + main_next_new: ${{ steps.get-version.outputs.main_next_new }} + main_next_renew: ${{ steps.get-version.outputs.main_next_renew }} + dev_next_now: ${{ steps.get-version.outputs.dev_next_now }} + dev_next_new: ${{ steps.get-version.outputs.dev_next_new }} + dev_next_renew: ${{ steps.get-version.outputs.dev_next_renew }} + commit: ${{ steps.get-version.outputs.commit }} + main_next_go_version: ${{ steps.get-version.outputs.main_next_go_version }} + dev_next_go_version: ${{ steps.get-version.outputs.dev_next_go_version }} + steps: + - name: Checkout code + uses: actions/checkout@v4.1.1 + with: + fetch-depth: 0 + + - name: Check version + id: get-version + run: | + MAIN_NEXT_NOW=$(awk '/latest/{print $NF; exit}' README.md) + DEV_NEXT_NOW=$(awk '/dev-next/{print $NF; exit}' README.md) + + LIST=$(wget -qO- --tries=3 "https://api.github.com/repos/SagerNet/sing-box/releases" | awk -F '"' '/tag_name/{print $(NF-1)}') + MAIN_NEXT_NEW=$(grep -vm1 '-' <<< "$LIST") + DEV_NEXT_NEW=$(grep -m1 '-' <<< "$LIST") + # GO_VERSION=$(curl -s https://go.dev/VERSION?m=text | awk 'NR==1' | sed 's/^go//') + + MAIN_NEXT_GO_VERSION_TEST=$(curl -s https://raw.githubusercontent.com/SagerNet/sing-box/main-next/Dockerfile | awk 'NR==1' | sed -n 's/.*golang:\(.*\)-alpine.*/\1/p') + if [[ $(grep -o "\." <<< "$MAIN_NEXT_GO_VERSION_TEST" | wc -l) -eq 1 ]]; then + MAIN_NEXT_GO_VERSION_TEMP=$(curl -s "https://go.dev/dl/?mode=json" | jq -r --arg version "go$MAIN_NEXT_GO_VERSION_TEST" '.[] | select(.version | startswith($version)) | .version' | sort -V | tail -n 1) + MAIN_NEXT_GO_VERSION=${MAIN_NEXT_GO_VERSION_TEMP#go} + else + MAIN_NEXT_GO_VERSION=$MAIN_NEXT_GO_VERSION_TEST + fi + + DEV_NEXT_GO_VERSION_TEST=$(curl -s https://raw.githubusercontent.com/SagerNet/sing-box/dev-next/Dockerfile | awk 'NR==1' | sed -n 's/.*golang:\(.*\)-alpine.*/\1/p') + if [[ $(grep -o "\." <<< "$DEV_NEXT_GO_VERSION_TEST" | wc -l) -eq 1 ]]; then + DEV_NEXT_GO_VERSION_TEMP=$(curl -s "https://go.dev/dl/?mode=json" | jq -r --arg version "go$DEV_NEXT_GO_VERSION_TEST" '.[] | select(.version | startswith($version)) | .version' | sort -V | tail -n 1) + DEV_NEXT_GO_VERSION=${MAIN_NEXT_GO_VERSION_TEMP#go} + else + DEV_NEXT_GO_VERSION=$DEV_NEXT_GO_VERSION_TEST + fi + + + echo "MAIN_NEXT_NOW: $MAIN_NEXT_NOW" + echo "MAIN_NEXT_NEW: $MAIN_NEXT_NEW" + echo "MAIN_NEXT_GO_VERSION: $MAIN_NEXT_GO_VERSION" + echo "DEV_NEXT_NOW: $DEV_NEXT_NOW" + echo "DEV_NEXT_NEW: $DEV_NEXT_NEW" + echo "DEV_NEXT_GO_VERSION: $DEV_NEXT_GO_VERSION" + + if [ "$MAIN_NEXT_NOW" != "$MAIN_NEXT_NEW" ]; then + MAIN_NEXT_RENEW=1 + fi + if [ "$DEV_NEXT_NOW" != "$DEV_NEXT_NEW" ]; then + DEV_NEXT_RENEW=1 + fi + + if [ "$MAIN_NEXT_NOW" != "$MAIN_NEXT_NEW" ]; then + echo "main_next_now=$MAIN_NEXT_NOW" >> $GITHUB_OUTPUT + echo "main_next_new=$MAIN_NEXT_NEW" >> $GITHUB_OUTPUT + echo "main_next_renew=$MAIN_NEXT_RENEW" >> $GITHUB_OUTPUT + echo "main_next_go_version=$MAIN_NEXT_GO_VERSION" >> $GITHUB_OUTPUT + fi + if [ "$DEV_NEXT_NOW" != "$DEV_NEXT_NEW" ]; then + echo "dev_next_now=$DEV_NEXT_NOW" >> $GITHUB_OUTPUT + echo "dev_next_new=$DEV_NEXT_NEW" >> $GITHUB_OUTPUT + echo "dev_next_renew=$DEV_NEXT_RENEW" >> $GITHUB_OUTPUT + echo "dev_next_go_version=$DEV_NEXT_GO_VERSION" >> $GITHUB_OUTPUT + fi + + DATE=$(date "+%Y/%m/%d") + if [[ "$MAIN_NEXT_RENEW" == '1' && "$DEV_NEXT_RENEW" == '1' ]]; then + COMMIT="Build to $MAIN_NEXT_NEW and $DEV_NEXT_NEW by Github Actions, $DATE" + echo "commit=$COMMIT" >> $GITHUB_OUTPUT + elif [ "$MAIN_NEXT_RENEW" == '1' ]; then + COMMIT="Build to $MAIN_NEXT_NEW by Github Actions, $DATE" + echo "commit=$COMMIT" >> $GITHUB_OUTPUT + elif [ "$DEV_NEXT_RENEW" == '1' ]; then + COMMIT="Build to $DEV_NEXT_NEW by Github Actions, $DATE" + echo "commit=$COMMIT" >> $GITHUB_OUTPUT + fi + # ============================================================================================================================================================================================= + Create-release: + needs: Check-version + runs-on: ubuntu-latest + name: "Create release" + env: + MAIN_NEXT_VERSION: ${{ needs.Check-version.outputs.main_next_new }} + MAIN_NEXT_RENEW: ${{ needs.Check-version.outputs.main_next_renew }} + DEV_NEXT_VERSION: ${{ needs.Check-version.outputs.dev_next_new }} + DEV_NEXT_RENEW: ${{ needs.Check-version.outputs.dev_next_renew }} + steps: + - name: Checkout code + uses: actions/checkout@v4.1.1 + with: + fetch-depth: 0 + + - name: Create main-next release + if: ${{ env.MAIN_NEXT_RENEW == '1' }} + uses: shogo82148/actions-create-release@v1.6.0 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + tag_name: ${{ env.MAIN_NEXT_VERSION }} + release_name: ${{ env.MAIN_NEXT_VERSION }} + draft: false + prerelease: false + overwrite: true + + - name: Create dev-next release + if: ${{ env.DEV_NEXT_RENEW == '1' }} + uses: shogo82148/actions-create-release@v1.6.0 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + tag_name: ${{ env.DEV_NEXT_VERSION }} + release_name: ${{ env.DEV_NEXT_VERSION }} + draft: false + prerelease: true + overwrite: true + # ============================================================================================================================================================================================= + Build-main-next-image: + needs: Check-version + runs-on: ubuntu-latest + name: "Build main-next image" + env: + DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }} + DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }} + DOCKERHUB_REPOSITORY: ${{ secrets.DOCKER_USERNAME }}/${{ secrets.DOCKER_REPO }} + VERSION: ${{ needs.Check-version.outputs.main_next_new }} + + steps: + - name: Checkout code + if: ${{ env.VERSION != '' }} + uses: actions/checkout@v4.1.1 + with: + fetch-depth: 0 + + - name: Set up QEMU + if: ${{ env.VERSION != '' }} + uses: docker/setup-qemu-action@v3.0.0 + + - name: Set up Docker Buildx + if: ${{ env.VERSION != '' }} + uses: docker/setup-buildx-action@v3.0.0 + + - name: Login to DockerHub + if: ${{ env.VERSION != '' }} + uses: docker/login-action@v3.0.0 + with: + username: ${{ env.DOCKER_USERNAME }} + password: ${{ env.DOCKER_PASSWORD }} + + - name: Push images to Docker hub + if: ${{ env.VERSION != '' }} + uses: docker/build-push-action@v5.1.0 + with: + push: true + platforms: linux/i386, linux/amd64, linux/arm/v7, linux/arm64 + build-args: BRANCH=${{ env.VERSION }} + tags: | + ${{ env.DOCKERHUB_REPOSITORY }}:latest + ${{ env.DOCKERHUB_REPOSITORY }}:${{ env.VERSION }} + # ============================================================================================================================================================================================= + Build-main-next-releases: + needs: Check-version + runs-on: ubuntu-latest + name: Build main-next releases + env: + CGO_ENABLED: 0 + BINARY: sing-box + VERSION: ${{ needs.Check-version.outputs.main_next_new }} + MAIN_NEXT_GO_VERSION: ${{ needs.Check-version.outputs.main_next_go_version }} + + strategy: + matrix: + goos: [linux, windows, darwin] + goarch: ["386", amd64, arm, arm64] + exclude: + - goarch: 386 + goos: linux + - goarch: arm + goos: linux + - goarch: "386" + goos: darwin + - goarch: "arm" + goos: darwin + - goarch: "386" + goos: windows + - goarch: arm + goos: windows + + steps: + - name: Checkout code + if: ${{ env.VERSION != '' }} + uses: actions/checkout@v4.1.1 + with: + fetch-depth: 0 + + - name: Git clone repository + if: ${{ env.VERSION != '' }} + uses: actions/checkout@v4 + with: + repository: "SagerNet/sing-box" + ref: "${{ env.VERSION }}" + fetch-depth: 1 + + - name: Build main-next releases + if: ${{ env.VERSION != '' }} + uses: fscarmen/go-release-action@v1.0.0 + with: + github_token: ${{ secrets.GITHUB_TOKEN }} # 你的 GITHUB_TOKEN 用于将版本上传到 Github Release。 + goos: ${{ matrix.goos }} # 运行程序的操作系统 + goarch: ${{ matrix.goarch }} # 运行程序的架构 + goversion: ${{ env.MAIN_NEXT_GO_VERSION }} # Go 编译环境版本 + project_path: "./cmd/sing-box" # 在哪里运行 go build 命令。 + binary_name: "${{ env.BINARY }}" # 二进制名称 + build_flags: "-trimpath -tags with_quic,with_grpc,with_dhcp,with_wireguard,with_ech,with_utls,with_reality_server,with_acme,with_clash_api,with_gvisor" # 传递给 go build 命令的其他参数 + ldflags: -s -w --extldflags '-static -fpic' -X github.com/sagernet/sing-box/constant.Version=${{ env.VERSION }} # ldflags参数 + md5sum: "false" + release_tag: "${{ env.VERSION }}" # 上传的标签 + release_name: "${{ env.VERSION }}" # 上传的名字 + compress_assets: "auto" # 自定义打包 + overwrite: true # 覆盖 + asset_name: "${{ env.BINARY }}-${{ matrix.goos }}-${{ matrix.goarch }}" # 自定义名称 + # ============================================================================================================================================================================================= + Build-dev-next-image: + needs: Check-version + runs-on: ubuntu-latest + name: "Build dev-next image" + env: + DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }} + DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }} + DOCKERHUB_REPOSITORY: ${{ secrets.DOCKER_USERNAME }}/${{ secrets.DOCKER_REPO }} + VERSION: ${{ needs.Check-version.outputs.dev_next_new }} + + steps: + - name: Checkout code + if: ${{ env.VERSION != '' }} + uses: actions/checkout@v4.1.1 + with: + fetch-depth: 0 + + - name: Set up QEMU + if: ${{ env.VERSION != '' }} + uses: docker/setup-qemu-action@v3.0.0 + + - name: Set up Docker Buildx + if: ${{ env.VERSION != '' }} + uses: docker/setup-buildx-action@v3.0.0 + + - name: Login to DockerHub + if: ${{ env.VERSION != '' }} + uses: docker/login-action@v3.0.0 + with: + username: ${{ env.DOCKER_USERNAME }} + password: ${{ env.DOCKER_PASSWORD }} + + - name: Push images to Docker hub + if: ${{ env.VERSION != '' }} + uses: docker/build-push-action@v5.1.0 + with: + push: true + platforms: linux/i386, linux/amd64, linux/arm/v7, linux/arm64 + build-args: BRANCH=${{ env.VERSION }} + tags: | + ${{ env.DOCKERHUB_REPOSITORY }}:dev-next + ${{ env.DOCKERHUB_REPOSITORY }}:${{ env.VERSION }} + # ============================================================================================================================================================================================= + Build-dev-next-releases: + needs: Check-version + runs-on: ubuntu-latest + name: Build dev-next releases + env: + CGO_ENABLED: 0 + BINARY: sing-box + VERSION: ${{ needs.Check-version.outputs.dev_next_new }} + DEV_NEXT_GO_VERSION: ${{ needs.Check-version.outputs.dev_next_go_version }} + + strategy: + matrix: + goos: [linux, windows, darwin] + goarch: ["386", amd64, arm, arm64] + exclude: + - goarch: 386 + goos: linux + - goarch: arm + goos: linux + - goarch: "386" + goos: darwin + - goarch: "arm" + goos: darwin + - goarch: "386" + goos: windows + - goarch: arm + goos: windows + + steps: + - name: Checkout code + if: ${{ env.VERSION != '' }} + uses: actions/checkout@v4.1.1 + with: + fetch-depth: 0 + + - name: Git clone repository + if: ${{ env.VERSION != '' }} + uses: actions/checkout@v4 + with: + repository: "SagerNet/sing-box" + ref: "${{ env.VERSION }}" + fetch-depth: 1 + + - name: Build dev-next releases + if: ${{ env.VERSION != '' }} + uses: fscarmen/go-release-action@v1.0.0 + with: + github_token: ${{ secrets.GITHUB_TOKEN }} # 你的 GITHUB_TOKEN 用于将版本上传到 Github Release。 + goos: ${{ matrix.goos }} # 运行程序的操作系统 + goarch: ${{ matrix.goarch }} # 运行程序的架构 + goversion: ${{ env.DEV_NEXT_GO_VERSION }} # Go 编译环境版本 + project_path: "./cmd/sing-box" # 在哪里运行 go build 命令。 + binary_name: "${{ env.BINARY }}" # 二进制名称 + build_flags: "-trimpath -tags with_quic,with_grpc,with_dhcp,with_wireguard,with_ech,with_utls,with_reality_server,with_acme,with_clash_api,with_gvisor" # 传递给 go build 命令的其他参数 + ldflags: -s -w -X github.com/sagernet/sing-box/constant.Version=${{ env.VERSION }} # ldflags参数 + md5sum: "false" + release_tag: "${{ env.VERSION }}" # 上传的标签 + release_name: "${{ env.VERSION }}" # 上传的名字 + compress_assets: "auto" # 自定义打包 + overwrite: true # 覆盖 + asset_name: "${{ env.BINARY }}-${{ matrix.goos }}-${{ matrix.goarch }}" # 自定义名称 + # ============================================================================================================================================================================================= + Mark-image-version: + needs: + - Check-version + - Create-release + - Build-main-next-image + - Build-main-next-releases + - Build-dev-next-image + - Build-dev-next-releases + runs-on: ubuntu-latest + name: "Mark the image version" + env: + MAIN_NEXT_NOW: ${{ needs.Check-version.outputs.main_next_now }} + MAIN_NEXT_NEW: ${{ needs.Check-version.outputs.main_next_new }} + MAIN_NEXT_RENEW: ${{ needs.Check-version.outputs.main_next_renew }} + DEV_NEXT_NOW: ${{ needs.Check-version.outputs.dev_next_now }} + DEV_NEXT_NEW: ${{ needs.Check-version.outputs.dev_next_new }} + DEV_NEXT_RENEW: ${{ needs.Check-version.outputs.dev_next_renew }} + COMMIT: ${{ needs.Check-version.outputs.commit }} + + steps: + - name: checkout + if: ${{ env.COMMIT != '' }} + uses: actions/checkout@v4.1.1 + with: + fetch-depth: 0 + + - name: Check and Mark + if: ${{ env.COMMIT != '' }} + run: | + if [ "${{ env.MAIN_NEXT_RENEW }}" == '1' ]; then + sed -i "s/\(latest.*:\).*/\1 ${{ env.MAIN_NEXT_NEW }}/" README.md + fi + + if [ "${{ env.DEV_NEXT_RENEW }}" == '1' ]; then + sed -i "s/\(dev.*:\).*/\1 ${{ env.DEV_NEXT_NEW }}/" README.md + fi + + git checkout --orphan tmp_work + git branch -d main + + - name: Upload to repository + if: ${{ env.COMMIT != '' }} + uses: stefanzweifel/git-auto-commit-action@v4.16.0 + with: + commit_message: ${{ env.COMMIT }} + create_branch: true + branch: main + push_options: --force + + - name: Delete old workflow runs + uses: Mattraks/delete-workflow-runs@v2 + with: + token: ${{ github.token }} + repository: ${{ github.repository }} + retain_days: 0 + keep_minimum_runs: 0 + delete_run_by_conclusion_pattern: "cancelled, skipped, success" diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..5e6135e --- /dev/null +++ b/Dockerfile @@ -0,0 +1,26 @@ +FROM --platform=$BUILDPLATFORM golang:1.22-alpine AS builder +WORKDIR /go/src/github.com/sagernet/sing-box +ARG TARGETOS TARGETARCH BRANCH +ENV CGO_ENABLED=0 +ENV GOOS=$TARGETOS +ENV GOARCH=$TARGETARCH + +RUN set -ex \ + && apk add git build-base \ + && git clone -b $BRANCH --single-branch https://github.com/sagernet/sing-box /go/src/github.com/sagernet/sing-box \ + && export COMMIT=$(git rev-parse --short HEAD) \ + && export VERSION=$(go run ./cmd/internal/read_tag) \ + && go build -v -trimpath -tags \ + "with_quic,with_grpc,with_dhcp,with_wireguard,with_ech,with_utls,with_reality_server,with_acme,with_clash_api,with_v2ray_api,with_gvisor" \ + -o /go/bin/sing-box \ + -ldflags "-X \"github.com/sagernet/sing-box/constant.Version=$VERSION\" -s -w -buildid=" \ + ./cmd/sing-box + +FROM --platform=$TARGETPLATFORM alpine AS dist +RUN set -ex \ + && apk upgrade \ + && apk add bash tzdata ca-certificates \ + && rm -rf /var/cache/apk/* + +COPY --from=builder /go/bin/sing-box /usr/local/bin/sing-box +ENTRYPOINT [ "sing-box" ] diff --git a/README.md b/README.md new file mode 100644 index 0000000..e4c7496 --- /dev/null +++ b/README.md @@ -0,0 +1,38 @@ +# Sing-Box 镜像 与 Releases 构建 +- **使用GitHub Actions进行自动构建** +- 本教程教您如何使用GitHub Actions 全自动构建Sing-Box Docker镜像 与 仓库Releases发布 + +## 目前Docker镜像 / Releases版本: + - ### **latest**: v1.10.3 + - ### **dev-next**: v1.11.0-beta.4 + +# 教程 +## 1. fork仓库 + +## 2. 查看具体权限 + - 进入fork的仓库后,点击上方 **Settings** ,在左侧菜单中找到**Actions** → **General**,从中找到 **Workflow permissions** 字段,查看是否已经选中 **Read and write permissions** 字段,如未选中,请选中后Save保存,如已选中,则进入下一步 + +## 3.保存必要字段 + - 点击上方 **Settings** ,在左侧菜单中找到**Secrets and variables** → **Actions**,在**Repository secrets**字段中,点击**New repository secret**,添加如下字段 + - 第一份 + Name: `DOCKER_REPO` + Secret: `Docker Hub的仓库名字,只能用小写字母,尽量避免符号` + + - 第二份 + Name: `DOCKER_USERNAME` + Secret: `Docker Hub的账号` + + - 第三份 + Name: `DOCKER_PASSWORD` + Secret: `Docker Hub的密码` + +## 4. 开启编译 + - 点击上方Actions,在左侧找到 **Build main-next and dev-next images & releases**,进入后,在右侧找到**Run workflow**执行 + +### 5.关于编译版本号问题 + - 脚本自动检测最新版本号,并将其写入本文档中,用于后续对比与最新版本号是否有差异,无差异则掠过,有差异则执行编译 + +## 至此,全部流程已经已经跑完,稍作等待,当所有流程的前侧变成绿色的对勾后,代表镜像已推送至指定仓库 + +# 特别感谢 + - [fscarmen2](https://github.com/fscarmen2)