如何创建Github操作以在Azure上部署Blazor服务器项目,目标是appsettings.staging.json?

t40tm48m  于 2023-01-02  发布在  Git
关注(0)|答案(2)|浏览(160)

我使用Github操作在Azure应用服务上部署了C#Blazor服务器项目。部署正常,但网站使用appsettings. json而不是appsettings. staging. json
我认为应该定义环境,但我找不到如何定义。
我试着提供
/p:EnvironmentName参数设置为"dotnet发布",但未成功。

env:
  API_RESOURCE_NAME: myappapica
  MYAPP_ASPNETCORE_ENVIRONMENT: Staging

jobs:
  build:
    environment: 
      name: staging
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v3

      - name: Set up .NET Core
        uses: actions/setup-dotnet@v3
        with:
          dotnet-version: "6.0.x"

      - name: Install wasm-tools
        run: dotnet workload install wasm-tools

      - uses: azure/login@v1
        with:
          creds: ${{ secrets.STAGING_AZURE_CREDENTIALS }}

      - name: Set backend env variables
        uses: azure/powershell@v1
        with:
          azPSVersion: "latest"
          inlineScript: |
            az extension add --source https://workerappscliextension.blob.core.windows.net/azure-cli-extension/containerapp-0.2.4-py2.py3-none-any.whl --yes
            az provider register --namespace Microsoft.App
            $apiUrl = "https://$(az resource show -g ${{ secrets.STAGING_AZURE_RESOURCE_GROUP_NAME }} -n ${{ env.API_RESOURCE_NAME }} --resource-type Microsoft.App/containerApps -o tsv --query properties.configuration.ingress.fqdn)"
            echo "MYAPP_API_URL=$apiUrl" | Out-File -FilePath $Env:GITHUB_ENV -Encoding utf-8 -Append

      - name: Build
        run: dotnet build src/Web/MyApp.Web --configuration Release

      - name: Publish
        run: dotnet publish --configuration Release src/Web/MyApp.Web /p:EnvironmentName=${{ env.MYAPP_ASPNETCORE_ENVIRONMENT }} --output web

      - uses: actions/upload-artifact@v3
        with:
          name: drop
          path: web

    outputs:
      MyAppApiUrl: ${{ env.MYAPP_API_URL }}

我的应用程序设置:

{
  ...
  "WebConfiguration": {
    "EnvironmentCode": "PRODUCTION",
  }
}

我的应用设置. Staging.json:

{
  ...
  "WebConfiguration": {
    "EnvironmentCode": "STAGING",
  }
}

我的网站总是显示EnvironmentCode ="生产"而不是"环境代码":"分期"。
任何帮助都很好!

7nbnzgx9

7nbnzgx91#

我通过回归基本面找到了解决方案:https://learn.microsoft.com/en-us/aspnet/core/fundamentals/environments?view=aspnetcore-7.0
对于Azure应用服务,它需要添加应用设置“ASPNETCORE_ENVIRONMENT”。由于我不想在Azure门户上手动执行此操作,我更新了我的部署作业以添加新的应用服务应用设置变量。

app-settings-json: |
            [
                {
                    "name": "ASPNETCORE_ENVIRONMENT",
                    "value": "${{ env.MYAPP_ASPNETCORE_ENVIRONMENT }}"
                }
            ]

下面是yml文件的一大部分。

env:
  API_RESOURCE_NAME: myappapica
  # Warning : This is case sensitive !
  MYAPP_ASPNETCORE_ENVIRONMENT: Staging

jobs:
  build:
  ......[SOME CODE]......
  
  deploy:
    needs: build
    environment: 
      name: staging
    if: ${{ github.ref == 'refs/heads/develop' && (github.event_name == 'push' || github.event.pull_request.merged == true || github.event_name == 'workflow_dispatch') }}
    runs-on: ubuntu-lastaging
    steps:
      - uses: actions/checkout@v3

      - uses: azure/login@v1
        with:
          creds: ${{ secrets.STAGING_AZURE_CREDENTIALS }}

      - name: Deploy ARM
        uses: azure/powershell@v1
        with:
          azPSVersion: "3.1.0"
          inlineScript: |
            az deployment group create -n ghaction -g ${{ secrets.STAGING_AZURE_RESOURCE_GROUP_NAME }} --template-file deploy/Web/staging.web.deployment.json --parameters webAppName=${{secrets.STAGING_WEBAPP_NAME}} servicePlanName=${{secrets.STAGING_SERVICE_PLAN_NAME}} servicePlanSku=${{secrets.STAGING_SERVICE_PLAN_SKU}}

      - name: Download web artifacts
        uses: actions/download-artifact@v3
        with:
          name: drop
          path: web
      
      - name: Collect App Service app settings variables
        uses: azure/powershell@v1
        with:
          azPSVersion: "3.1.0"
          inlineScript: |
            az extension add --name application-insights
            $applicationInsightConnectionString ="$(az monitor app-insights component show -g ${{ secrets.STAGING_AZURE_RESOURCE_GROUP_NAME }} --app ${{ secrets.STAGING_APPLICATION_INSIGHT_NAME }} -o tsv --query connectionString)"
            echo "APPLICATION_INSIGHT_CONNECTION_STRING=$applicationInsightConnectionString" | Out-File -FilePath $Env:GITHUB_ENV -Encoding utf-8 -Append
            
      
      - name: Update App Service app settings variables
        uses: Azure/appservice-settings@v1
        with:
          app-name: ${{ secrets.STAGING_WEBAPP_NAME }}
          app-settings-json: |
            [
                {
                    "name": "ASPNETCORE_ENVIRONMENT",
                    "value": "${{ env.MYAPP_ASPNETCORE_ENVIRONMENT }}"
                }
            ]

      - name: Azure WebApp
        uses: Azure/webapps-deploy@v2
        with:
          app-name: ${{ secrets.STAGING_WEBAPP_NAME }}
          package: web
vngu2lb8

vngu2lb82#

我已经创建了一个Blazor服务器应用程序,并使用GitHub操作部署到Azure应用服务。
如果我们默认使用Deployment Center =〉GitHub Actions部署应用程序,则将使用.yaml文件中的Production设置创建工作流文件。

  • 我们需要在GitHub Repository中手动创建工作流,而不是使用Azure Portal中的Deployment Center =〉GitHub

在Git Hub中,添加新的环境。

  • 使用staging环境创建新工作流。

有关详细信息,请参阅MSDocUsing AppSettings in Blazor WebAssembly

相关问题