Deploy to Cloud Server #403
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Deploy to Cloud Server | |
| on: | |
| workflow_dispatch: # 手动触发 | |
| inputs: | |
| api: | |
| description: 'Deploy api' | |
| required: false | |
| type: boolean | |
| default: false | |
| user: | |
| description: 'Deploy user' | |
| required: false | |
| type: boolean | |
| default: false | |
| classroom: | |
| description: 'Deploy classroom' | |
| required: false | |
| type: boolean | |
| default: false | |
| course: | |
| description: 'Deploy course' | |
| required: false | |
| type: boolean | |
| default: false | |
| launch_screen: | |
| description: 'Deploy launch_screen' | |
| required: false | |
| type: boolean | |
| default: false | |
| paper: | |
| description: 'Deploy paper' | |
| required: false | |
| type: boolean | |
| default: false | |
| academic: | |
| description: 'Deploy academic' | |
| required: false | |
| type: boolean | |
| default: false | |
| version: | |
| description: 'Deploy version' | |
| required: false | |
| type: boolean | |
| default: false | |
| common: | |
| description: 'Deploy common' | |
| required: false | |
| type: boolean | |
| default: false | |
| oa: | |
| description: 'Deploy oa' | |
| required: false | |
| type: boolean | |
| default: false | |
| captcha: | |
| description: 'Deploy captcha' | |
| required: false | |
| type: boolean | |
| default: false | |
| permissions: | |
| contents: read | |
| jobs: | |
| setup: | |
| runs-on: ubuntu-latest | |
| outputs: | |
| matrix: ${{ steps.set-matrix.outputs.matrix }} | |
| steps: | |
| - id: set-matrix | |
| run: | | |
| services='[]' | |
| [ "${{ github.event.inputs.api }}" = "true" ] && services=$(echo $services | jq -c '. += ["api"]') | |
| [ "${{ github.event.inputs.user }}" = "true" ] && services=$(echo $services | jq -c '. += ["user"]') | |
| [ "${{ github.event.inputs.classroom }}" = "true" ] && services=$(echo $services | jq -c '. += ["classroom"]') | |
| [ "${{ github.event.inputs.course }}" = "true" ] && services=$(echo $services | jq -c '. += ["course"]') | |
| [ "${{ github.event.inputs.launch_screen }}" = "true" ] && services=$(echo $services | jq -c '. += ["launch_screen"]') | |
| [ "${{ github.event.inputs.paper }}" = "true" ] && services=$(echo $services | jq -c '. += ["paper"]') | |
| [ "${{ github.event.inputs.academic }}" = "true" ] && services=$(echo $services | jq -c '. += ["academic"]') | |
| [ "${{ github.event.inputs.version }}" = "true" ] && services=$(echo $services | jq -c '. += ["version"]') | |
| [ "${{ github.event.inputs.common }}" = "true" ] && services=$(echo $services | jq -c '. += ["common"]') | |
| [ "${{ github.event.inputs.oa }}" = "true" ] && services=$(echo $services | jq -c '. += ["oa"]') | |
| [ "${{ github.event.inputs.captcha }}" = "true" ] && services=$(echo $services | jq -c '. += ["captcha"]') | |
| echo "matrix={\"service\":$(echo $services | jq -c .)}" >> $GITHUB_OUTPUT | |
| build: | |
| runs-on: ubuntu-latest | |
| needs: setup | |
| if: ${{ needs.setup.outputs.matrix != '{"service":[]}' }} | |
| strategy: | |
| matrix: | |
| service: ${{ fromJson(needs.setup.outputs.matrix).service }} | |
| max-parallel: 1 # 串行执行,见Makefile:149 | |
| steps: | |
| - name: Check out the code | |
| uses: actions/checkout@v6 | |
| - name: Log in to Alibaba Cloud Docker Registry | |
| uses: docker/login-action@v4 | |
| with: | |
| registry: ${{ secrets.ALIYUN_DOCKER_REGISTRY }} | |
| username: ${{ secrets.ALIYUN_DOCKER_USER }} | |
| password: ${{ secrets.ALIYUN_DOCKER_PASSWORD }} | |
| - name: Build and Push Docker Image | |
| run: | | |
| cd $GITHUB_WORKSPACE && bash ./hack/image-build-and-push.sh ${{ matrix.service }} | |
| deploy: | |
| runs-on: ubuntu-latest | |
| needs: [build, setup] # 确保在 build 和 setup 作业完成后再执行 | |
| if: ${{ needs.setup.outputs.matrix != '{"service":[]}' }} | |
| concurrency: | |
| group: global-deployment-workflow | |
| cancel-in-progress: false # 后续的 workflow 处于 pending | |
| steps: | |
| - name: SSH and deploy on server | |
| uses: appleboy/ssh-action@v1.2.5 | |
| env: | |
| SERVICES: ${{ needs.setup.outputs.matrix }} | |
| with: | |
| port: ${{ secrets.SERVER_PORT }} | |
| host: ${{ secrets.SERVER_IP }} | |
| username: ${{ secrets.SERVER_USER }} | |
| key: ${{ secrets.SERVER_SSH_KEY }} | |
| envs: SERVICES | |
| script: | | |
| # 解析 JSON 并提取 service 数组 | |
| services=$(echo "$SERVICES" | jq -r '.service[]') | |
| for service in $services; do | |
| cd /home/srv/hack | |
| bash docker-run.sh $service | |
| done |