From 3aae31d100f7ae5c2e700371a8ec83087c458f48 Mon Sep 17 00:00:00 2001 From: AirryCo Date: Tue, 26 Nov 2024 19:41:30 +0800 Subject: [PATCH] docker: Add Dockerfile for multi-platforms. --- .github/workflows/build-nightly-cron.yaml | 109 ++++++++++++++++++++-- .github/workflows/build-release-cron.yaml | 104 ++++++++++++++++++++- docker/Dockerfile | 18 ++++ docker/Dockerfile-fika | 26 ++++++ docker/entrypoint.sh | 37 ++++++++ 5 files changed, 280 insertions(+), 14 deletions(-) create mode 100644 docker/Dockerfile create mode 100644 docker/Dockerfile-fika create mode 100644 docker/entrypoint.sh diff --git a/.github/workflows/build-nightly-cron.yaml b/.github/workflows/build-nightly-cron.yaml index 6fce0d8..87ea6b6 100644 --- a/.github/workflows/build-nightly-cron.yaml +++ b/.github/workflows/build-nightly-cron.yaml @@ -292,6 +292,7 @@ jobs: outputs: SPT_SERVER_COMMIT_TIME: ${{ steps.merge.outputs.SPT_SERVER_COMMIT_TIME }} LINUX_COMMIT_ID: ${{ steps.debug-info.outputs.COMMIT_ID }} + LINUX_COMMIT_ID_WHOLE: ${{ steps.debug-info.outputs.COMMIT_ID_WHOLE }} LINUX_ARTIFACT: ${{ steps.filename.outputs.LINUX_ARTIFACT }} LINUX_RELEASE_FILE: ${{ steps.filename.outputs.LINUX_RELEASE }} LINUX_RELEASE_FIKA_FILE: ${{ steps.filename.outputs.LINUX_RELEASE_FIKA }} @@ -409,8 +410,9 @@ jobs: - name: Compress fika-server to docker dir run: | cd fika - tar -czf ${{ env.SOURCECODE_DIR }}/docker/fika-server.tgz . - ls -l ${{ env.SOURCECODE_DIR }}/docker + tar -czf ${{ env.SOURCECODE_DIR }}/docker/FikaBuiltIn/fika-server.tgz . + cp ${{ env.SOURCECODE_DIR }}/docker/spt-server.tgz ${{ env.SOURCECODE_DIR }}/docker/FikaBuiltIn + ls -l ${{ env.SOURCECODE_DIR }}/docker/FikaBuiltIn shell: bash - name: Extract metadata for Docker @@ -472,21 +474,20 @@ jobs: org.opencontainers.image.source=https://github.com/AirryCo/spt-server org.opencontainers.image.url=https://github.com/AirryCo/spt-server org.opencontainers.image.revision=${{ steps.debug-info.outputs.COMMIT_ID_WHOLE }} - platforms: linux/amd64,linux/arm64 + platforms: linux/amd64 - name: Build and Push with Fika Server id: build-and-push-with-fika uses: docker/build-push-action@v6 with: - context: ${{ env.SOURCECODE_DIR }}/docker - file: ${{ env.SOURCECODE_DIR }}/docker/Dockerfile-fika + context: ${{ env.SOURCECODE_DIR }}/docker/FikaBuiltIn push: true tags: ${{ steps.meta-fika.outputs.tags }} labels: | - ${{ steps.meta.outputs.labels }} + ${{ steps.meta-fika.outputs.labels }} org.opencontainers.image.source=https://github.com/AirryCo/spt-server org.opencontainers.image.url=https://github.com/AirryCo/spt-server org.opencontainers.image.revision=${{ steps.debug-info.outputs.COMMIT_ID_WHOLE }} - platforms: linux/amd64,linux/arm64 + platforms: linux/amd64 - name: Push Linux Server Code run: | @@ -497,8 +498,98 @@ jobs: git push shell: bash + build-docker-image-multi-arch: + needs: [prepare, build-server-linux] + if: needs.prepare.outputs.PROCEED == 'true' || needs.prepare.outputs.PROCEED_FIKA == 'true' + runs-on: ubuntu-latest + env: + DOCKERHUB_USER: stblog + DOCKERHUB_REPOSITORY: spt-server + GHCR_USER: airryco + GHCR_REPOSITORY: spt-server + + steps: + - name: Checkout + uses: actions/checkout@v4 + + # docker + - name: Extract metadata for Docker + id: meta + uses: docker/metadata-action@v5 + with: + images: | + ${{ env.DOCKERHUB_USER }}/${{ env.DOCKERHUB_REPOSITORY }} + ghcr.io/${{ env.GHCR_USER }}/${{ env.GHCR_REPOSITORY }} + registry.cn-shenzhen.aliyuncs.com/spt-server/spt-server + tags: | + type=raw,value=nightly + - name: Extract metadata for Docker with Fika-Server + id: meta-fika + uses: docker/metadata-action@v5 + with: + images: | + ${{ env.DOCKERHUB_USER }}/${{ env.DOCKERHUB_REPOSITORY }} + ghcr.io/${{ env.GHCR_USER }}/${{ env.GHCR_REPOSITORY }} + registry.cn-shenzhen.aliyuncs.com/spt-server/spt-server + tags: | + type=raw,value=nightly-fika + + - name: Set up QEMU + uses: docker/setup-qemu-action@v3 + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + # DockerHub + - name: Login to Dockerhub + uses: docker/login-action@v3 + with: + username: ${{ env.DOCKERHUB_USER }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + # GitHub Container Registry + - name: Login to Github Container Registry + uses: docker/login-action@v3 + with: + registry: ghcr.io + username: ${{ env.GHCR_USER }} + password: ${{ secrets.GHCR_TOKEN }} + # Aliyun Registry + - name: Login to Aliyun Registry + uses: docker/login-action@v3 + with: + registry: registry.cn-shenzhen.aliyuncs.com + username: ${{ secrets.ALIYUN_REGISTRY_USER }} + password: ${{ secrets.ALIYUN_REGISTRY_TOKEN }} + + - name: Build and Push + id: build-and-push + uses: docker/build-push-action@v6 + with: + context: ./docker + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: | + ${{ steps.meta.outputs.labels }} + org.opencontainers.image.source=https://github.com/AirryCo/spt-server + org.opencontainers.image.url=https://github.com/AirryCo/spt-server + org.opencontainers.image.revision=${{ needs.build-server-linux.outputs.LINUX_COMMIT_ID_WHOLE }} + platforms: linux/arm64 + - name: Build and Push with Fika Server + id: build-and-push-with-fika + uses: docker/build-push-action@v6 + with: + context: ./docker + file: ./docker/Dockerfile-fika + push: true + tags: ${{ steps.meta-fika.outputs.tags }} + labels: | + ${{ steps.meta-fika.outputs.labels }} + org.opencontainers.image.source=https://github.com/AirryCo/spt-server + org.opencontainers.image.url=https://github.com/AirryCo/spt-server + org.opencontainers.image.revision=${{ needs.build-server-linux.outputs.LINUX_COMMIT_ID_WHOLE }} + platforms: linux/arm64 + update-trigger-and-push: - needs: [prepare, build-server-windows, build-server-linux] + needs: [prepare, build-server-windows, build-docker-image-multi-arch] runs-on: ubuntu-latest steps: - name: Checkout @@ -528,8 +619,6 @@ jobs: steps: - name: Checkout uses: actions/checkout@v4 - with: - fetch-depth: 0 - name: Download Windows Artifact uses: actions/download-artifact@v4 diff --git a/.github/workflows/build-release-cron.yaml b/.github/workflows/build-release-cron.yaml index 24ed63d..3e63848 100644 --- a/.github/workflows/build-release-cron.yaml +++ b/.github/workflows/build-release-cron.yaml @@ -208,6 +208,7 @@ jobs: GHCR_REPOSITORY: spt-server outputs: LINUX_COMMIT_ID: ${{ steps.debug-info.outputs.COMMIT_ID }} + LINUX_COMMIT_ID_WHOLE: ${{ steps.debug-info.outputs.COMMIT_ID_WHOLE }} LINUX_ARTIFACT: ${{ steps.filename.outputs.LINUX_ARTIFACT }} LINUX_RELEASE_FILE: ${{ steps.filename.outputs.LINUX_RELEASE }} @@ -370,8 +371,7 @@ jobs: org.opencontainers.image.source=https://github.com/AirryCo/spt-server org.opencontainers.image.url=https://github.com/AirryCo/spt-server org.opencontainers.image.revision=${{ steps.debug-info.outputs.COMMIT_ID_WHOLE }} - platforms: linux/amd64,linux/arm64 - + platforms: linux/amd64 - name: Build and Push For Latest if: ${{ !contains(needs.prepare.outputs.TARGET_TAG, 'BEM') }} uses: docker/build-push-action@v6 @@ -384,7 +384,7 @@ jobs: org.opencontainers.image.source=https://github.com/AirryCo/spt-server org.opencontainers.image.url=https://github.com/AirryCo/spt-server org.opencontainers.image.revision=${{ steps.debug-info.outputs.COMMIT_ID_WHOLE }} - platforms: linux/amd64,linux/arm64 + platforms: linux/amd64 - name: Push Linux Server Code And Tag run: | @@ -399,8 +399,104 @@ jobs: git push -u origin ${{ needs.prepare.outputs.TARGET_TAG }} shell: bash + build-docker-image-multi-arch: + needs: [prepare, build-server-linux] + if: needs.prepare.outputs.PROCEED == 'true' || needs.prepare.outputs.PROCEED_FIKA == 'true' + runs-on: ubuntu-latest + env: + DOCKERHUB_USER: stblog + DOCKERHUB_REPOSITORY: spt-server + GHCR_USER: airryco + GHCR_REPOSITORY: spt-server + + steps: + - name: Checkout + uses: actions/checkout@v4 + + # docker + - name: Extract metadata for Docker with bem version + if: ${{ contains(needs.prepare.outputs.TARGET_TAG, 'BEM') }} + id: meta + uses: docker/metadata-action@v5 + with: + images: | + ${{ env.DOCKERHUB_USER }}/${{ env.DOCKERHUB_REPOSITORY }} + ghcr.io/${{ env.GHCR_USER }}/${{ env.GHCR_REPOSITORY }} + registry.cn-shenzhen.aliyuncs.com/spt-server/spt-server + tags: | + type=semver,pattern={{version}},value=${{ needs.prepare.outputs.TARGET_TAG }} + type=semver,pattern={{major}}.{{minor}},value=${{ needs.prepare.outputs.TARGET_TAG }} + - name: Extract metadata for Docker with latest version + if: ${{ !contains(needs.prepare.outputs.TARGET_TAG, 'BEM') }} + id: meta-latest + uses: docker/metadata-action@v5 + with: + images: | + ${{ env.DOCKERHUB_USER }}/${{ env.DOCKERHUB_REPOSITORY }} + ghcr.io/${{ env.GHCR_USER }}/${{ env.GHCR_REPOSITORY }} + registry.cn-shenzhen.aliyuncs.com/spt-server/spt-server + tags: | + type=semver,pattern={{version}},value=${{ needs.prepare.outputs.TARGET_TAG }} + type=semver,pattern={{major}}.{{minor}},value=${{ needs.prepare.outputs.TARGET_TAG }} + type=raw,value=latest + + - name: Set up QEMU + uses: docker/setup-qemu-action@v3 + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + # DockerHub + - name: Login to Dockerhub + uses: docker/login-action@v3 + with: + username: ${{ env.DOCKERHUB_USER }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + # GitHub Container Registry + - name: Login to Github Container Registry + uses: docker/login-action@v3 + with: + registry: ghcr.io + username: ${{ env.GHCR_USER }} + password: ${{ secrets.GHCR_TOKEN }} + # Aliyun Registry + - name: Login to Aliyun Registry + uses: docker/login-action@v3 + with: + registry: registry.cn-shenzhen.aliyuncs.com + username: ${{ secrets.ALIYUN_REGISTRY_USER }} + password: ${{ secrets.ALIYUN_REGISTRY_TOKEN }} + + - name: Build and Push For BEM + if: ${{ contains(needs.prepare.outputs.TARGET_TAG, 'BEM') }} + id: build-and-push + uses: docker/build-push-action@v6 + with: + context: ./docker + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: | + ${{ steps.meta.outputs.labels }} + org.opencontainers.image.source=https://github.com/AirryCo/spt-server + org.opencontainers.image.url=https://github.com/AirryCo/spt-server + org.opencontainers.image.revision=${{ needs.build-server-linux.outputs.LINUX_COMMIT_ID_WHOLE }} + platforms: linux/arm64 + - name: Build and Push For Latest + if: ${{ !contains(needs.prepare.outputs.TARGET_TAG, 'BEM') }} + id: build-and-push + uses: docker/build-push-action@v6 + with: + context: ./docker + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: | + ${{ steps.meta.outputs.labels }} + org.opencontainers.image.source=https://github.com/AirryCo/spt-server + org.opencontainers.image.url=https://github.com/AirryCo/spt-server + org.opencontainers.image.revision=${{ needs.build-server-linux.outputs.LINUX_COMMIT_ID_WHOLE }} + platforms: linux/arm64 + assemble-and-publish: - needs: [prepare, build-server-windows, build-server-linux] + needs: [prepare, build-server-windows, build-server-linux, build-docker-image-multi-arch] runs-on: ubuntu-latest steps: diff --git a/docker/Dockerfile b/docker/Dockerfile new file mode 100644 index 0000000..e717afd --- /dev/null +++ b/docker/Dockerfile @@ -0,0 +1,18 @@ +FROM node:20.11.1-alpine AS server-builder +RUN apk add --no-cache git git-lfs && \ + git clone -b 3.10.0-DEV https://github.com/AirryCo/spt-server.git /spt-server && \ + cd /spt-server && git lfs pull && \ + cd project && \ + npm install && npm run build:release + +FROM debian:bookworm-slim +LABEL author="AirryCo " +ENV TZ=Asia/Shanghai +COPY --from=server-builder /spt-server/project/build/ /app/spt-server/ +VOLUME /opt/spt-server +WORKDIR /opt/spt-server +EXPOSE 6969 +COPY entrypoint.sh /entrypoint.sh +RUN chmod +x /entrypoint.sh +ENTRYPOINT ["/entrypoint.sh"] + diff --git a/docker/Dockerfile-fika b/docker/Dockerfile-fika new file mode 100644 index 0000000..2296c5c --- /dev/null +++ b/docker/Dockerfile-fika @@ -0,0 +1,26 @@ +FROM node:20.11.1-alpine AS fika-builder +RUN apk add --no-cache git git-lfs unzip && \ + git clone -b spt-3.10 https://github.com/project-fika/Fika-Server.git /fika-server && \ + cd /fika-server && \ + npm install && npm run build && \ + mkdir output && cd output && unzip /fika-server/dist/fika-server.zip + +FROM node:20.11.1-alpine AS server-builder +RUN apk add --no-cache git git-lfs && \ + git clone -b 3.10.0-DEV https://github.com/AirryCo/spt-server.git /spt-server && \ + cd /spt-server && git lfs pull && \ + cd project && \ + npm install && npm run build:release + +FROM debian:bookworm-slim +LABEL author="AirryCo " +ENV TZ=Asia/Shanghai +COPY --from=server-builder /spt-server/project/build/ /app/spt-server/ +COPY --from=fika-builder /fika-server/output/ /app/spt-server/ +VOLUME /opt/spt-server +WORKDIR /opt/spt-server +EXPOSE 6969 +COPY entrypoint.sh /entrypoint.sh +RUN chmod +x /entrypoint.sh +ENTRYPOINT ["/entrypoint.sh"] + diff --git a/docker/entrypoint.sh b/docker/entrypoint.sh new file mode 100644 index 0000000..41e9651 --- /dev/null +++ b/docker/entrypoint.sh @@ -0,0 +1,37 @@ +#!/bin/bash + +if [ -f /opt/spt-server/SPT.Server ]; then + appHash=$(md5sum /app/spt-server/SPT.Server | awk '{ print $1 }') + optHash=$(md5sum /opt/spt-server/SPT.Server | awk '{ print $1 }') + if [ "$appHash" = "$optHash" ]; then + echo "MD5 verification successful!" + else + echo "MD5 mismatch, copy files to /opt/spt-server." + cp -r /app/spt-server /opt + echo "Finished!" + fi +else + echo "Program is not found, copy files to /opt/spt-server." + cp -r /app/spt-server /opt + echo "Finished!" +fi + +cd /opt/spt-server + +if [ -z "$backendIp" ]; then + IP=$(hostname -I | awk '{print $1}') +else + IP=$backendIp +fi + +if [ -z "$backendPort" ]; then + PORT=6969 +else + PORT=$backendPort +fi + +sed -i "0,/127.0.0.1/s/127.0.0.1/${IP}/" SPT_Data/Server/configs/http.json +sed -i "s/6969/${PORT}/g" SPT_Data/Server/configs/http.json + +chmod +x SPT.Server && ./SPT.Server +