From d18d8c0e3f10a969620954d3e12b82d86f013733 Mon Sep 17 00:00:00 2001 From: ljw <84855512@qq.com> Date: Fri, 27 Sep 2024 14:10:50 +0800 Subject: [PATCH] up build docker --- .github/workflows/build.yml | 166 ++++++++++++++++++++++++++++ .github/workflows/release_arm64.yml | 83 -------------- Dockerfile | 28 +---- 3 files changed, 167 insertions(+), 110 deletions(-) create mode 100644 .github/workflows/build.yml delete mode 100644 .github/workflows/release_arm64.yml diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..5cb6793 --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,166 @@ +name: Build + +on: + workflow_dispatch: + push: + tags: + - 'v*.*.*' # 当推送带有版本号的 tag(例如 v1.0.0)时触发工作流 + +jobs: + build: + runs-on: ubuntu-latest + + strategy: + fail-fast: false + matrix: + job: + - { platform: "amd64", goos: "linux" } + - { platform: "arm64v8", goos: "linux" } + - { platform: "amd64", goos: "windows" } + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Set up Go environment + uses: actions/setup-go@v4 + with: + go-version: '1.22' # 选择 Go 版本 + + - name: Set up npm + uses: actions/setup-node@v2 + with: + node-version: '20' + + + - name: build rustdesk-api-web + run: | + git clone https://github.com/lejianwen/rustdesk-api-web + cd rustdesk-api-web + npm install + npm run build + mkdir ../resources/admin/ -p + cp -ar dist/* ../resources/admin/ + + - name: tidy + run: go mod tidy + + - name: swag + run: | + go install github.com/swaggo/swag/cmd/swag@latest + swag init -g cmd/apimain.go --output docs/api --instanceName api --exclude http/controller/admin + swag init -g cmd/apimain.go --output docs/admin --instanceName admin --exclude http/controller/api + + - name: Build for ${{ matrix.goos }}-${{ matrix.goarch }} + run: | + mkdir release -p + cp -ar resources release/ + cp -ar docs release/ + cp -ar conf release/ + mkdir -p release/data + mkdir -p release/runtime + if [ "${{ matrix.goos }}" = "windows" ]; then + sudo apt-get install gcc-mingw-w64-x86-64 zip -y + GOOS=${{ matrix.goos }} GOARCH=${{ matrix.goarch }} CC=x86_64-w64-mingw32-gcc CGO_LDFLAGS="-static" CGO_ENABLED=1 go build -ldflags "-s -w" -o ./release/apimain.exe ./cmd/apimain.go + zip -r ${{ matrix.goos}}-${{ matrix.goarch }}.zip ./release + else + if [ "${{ matrix.platform }}" = "arm64v8" ]; then + wget https://musl.cc/aarch64-linux-musl-cross.tgz + tar -xvzf aarch64-linux-musl-cross.tgz + export PATH=$PATH:$PWD/aarch64-linux-musl-cross/bin + GOOS=${{ matrix.goos }} GOARCH=${{ matrix.goarch }} CC=aarch64-linux-musl-gcc CGO_LDFLAGS="-static" CGO_ENABLED=1 go build -ldflags "-s -w" -o ./release/apimain ./cmd/apimain.go + else + sudo apt-get install musl musl-dev musl-tools -y + GOOS=${{ matrix.goos }} GOARCH=${{ matrix.goarch }} CC=musl-gcc CGO_LDFLAGS="-static" CGO_ENABLED=1 go build -ldflags "-s -w" -o ./release/apimain ./cmd/apimain.go + fi + tar -czf ${{ matrix.goos}}-${{ matrix.goarch }}.tar.gz ./release + fi + + - name: Upload artifact + uses: actions/upload-artifact@v4 + with: + name: rustdesk-api-${{ matrix.goos }}-${{ matrix.goarch }} + path: | + ${{ matrix.goos}}-${{ matrix.goarch }}.tar.gz + ${{ matrix.goos}}-${{ matrix.goarch }}.zip + + - name: Upload to GitHub Release + uses: softprops/action-gh-release@v2 + with: + files: | + ${{ matrix.goos}}-${{ matrix.goarch }}.tar.gz + ${{ matrix.goos}}-${{ matrix.goarch }}.zip + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + docker: + name: Push Docker Image ${{ matrix.platform }} + needs: build + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + job: + - { platform: "amd64", goos: "linux", docker_platform: "linux/amd64" } + - { platform: "arm64v8", goos: "linux", docker_platform: "linux/arm64" } + steps: +# - name: Checkout code +# uses: actions/checkout@v4 + + - name: Log in to Docker Hub + uses: docker/login-action@v2 + with: + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_ACCESS_TOKEN }} + + - name: Extract version from tag + id: vars + run: echo "TAG=${GITHUB_REF#refs/tags/}" >> $GITHUB_ENV + + - name: Download binaries + uses: actions/download-artifact@v3 + with: + name: rustdesk-api-${{ matrix.goos }}-${{ matrix.goarch }} + path: rustdesk-api/ + + - name: Unzip binaries + run: | + tar -xzf rustdesk-api/rustdesk-api-${{ matrix.goos }}-${{ matrix.goarch }}.tar.gz -C rustdesk-api/ + + - name: Build and push Docker image + uses: docker/build-push-action@v5 + with: + context: "." + file: ./Dockerfile + platforms: ${{ matrix.job.docker_platform }} + push: true + tags: | + lejianwen/rustdesk-api:latest-${{ matrix.job.platform }} + lejianwen/rustdesk-api:${{ env.TAG }}-${{ matrix.job.platform }} + docker-manifest: + name: Push Docker Manifest + needs: docker + runs-on: ubuntu-latest + steps: + - name: Extract version from tag + id: vars + run: echo "TAG=${GITHUB_REF#refs/tags/}" >> $GITHUB_ENV + + - name: Log in to Docker Hub + uses: docker/login-action@v2 + with: + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_ACCESS_TOKEN }} + + - name: Create and push manifest (:version) + uses: Noelware/docker-manifest-action@master + with: + base-image: lejianwen/rustdesk-api:${{ env.TAG }} + extra-images: lejianwen/rustdesk-api:${{ env.TAG }}-amd64,lejianwen/rustdesk-api:${{ env.TAG }}-arm64v8 + push: true + +# - name: Create and push manifest (:latest) +# uses: Noelware/docker-manifest-action@master +# with: +# base-image: lejianwen/rustdesk-api:latest +# extra-images: lejianwen/rustdesk-api:latest-amd64,lejianwen/rustdesk-api:latest-arm64v8 +# push: true diff --git a/.github/workflows/release_arm64.yml b/.github/workflows/release_arm64.yml deleted file mode 100644 index 84e75ed..0000000 --- a/.github/workflows/release_arm64.yml +++ /dev/null @@ -1,83 +0,0 @@ -name: Build and Release Arm64 - -#on: -# push: -# tags: -# - 'v*.*.*' # 当推送带有版本号的 tag(例如 v1.0.0)时触发工作流 -on: - push: - branches: [ "master" ] -# pull_request: -# branches: [ "master" ] - -jobs: - build: - runs-on: ubuntu-latest - - strategy: - matrix: - goos: [ linux ] # 指定要构建的操作系统 - goarch: [ arm64 ] # 指定架构 - - steps: - - name: Checkout code - uses: actions/checkout@v4 - - - name: Set up Go environment - uses: actions/setup-go@v4 - with: - go-version: '1.22' # 选择 Go 版本 - - - name: Set up npm - uses: actions/setup-node@v2 - with: - node-version: '20' - - - - name: build rustdesk-api-web - run: | - git clone https://github.com/lejianwen/rustdesk-api-web - cd rustdesk-api-web - npm install - npm run build - mkdir ../resources/admin/ -p - cp -ar dist/* ../resources/admin/ - - - name: tidy - run: go mod tidy - - - - name: swag - run: | - go install github.com/swaggo/swag/cmd/swag@latest - swag init -g cmd/apimain.go --output docs/api --instanceName api --exclude http/controller/admin - swag init -g cmd/apimain.go --output docs/admin --instanceName admin --exclude http/controller/api - - - name: Build for ${{ matrix.goos }}-${{ matrix.goarch }} - run: | - wget https://musl.cc/aarch64-linux-musl-cross.tgz - tar -xvzf aarch64-linux-musl-cross.tgz - export PATH=$PATH:$PWD/aarch64-linux-musl-cross/bin - mkdir release -p - cp -ar resources release/ - cp -ar docs release/ - cp -ar conf release/ - mkdir -p release/data - mkdir -p release/runtime - GOOS=${{ matrix.goos }} GOARCH=${{ matrix.goarch }} CC=aarch64-linux-musl-gcc CGO_LDFLAGS="-static" CGO_ENABLED=1 go build -ldflags "-s -w" -o ./release/apimain ./cmd/apimain.go - tar -czf ${{ matrix.goos}}-${{ matrix.goarch }}.tar.gz ./release - - - name: Upload artifact - uses: actions/upload-artifact@v4 - with: - name: myapp-${{ matrix.goos }}-${{ matrix.goarch }} - path: | - ${{ matrix.goos}}-${{ matrix.goarch }}.tar.gz - -# - name: Upload to GitHub Release -# uses: softprops/action-gh-release@v2 -# with: -# files: | -# ${{ matrix.goos}}-${{ matrix.goarch }}.tar.gz -# env: -# GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/Dockerfile b/Dockerfile index 0bf9e76..835b5cc 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,35 +1,9 @@ FROM golang:1.22-alpine as builder -RUN set -eux; \ - apk add --no-cache git gcc build-base sqlite-dev npm nodejs; \ - git clone https://github.com/lejianwen/rustdesk-api-web; \ - git clone https://github.com/lejianwen/rustdesk-api; \ - #先编译后台 - cd rustdesk-api-web; \ - npm install; \ - npm run build; \ - cd ..; \ - mkdir -p rustdesk-api/resources/admin; \ - cp -ar rustdesk-api-web/dist/* rustdesk-api/resources/admin; \ - cd rustdesk-api; \ - go mod tidy; \ - go install github.com/swaggo/swag/cmd/swag@latest; \ - swag init -g cmd/apimain.go --output docs/api --instanceName api --exclude http/controller/admin; \ - swag init -g cmd/apimain.go --output docs/admin --instanceName admin --exclude http/controller/api; \ - go env -w GO111MODULE=on;\ - CGO_LDFLAGS="-static" CGO_ENABLED=1 go build -ldflags "-s -w" -o ./release/apimain ./cmd/apimain.go; \ - cp -ar resources release/; \ - mkdir -p release/resources/public; \ - cp -ar docs release/; \ - cp -ar conf release/; \ - mkdir -p release/data; \ - mkdir -p release/runtime; - - FROM alpine WORKDIR /app RUN apk add --no-cache tzdata -COPY --from=builder /go/rustdesk-api/release /app/ +COPY rustdesk-api/release /app/ VOLUME /app/data EXPOSE 21114