使用Github Actions将多个项目部署到Azure

huangapple go评论72阅读模式
英文:

Deploy multiple projects to Azure using Github Actions

问题

我遇到了与这个问题相同的问题。我已经修改了自动生成的yml文件,使得在构建步骤中有如下内容:

之前:

- name: Build with dotnet
  run: dotnet build --configuration Release

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

之后:

- name: Build with dotnet
  run: dotnet build {项目文件夹}/project_name.csproj --configuration Release

- name: dotnet publish
  run: dotnet publish {项目文件夹}/project_name.csproj -c Release -o ${{env.DOTNET_ROOT}}/myapp

部署成功,构建和部署操作都没有出现任何错误。但是,我的Web应用程序无法加载,显示403错误,当我检查wwwroot文件夹时,它是空的,没有文件被部署。

以下是部署的相关代码:

deploy:
    runs-on: windows-latest
    needs: build
    environment:
      name: 'dev'
      url: ${{ steps.deploy-to-webapp.outputs.webapp-url }}

    steps:
      - name: Download artifact from build job
        uses: actions/download-artifact@v2
        with:
          name: .net-app

      - name: Deploy to Azure Web App
        id: deploy-to-webapp
        uses: azure/webapps-deploy@v2
        with:
          app-name: 'myappname'
          slot-name: 'dev'
          publish-profile: ${{ secrets.AZUREAPPSERVICE_PUBLISHPROFILE_****** }}
          package: .

我错过了什么?

英文:

I'm encountering the same issue as this one. I have already modified the auto-generated yml file such that I have this in the build steps:

Before:

- name: Build with dotnet
  run: dotnet build --configuration Release

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

After:

  - name: Build with dotnet
    run: dotnet build {project folder}/project_name.csproj --configuration Release

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

The deployment is successful where both the build and deploy actions are completed without any errors. However, my web app is not loading showing a 403 error, and when I checked the wwwroot folder it's empty and no files were deployed.

Here's the relevant code for the deployment:

deploy:
    runs-on: windows-latest
    needs: build
    environment:
      name: 'dev'
      url: ${{ steps.deploy-to-webapp.outputs.webapp-url }}

    steps:
      - name: Download artifact from build job
        uses: actions/download-artifact@v2
        with:
          name: .net-app

      - name: Deploy to Azure Web App
        id: deploy-to-webapp
        uses: azure/webapps-deploy@v2
        with:
          app-name: 'myappname'
          slot-name: 'dev'
          publish-profile: ${{ secrets.AZUREAPPSERVICE_PUBLISHPROFILE_****** }}
          package: .

What did I miss?

答案1

得分: 1

dotnet publish不会将您的应用程序发布到Azure App Service。它只会准备一个部署包。

要将应用程序部署到Azure App Service,您需要使用此任务 azure/webapps-deploy

查看这里的示例工作流程

# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.

# GitHub recommends pinning actions to a commit SHA.
# To get a newer version, you will need to update the SHA.
# You can also reference a tag or branch, but the action may change without warning.

name: Build and deploy ASP.Net Core app to an Azure Web App

env:
  AZURE_WEBAPP_NAME: MY_WEBAPP_NAME   # set this to your application's name
  AZURE_WEBAPP_PACKAGE_PATH: '.'      # set this to the path to your web app project, defaults to the repository root
  DOTNET_VERSION: '5'                 # set this to the .NET Core version to use

on:
  push:
    branches:
      - main

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v3

      - name: Set up .NET Core
        uses: actions/setup-dotnet@v3
        with:
          dotnet-version: ${{ env.DOTNET_VERSION }}

      - name: Set up dependency caching for faster builds
        uses: actions/cache@v3
        with:
          path: ~/.nuget/packages
          key: ${{ runner.os }}-nuget-${{ hashFiles('**/packages.lock.json') }}
          restore-keys: |
                        ${{ runner.os }}-nuget-

      - name: Build with dotnet
        run: dotnet build --configuration Release

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

      - name: Upload artifact for deployment job
        uses: actions/upload-artifact@v3
        with:
          name: .net-app
          path: ${{env.DOTNET_ROOT}}/myapp

  deploy:
    runs-on: ubuntu-latest
    needs: build
    environment:
      name: 'production'
      url: ${{ steps.deploy-to-webapp.outputs.webapp-url }}

    steps:
      - name: Download artifact from build job
        uses: actions/download-artifact@v3
        with:
          name: .net-app

      - name: Deploy to Azure Web App
        id: deploy-to-webapp
        uses: azure/webapps-deploy@azure/webapps-deploy@v2
        with:
          app-name: ${{ env.AZURE_WEBAPP_NAME }}
          publish-profile: ${{ secrets.AZURE_WEBAPP_PUBLISH_PROFILE }}
          package: ${{ env.AZURE_WEBAPP_PACKAGE_PATH }}

如果您想部署多个项目,可以使用类似于以下示例的矩阵:

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        include:
          - app: 'web-app-name-1'
            artifact: 'artifact-path-1'
            profileName: 'profile-1'
          - app: 'web-app-name-2'
            artifact: 'artifact-path-2'
            profileName: 'profile-2'
          - app: 'web-app-name-3'
            artifact: 'artifact-path-3'
            profileName: 'profile-3'
    
    steps:
    - name: Download artifact from build job
      uses: actions/download-artifact@v3
      with:
        name: ${{ matrix.artifact }}

    - name: Deploy to Azure Web App
      id: deploy-to-webapp
      uses: azure/webapps-deploy@azure/webapps-deploy@v2
      with:
        app-name: ${{ matrix.app }}
        publish-profile: ${{ secrets[matrix.profileName] }}
        package: ${{ matrix.artifact }}
英文:

dotnet publish doesn't publish your app to Azure App Service. It will prepare just a package for deployment.

To deploy app to Azure App Service you need to use this task azure/webapps-deploy.

Take a look here on an example workflow:

# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.
# GitHub recommends pinning actions to a commit SHA.
# To get a newer version, you will need to update the SHA.
# You can also reference a tag or branch, but the action may change without warning.
name: Build and deploy ASP.Net Core app to an Azure Web App
env:
AZURE_WEBAPP_NAME: MY_WEBAPP_NAME   # set this to your application's name
AZURE_WEBAPP_PACKAGE_PATH: '.'      # set this to the path to your web app project, defaults to the repository root
DOTNET_VERSION: '5'                 # set this to the .NET Core version to use
on:
push:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up .NET Core
uses: actions/setup-dotnet@v3
with:
dotnet-version: ${{ env.DOTNET_VERSION }}
- name: Set up dependency caching for faster builds
uses: actions/cache@v3
with:
path: ~/.nuget/packages
key: ${{ runner.os }}-nuget-${{ hashFiles('**/packages.lock.json') }}
restore-keys: |
${{ runner.os }}-nuget-
- name: Build with dotnet
run: dotnet build --configuration Release
- name: dotnet publish
run: dotnet publish -c Release -o ${{env.DOTNET_ROOT}}/myapp
- name: Upload artifact for deployment job
uses: actions/upload-artifact@v3
with:
name: .net-app
path: ${{env.DOTNET_ROOT}}/myapp
deploy:
runs-on: ubuntu-latest
needs: build
environment:
name: 'production'
url: ${{ steps.deploy-to-webapp.outputs.webapp-url }}
steps:
- name: Download artifact from build job
uses: actions/download-artifact@v3
with:
name: .net-app
- name: Deploy to Azure Web App
id: deploy-to-webapp
uses: azure/webapps-deploy@azure/webapps-deploy@v2
with:
app-name: ${{ env.AZURE_WEBAPP_NAME }}
publish-profile: ${{ secrets.AZURE_WEBAPP_PUBLISH_PROFILE }}
package: ${{ env.AZURE_WEBAPP_PACKAGE_PATH }}

If you want to deploy multiple project you can do it using matrix like here

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        include:
          - app: 'web-app-name-1'
            artifact: 'artifact-path-1'
            profileName: 'profile-1'
          - app: 'web-app-name-2'
            artifact: 'artifact-path-2'
            profileName: 'profile-2'
          - app: 'web-app-name-3'
            artifact: 'artifact-path-3'
            profileName: 'profile-3'
    
    steps:
    - name: Download artifact from build job
      uses: actions/download-artifact@v3
      with:
        name: ${{ matrix.artifact }}

    - name: Deploy to Azure Web App
      id: deploy-to-webapp
      uses: azure/webapps-deploy@azure/webapps-deploy@v2
      with:
        app-name: ${{ matrix.app }}
        publish-profile: ${{ secrets[matrix.profileName] }}
        package: ${{ matrix.artifact }}

huangapple
  • 本文由 发表于 2023年7月6日 11:29:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76625311.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定