/ .github / workflows / publish-server.yml
publish-server.yml
  1  name: Publish Server
  2  
  3  on:
  4    push:
  5      tags:
  6        - 'server/v*'
  7  
  8  permissions:
  9    contents: read
 10  
 11  jobs:
 12    publish-pypi:
 13      if: startsWith(github.ref, 'refs/tags/server/v')
 14      runs-on: ubuntu-latest
 15      steps:
 16        - name: Checkout code
 17          uses: actions/checkout@v6
 18  
 19        - name: Set up Python
 20          uses: actions/setup-python@v6
 21          with:
 22            python-version: '3.10'
 23  
 24        - name: Install uv
 25          uses: astral-sh/setup-uv@v7
 26          with:
 27            version: "latest"
 28  
 29        - name: Build package
 30          working-directory: server
 31          run: |
 32            uv build
 33  
 34        - name: Publish to PyPI
 35          working-directory: server
 36          env:
 37            UV_PUBLISH_TOKEN: ${{ secrets.PYPI_API_TOKEN }}
 38          run: |
 39            uv publish
 40  
 41    publish-image:
 42      if: startsWith(github.ref, 'refs/tags/server/v')
 43      runs-on: ubuntu-latest
 44      steps:
 45        - name: Checkout code
 46          uses: actions/checkout@v6
 47        - name: Set up QEMU
 48          uses: docker/setup-qemu-action@v3
 49  
 50        - name: Set up Docker Buildx
 51          uses: docker/setup-buildx-action@v3
 52  
 53        - name: Login to DockerHub
 54          uses: docker/login-action@v3
 55          with:
 56            username: ${{ secrets.DOCKERHUB_USERNAME }}
 57            password: ${{ secrets.DOCKERHUB_PASSWORD }}
 58  
 59        - name: Login to ACR
 60          uses: docker/login-action@v3
 61          with:
 62            registry: sandbox-registry.cn-zhangjiakou.cr.aliyuncs.com
 63            username: ${{ secrets.ACR_USERNAME }}
 64            password: ${{ secrets.ACR_PASSWORD }}
 65  
 66        - name: Parse tag and set variables
 67          id: parse_tag
 68          run: |
 69            if [[ "${{ github.ref }}" == refs/tags/server/* ]]; then
 70              TAG_PATH="${{ github.ref }}"
 71              TAG_PATH="${TAG_PATH#refs/tags/}"
 72  
 73              IMAGE_TAG="${TAG_PATH#server/}"
 74  
 75              if [ -z "$IMAGE_TAG" ]; then
 76                echo "failed to parse image tag from $TAG_PATH" >&2
 77                exit 1
 78              fi
 79  
 80              echo "image_tag=$IMAGE_TAG" >> $GITHUB_OUTPUT
 81            else
 82              echo "cannot parse tag"
 83              exit 1
 84            fi
 85  
 86        - name: Build and push to registries
 87          working-directory: server
 88          env:
 89            TAG: ${{ steps.parse_tag.outputs.image_tag }}
 90          run: |
 91            chmod +x build.sh
 92            ./build.sh
 93  
 94    bump-server-chart:
 95      if: startsWith(github.ref, 'refs/tags/server/v')
 96      needs: publish-image
 97      runs-on: ubuntu-latest
 98      permissions:
 99        contents: write
100        pull-requests: write
101      steps:
102        - name: Checkout code
103          uses: actions/checkout@v6
104  
105        - name: Parse tag and set variables
106          id: parse_tag
107          run: |
108            TAG_PATH="${{ github.ref }}"
109            TAG_PATH="${TAG_PATH#refs/tags/}"
110            IMAGE_TAG="${TAG_PATH#server/}"
111            if [ -z "$IMAGE_TAG" ]; then
112              echo "failed to parse image tag from $TAG_PATH" >&2
113              exit 1
114            fi
115            echo "image_tag=$IMAGE_TAG" >> $GITHUB_OUTPUT
116  
117        - name: Bump server chart image tag
118          env:
119            GH_TOKEN: ${{ github.token }}
120          run: |
121            IMAGE_TAG="${{ steps.parse_tag.outputs.image_tag }}"
122            if [[ "$IMAGE_TAG" =~ ^v ]]; then
123              VERSION="$IMAGE_TAG"
124            else
125              VERSION="v${IMAGE_TAG}"
126            fi
127  
128            chmod +x scripts/bump-server-chart.sh
129            ./scripts/bump-server-chart.sh "$VERSION"
130  
131            BRANCH="bump/server-chart-${VERSION}"
132            git config user.name "github-actions[bot]"
133            git config user.email "github-actions[bot]@users.noreply.github.com"
134            git checkout -b "$BRANCH"
135            git add kubernetes/charts/opensandbox-server/values.yaml
136            git diff --staged --quiet && echo "No changes to commit" && exit 0
137            git commit -m "chore(chart): bump opensandbox-server image to ${VERSION}"
138            git push origin "$BRANCH"
139  
140            gh pr create \
141              --title "chore(chart): bump opensandbox-server image to ${VERSION}" \
142              --body "Auto-generated after publishing server image \`${VERSION}\` (tag \`${{ github.ref_name }}\`)." \
143              --base "$(gh api repos/${{ github.repository }} --jq .default_branch)"