From 59714df27167e0796a2705f455b76ff5d8ff1c01 Mon Sep 17 00:00:00 2001 From: Xu-create-ops Date: Sat, 14 Jun 2025 16:00:22 +0800 Subject: [PATCH 1/3] update --- .../workflows/azure-back-end-03b2a610f.yml | 52 ++++++++++++++---- ...e-static-web-apps-green-moss-03b2a610f.yml | 53 +++++++++++++++---- 2 files changed, 86 insertions(+), 19 deletions(-) diff --git a/.github/workflows/azure-back-end-03b2a610f.yml b/.github/workflows/azure-back-end-03b2a610f.yml index 4831ae3..b7f2129 100644 --- a/.github/workflows/azure-back-end-03b2a610f.yml +++ b/.github/workflows/azure-back-end-03b2a610f.yml @@ -1,13 +1,16 @@ -name: Build and deploy .NET app to Azure Web App +name: CI/CD for .NET app to Azure on: + # Automatically trigger on all branches push and PR to main push: - branches: - - main + branches: ['**'] + pull_request: + branches: ['main'] + # Allow manual trigger with environment selection workflow_dispatch: inputs: environment: - description: '选择部署环境' + description: 'Select deployment environment' required: true default: 'Test' type: choice @@ -19,11 +22,42 @@ env: DOTNET_VERSION: '9.0.x' jobs: - build-and-deploy: + # ✅ CI: build + test + ci-check: runs-on: windows-latest + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Setup .NET SDK + uses: actions/setup-dotnet@v4 + with: + dotnet-version: ${{ env.DOTNET_VERSION }} + + - name: Restore dependencies + run: dotnet restore ./dev-share-api/dev-share-api.csproj + + - name: Build + run: dotnet build ./dev-share-api/dev-share-api.csproj --configuration Release --no-restore + + - name: Run tests (optional) + run: echo "✅ 你可以在这里添加 dotnet test" + + # ✅ Deploy step only triggered on push to main or manual trigger + deploy: + needs: ci-check + runs-on: windows-latest + + # Conditional deploy: only on manual trigger or push to main + if: | + github.event_name == 'workflow_dispatch' || + (github.event_name == 'push' && github.ref == 'refs/heads/main') + + env: + IS_PRODUCTION: ${{ github.event.inputs.environment == 'Production' }} steps: - - name: Checkout source + - name: Checkout code uses: actions/checkout@v4 - name: Setup .NET SDK @@ -39,14 +73,14 @@ jobs: - name: Publish run: | - $envName = if ("${{ github.event.inputs.environment }}" -eq "Production") { "Production" } else { "Test" } + $envName = if ("${{ env.IS_PRODUCTION }}" -eq "true") { "Production" } else { "Test" } dotnet publish ./dev-share-api/dev-share-api.csproj ` --configuration Release ` --output ./publish ` -p:EnvironmentName=$envName - name: Deploy to Azure Web App - Production - if: ${{ github.event.inputs.environment == 'Production' }} + if: ${{ env.IS_PRODUCTION == 'true' }} uses: azure/webapps-deploy@v3 with: app-name: prod-share-app @@ -54,7 +88,7 @@ jobs: package: ./publish - name: Deploy to Azure Web App - Test - if: ${{ github.event.inputs.environment == 'Test' }} + if: ${{ env.IS_PRODUCTION != 'true' }} uses: azure/webapps-deploy@v3 with: app-name: dev-share diff --git a/.github/workflows/azure-static-web-apps-green-moss-03b2a610f.yml b/.github/workflows/azure-static-web-apps-green-moss-03b2a610f.yml index 0e25ead..062f7c9 100644 --- a/.github/workflows/azure-static-web-apps-green-moss-03b2a610f.yml +++ b/.github/workflows/azure-static-web-apps-green-moss-03b2a610f.yml @@ -1,13 +1,16 @@ -name: Build and deploy Next.js app to Azure Web App +name: CI/CD for Next.js app to Azure Web App on: + # Trigger on all branches push and PR to main push: - branches: - - main + branches: ['**'] + pull_request: + branches: ['main'] + # Allow manual trigger with environment selection workflow_dispatch: inputs: environment: - description: '选择部署环境' + description: 'Select deployment environment' required: true default: 'Test' type: choice @@ -19,11 +22,37 @@ env: NODE_VERSION: "20.x" jobs: - build-and-deploy: + # ✅ CI step for build verification on all branches and PRs + ci-check: runs-on: ubuntu-latest steps: - - name: Checkout source + - name: Checkout source code + uses: actions/checkout@v4 + + - name: Set up Node.js + uses: actions/setup-node@v4 + with: + node-version: ${{ env.NODE_VERSION }} + + - name: Install dependencies and build (CI only) + run: | + npm install + npm run build + working-directory: ./dev-share-ui + + # ✅ Deploy step only triggered on push to main or manual trigger + deploy: + needs: ci-check + runs-on: ubuntu-latest + + # Conditional deploy: only on manual trigger or push to main + if: | + github.event_name == 'workflow_dispatch' || + (github.event_name == 'push' && github.ref == 'refs/heads/main') + + steps: + - name: Checkout source code uses: actions/checkout@v4 - name: Set up Node.js @@ -33,15 +62,19 @@ jobs: - name: Copy correct .env file run: | - cp ./dev-share-ui/.env.${{ github.event.inputs.environment == 'Production' && 'production' || 'test' }} ./dev-share-ui/.env + if [[ "${{ github.event.inputs.environment }}" == "Production" ]]; then + cp ./dev-share-ui/.env.production ./dev-share-ui/.env + else + cp ./dev-share-ui/.env.test ./dev-share-ui/.env + fi - - name: npm install and build + - name: Install dependencies and build run: | npm install npm run build working-directory: ./dev-share-ui - - name: Copy static assets for standalone deployment + - name: Prepare static assets for standalone deployment run: | cd dev-share-ui cp -r .next/static .next/standalone/.next/ @@ -56,7 +89,7 @@ jobs: package: dev-share-ui/.next/standalone - name: Deploy to Azure Web App - Test - if: ${{ github.event.inputs.environment == 'Test' }} + if: ${{ github.event.inputs.environment != 'Production' }} uses: azure/webapps-deploy@v3 with: app-name: dev-share-ui-v1 From f913e3455d5ec07e644d09cd2d38f8ce5b53b8ca Mon Sep 17 00:00:00 2001 From: Xu-create-ops Date: Sat, 14 Jun 2025 16:16:54 +0800 Subject: [PATCH 2/3] update --- .../workflows/azure-back-end-03b2a610f.yml | 43 ++++--------------- ...e-static-web-apps-green-moss-03b2a610f.yml | 30 ++----------- 2 files changed, 12 insertions(+), 61 deletions(-) diff --git a/.github/workflows/azure-back-end-03b2a610f.yml b/.github/workflows/azure-back-end-03b2a610f.yml index b7f2129..d078de6 100644 --- a/.github/workflows/azure-back-end-03b2a610f.yml +++ b/.github/workflows/azure-back-end-03b2a610f.yml @@ -1,12 +1,8 @@ name: CI/CD for .NET app to Azure on: - # Automatically trigger on all branches push and PR to main - push: - branches: ['**'] pull_request: branches: ['main'] - # Allow manual trigger with environment selection workflow_dispatch: inputs: environment: @@ -17,45 +13,22 @@ on: options: - Test - Production + push: + branches: + - main env: DOTNET_VERSION: '9.0.x' jobs: - # ✅ CI: build + test - ci-check: - runs-on: windows-latest - steps: - - name: Checkout code - uses: actions/checkout@v4 - - - name: Setup .NET SDK - uses: actions/setup-dotnet@v4 - with: - dotnet-version: ${{ env.DOTNET_VERSION }} - - - name: Restore dependencies - run: dotnet restore ./dev-share-api/dev-share-api.csproj - - - name: Build - run: dotnet build ./dev-share-api/dev-share-api.csproj --configuration Release --no-restore - - - name: Run tests (optional) - run: echo "✅ 你可以在这里添加 dotnet test" - - # ✅ Deploy step only triggered on push to main or manual trigger deploy: - needs: ci-check runs-on: windows-latest - # Conditional deploy: only on manual trigger or push to main + # Only deploy when manually triggered or pushing to main if: | - github.event_name == 'workflow_dispatch' || + github.event_name == 'workflow_dispatch' || (github.event_name == 'push' && github.ref == 'refs/heads/main') - env: - IS_PRODUCTION: ${{ github.event.inputs.environment == 'Production' }} - steps: - name: Checkout code uses: actions/checkout@v4 @@ -73,14 +46,14 @@ jobs: - name: Publish run: | - $envName = if ("${{ env.IS_PRODUCTION }}" -eq "true") { "Production" } else { "Test" } + $envName = if ("${{ github.event.inputs.environment }}" -eq "Production") { "Production" } else { "Test" } dotnet publish ./dev-share-api/dev-share-api.csproj ` --configuration Release ` --output ./publish ` -p:EnvironmentName=$envName - name: Deploy to Azure Web App - Production - if: ${{ env.IS_PRODUCTION == 'true' }} + if: ${{ github.event.inputs.environment == 'Production' }} uses: azure/webapps-deploy@v3 with: app-name: prod-share-app @@ -88,7 +61,7 @@ jobs: package: ./publish - name: Deploy to Azure Web App - Test - if: ${{ env.IS_PRODUCTION != 'true' }} + if: ${{ github.event.inputs.environment != 'Production' }} uses: azure/webapps-deploy@v3 with: app-name: dev-share diff --git a/.github/workflows/azure-static-web-apps-green-moss-03b2a610f.yml b/.github/workflows/azure-static-web-apps-green-moss-03b2a610f.yml index 062f7c9..a83304d 100644 --- a/.github/workflows/azure-static-web-apps-green-moss-03b2a610f.yml +++ b/.github/workflows/azure-static-web-apps-green-moss-03b2a610f.yml @@ -1,12 +1,8 @@ name: CI/CD for Next.js app to Azure Web App on: - # Trigger on all branches push and PR to main - push: - branches: ['**'] pull_request: branches: ['main'] - # Allow manual trigger with environment selection workflow_dispatch: inputs: environment: @@ -17,36 +13,18 @@ on: options: - Test - Production + push: + branches: + - main env: NODE_VERSION: "20.x" jobs: - # ✅ CI step for build verification on all branches and PRs - ci-check: - runs-on: ubuntu-latest - - steps: - - name: Checkout source code - uses: actions/checkout@v4 - - - name: Set up Node.js - uses: actions/setup-node@v4 - with: - node-version: ${{ env.NODE_VERSION }} - - - name: Install dependencies and build (CI only) - run: | - npm install - npm run build - working-directory: ./dev-share-ui - - # ✅ Deploy step only triggered on push to main or manual trigger deploy: - needs: ci-check runs-on: ubuntu-latest - # Conditional deploy: only on manual trigger or push to main + # Only deploy on manual trigger or main push if: | github.event_name == 'workflow_dispatch' || (github.event_name == 'push' && github.ref == 'refs/heads/main') From 427960c9f9575ac4dd68145b69db619f7436a6ac Mon Sep 17 00:00:00 2001 From: Xu-create-ops Date: Sat, 14 Jun 2025 16:28:11 +0800 Subject: [PATCH 3/3] update --- .../workflows/azure-back-end-03b2a610f.yml | 40 ++++++++++++---- ...e-static-web-apps-green-moss-03b2a610f.yml | 48 ++++++++++++------- 2 files changed, 61 insertions(+), 27 deletions(-) diff --git a/.github/workflows/azure-back-end-03b2a610f.yml b/.github/workflows/azure-back-end-03b2a610f.yml index d078de6..d72b3b9 100644 --- a/.github/workflows/azure-back-end-03b2a610f.yml +++ b/.github/workflows/azure-back-end-03b2a610f.yml @@ -1,8 +1,15 @@ name: CI/CD for .NET app to Azure on: + # ✅ PR 到 main:只执行 CI pull_request: branches: ['main'] + + # ✅ 合并 PR 到 main:自动部署到 Test + push: + branches: ['main'] + + # ✅ 手动触发部署 workflow_dispatch: inputs: environment: @@ -13,21 +20,34 @@ on: options: - Test - Production - push: - branches: - - main env: DOTNET_VERSION: '9.0.x' jobs: - deploy: + ci-check: + # ✅ 只在 PR 阶段执行 + if: github.event_name == 'pull_request' runs-on: windows-latest + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Setup .NET SDK + uses: actions/setup-dotnet@v4 + with: + dotnet-version: ${{ env.DOTNET_VERSION }} + + - name: Restore dependencies + run: dotnet restore ./dev-share-api/dev-share-api.csproj - # Only deploy when manually triggered or pushing to main - if: | - github.event_name == 'workflow_dispatch' || - (github.event_name == 'push' && github.ref == 'refs/heads/main') + - name: Build + run: dotnet build ./dev-share-api/dev-share-api.csproj --configuration Release --no-restore + + deploy: + # ✅ 仅在 main 合并 或 workflow_dispatch 时执行 + if: github.event_name == 'push' || github.event_name == 'workflow_dispatch' + runs-on: windows-latest steps: - name: Checkout code @@ -53,7 +73,7 @@ jobs: -p:EnvironmentName=$envName - name: Deploy to Azure Web App - Production - if: ${{ github.event.inputs.environment == 'Production' }} + if: github.event.inputs.environment == 'Production' uses: azure/webapps-deploy@v3 with: app-name: prod-share-app @@ -61,7 +81,7 @@ jobs: package: ./publish - name: Deploy to Azure Web App - Test - if: ${{ github.event.inputs.environment != 'Production' }} + if: github.event.inputs.environment != 'Production' uses: azure/webapps-deploy@v3 with: app-name: dev-share diff --git a/.github/workflows/azure-static-web-apps-green-moss-03b2a610f.yml b/.github/workflows/azure-static-web-apps-green-moss-03b2a610f.yml index a83304d..faa224e 100644 --- a/.github/workflows/azure-static-web-apps-green-moss-03b2a610f.yml +++ b/.github/workflows/azure-static-web-apps-green-moss-03b2a610f.yml @@ -3,6 +3,10 @@ name: CI/CD for Next.js app to Azure Web App on: pull_request: branches: ['main'] + + push: + branches: ['main'] + workflow_dispatch: inputs: environment: @@ -13,53 +17,63 @@ on: options: - Test - Production - push: - branches: - - main env: NODE_VERSION: "20.x" jobs: - deploy: + ci-check: + if: github.event_name == 'pull_request' runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Setup Node.js + uses: actions/setup-node@v4 + with: + node-version: ${{ env.NODE_VERSION }} - # Only deploy on manual trigger or main push - if: | - github.event_name == 'workflow_dispatch' || - (github.event_name == 'push' && github.ref == 'refs/heads/main') + - name: Install & Build (CI only) + run: | + npm install + npm run build + working-directory: ./dev-share-ui + deploy: + if: github.event_name == 'push' || github.event_name == 'workflow_dispatch' + runs-on: ubuntu-latest steps: - - name: Checkout source code + - name: Checkout code uses: actions/checkout@v4 - - name: Set up Node.js + - name: Setup Node.js uses: actions/setup-node@v4 with: node-version: ${{ env.NODE_VERSION }} - name: Copy correct .env file run: | + envFile=".env.test" if [[ "${{ github.event.inputs.environment }}" == "Production" ]]; then - cp ./dev-share-ui/.env.production ./dev-share-ui/.env - else - cp ./dev-share-ui/.env.test ./dev-share-ui/.env + envFile=".env.production" fi + cp ./dev-share-ui/$envFile ./dev-share-ui/.env - - name: Install dependencies and build + - name: Install & Build run: | npm install npm run build working-directory: ./dev-share-ui - - name: Prepare static assets for standalone deployment + - name: Prepare static assets run: | cd dev-share-ui cp -r .next/static .next/standalone/.next/ if [ -d "public" ]; then cp -r public .next/standalone/; fi - name: Deploy to Azure Web App - Production - if: ${{ github.event.inputs.environment == 'Production' }} + if: github.event.inputs.environment == 'Production' uses: azure/webapps-deploy@v3 with: app-name: prod-share-ui @@ -67,7 +81,7 @@ jobs: package: dev-share-ui/.next/standalone - name: Deploy to Azure Web App - Test - if: ${{ github.event.inputs.environment != 'Production' }} + if: github.event.inputs.environment != 'Production' uses: azure/webapps-deploy@v3 with: app-name: dev-share-ui-v1