更新 哎!是世界观! #14
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: Build and Deploy | |
| concurrency: production | |
| on: | |
| push: | |
| branches: | |
| - main | |
| workflow_dispatch: | |
| jobs: | |
| # =============================================================== | |
| # JOB 1: Build and Push Docker Image to GHCR | |
| # =============================================================== | |
| build-and-push-image: | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: read | |
| packages: write | |
| outputs: | |
| image_tag: ${{ steps.meta.outputs.version }} # 输出 commit SHA 标签给下一个 job | |
| image_repo: ghcr.io/${{ github.repository }} | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| with: | |
| submodules: true | |
| - name: Log in to GHCR | |
| uses: docker/login-action@v3 | |
| with: | |
| registry: ghcr.io | |
| username: ${{ github.repository_owner }} | |
| password: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Extract metadata for Docker | |
| id: meta | |
| uses: docker/metadata-action@v5 | |
| with: | |
| images: ghcr.io/${{ github.repository }} | |
| tags: type=sha,prefix=,format=short | |
| - name: Build and push Docker image | |
| uses: docker/build-push-action@v6 | |
| with: | |
| context: . | |
| push: true | |
| tags: ${{ steps.meta.outputs.tags }} | |
| labels: ${{ steps.meta.outputs.labels }} | |
| # =============================================================== | |
| # JOB 2: Deploy to Kubernetes using Helm | |
| # =============================================================== | |
| deploy-to-kubernetes: | |
| runs-on: ubuntu-latest | |
| needs: build-and-push-image # 依赖上一个 job | |
| environment: Production | |
| permissions: | |
| contents: read | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 # 获取所有历史记录,以便比较文件变更 | |
| - name: Set up Helm | |
| uses: azure/setup-helm@v4 | |
| - name: Set up Kubeconfig | |
| run: | | |
| mkdir -p ~/.kube | |
| echo "${{ secrets.KUBECONFIG }}" > ~/.kube/config | |
| chmod 600 ~/.kube/config | |
| - name: Deploy with local Helm chart | |
| run: | | |
| # 定义变量 | |
| CHART_PATH="./charts/alienfamily-novel" # 你的本地 Chart 路径 | |
| RELEASE_NAME="alienfamily-novel" # 你想要在 K8s 中部署的 release 名称 | |
| NAMESPACE="alienfamily-novel" # 你要部署到的命名空间 | |
| # 直接使用本地 Chart 路径执行 Helm 部署 | |
| echo "Upgrading release $RELEASE_NAME in namespace $NAMESPACE using local chart at $CHART_PATH..." | |
| helm upgrade --install $RELEASE_NAME $CHART_PATH \ | |
| --namespace $NAMESPACE \ | |
| --create-namespace \ | |
| --set image.tag=${{ needs.build-and-push-image.outputs.image_tag }} \ | |
| -f $CHART_PATH/values.yaml \ | |
| --atomic \ | |
| --wait |