如何将 github 密钥传递给使用 github 操作部署的 ASP.NET Core 应用程序?

问题描述 投票:0回答:2

我想在存储库代码中使用 GitHub 机密。 该存储库是一个 ASP.NET Core Web 应用程序。我正在使用 Github 操作将应用程序部署到 Azure 应用程序服务。

我尝试在工作流程中像这样声明环境变量

# Docs for the Azure Web Apps Deploy action: https://go.microsoft.com/fwlink/?linkid=2134798
# More GitHub Actions for Azure: https://go.microsoft.com/fwlink/?linkid=2135048

name: Azure App Service - TestJuzer(Production), Build and deploy DotnetCore app

on:
  push:
    branches:
      - master

env:
  TEST_STRING: ${{ secrets.TEST_STRING }}

jobs:
  build-and-deploy:
    runs-on: windows-latest
    
    steps:
    # checkout the repo
    - name: 'Checkout Github Action'
      uses: actions/checkout@master

    - name: Set up Node.js '12.x'
      uses: actions/setup-node@v1
      with:
        node-version: '12.x'

    - name: Set up .NET Core
      uses: actions/setup-dotnet@v1
      with:
        dotnet-version: '5.0.x'

    - name: Build with dotnet
      run: dotnet build --configuration Release
      env:
        TEST_STRING: ${{ secrets.TEST_STRING }}

    - name: dotnet publish
      run: dotnet publish -c Release -o ${{env.DOTNET_ROOT}}/myapp
      

    - name: Run Azure webapp deploy action using publish profile credentials
      uses: azure/webapps-deploy@v2
      with:
        app-name: TestJuzer
        slot-name: Production
        publish-profile: ${{ secrets.AZUREAPPSERVICE_PUBLISHPROFILE_461CFB6B8D42419FA7F58944D621BA78 }}
        package: ${{env.DOTNET_ROOT}}/myapp
      env:
        TEST_STRING: ${{ secrets.TEST_STRING }}

并像这样访问.net中的环境变量

Environment.GetEnvironmentVariable("TEST_STRING")

“TEST_STRING”是秘密的名称。但我变得空了。

我想将机密作为工作流程中的环境变量传递,并在已部署的应用程序中使用它。

任何帮助表示感谢

asp.net-core github github-actions
2个回答
0
投票

您在代理计算机上设置环境变量。在这里,您应该将其设置在应用服务上。您可以使用Azure应用程序服务设置

这是示例工作流程:

# .github/workflows/configureAppSettings.yml
on: [push]

jobs:
  build:
    runs-on: windows-latest
    steps:
    - uses: azure/login@v1
      with:
        creds: '${{ secrets.AZURE_CREDENTIALS }}'
    - uses: azure/appservice-settings@v1
      with:
        app-name: 'my-app'
        slot-name: 'staging'  # Optional and needed only if the settings have to be configured on the specific deployment slot
        app-settings-json: '${{ secrets.APP_SETTINGS }}' 
        connection-strings-json: '${{ secrets.CONNECTION_STRINGS }}'
        general-settings-json: '{"alwaysOn": "false", "webSocketsEnabled": "true"}' #'General configuration settings as Key Value pairs'
      id: settings
    - run: echo "The webapp-url is ${{ steps.settings.outputs.webapp-url }}"
    - run: |
        az logout

在你的情况下可能是这样的:

    - name: Set Web App settings
      uses: Azure/appservice-settings@v1
      with:
       app-name: 'node-rnc'
       app-settings-json: |
         [
             {
                 "name": "TEST_STRING",
                 "value": "${{ secrets.TEST_STRING }}",
                 "slotSetting": false
             }
         ]

0
投票

有一个用于设置配置设置的 github 操作步骤

https://github.com/Azure/appservice-settings

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


- name: Azure App Service Settings
  uses: Azure/appservice-settings@v1
  with:
    app-name: ${{ env.AZURE_FUNCTIONAPP_NAME }}
    app-settings-json: |
    [
       {
          "name": "appSetting1",
          "value": "${{ vars.APP_SETTING_1 }}"",
          "slotSetting": false
        }
    ]
© www.soinside.com 2019 - 2024. All rights reserved.