Movatterモバイル変換


[0]ホーム

URL:


Skip to main content

This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Download Microsoft EdgeMore info about Internet Explorer and Microsoft Edge
Table of contentsExit editor mode

Quickstart: Create a GitHub workflow to publish an app

Feedback

In this article

In this quickstart, you will learn how to create a GitHub workflow to publish your .NET app from source code. Automatically publishing your .NET app from GitHub to a destination is referred to as a continuous deployment (CD). There are many possible destinations to publish an application, in this quickstart you'll publish to Azure.

Prerequisites

Add publish profile

To publish the app to Azure, open the Azure portal for the App Service instance of the application. In the resourceOverview, selectGet publish profile and save the *.PublishSetting file locally.

Azure Portal, App Service resource: Get publish profile

Warning

The publish profile contains sensitive information, such as credentials for accessing your Azure App Service resource. This information should always be treated very carefully.

In the GitHub repository, navigate toSettings and selectSecrets from the left navigation menu. SelectNew repository secret, to add a new secret.

GitHub / Settings / Secret: Add new repository secret

EnterAZURE_PUBLISH_PROFILE as theName, and paste the XML content from the publish profile into theValue text area. SelectAdd secret. For more information, seeEncrypted secrets.

Create a workflow file

In the GitHub repository, add a new YAML file to the.github/workflows directory. Choose a meaningful file name, something that will clearly indicate what the workflow is intended to do. For more information, seeWorkflow file.

Important

GitHub requires that workflow composition files to be placed within the.github/workflows directory.

Workflow files typically define a composition of one or more GitHub Action via thejobs.<job_id>/steps[*]. For more information, see,Workflow syntax for GitHub Actions.

Create a new file namedpublish-app.yml, copy and paste the following YML contents into it:

name: publishon:  push:    branches: [ production ]env:  AZURE_WEBAPP_NAME: DotNetWeb  AZURE_WEBAPP_PACKAGE_PATH: '.' # Set this to the path to your web app project, defaults to the repository root:  DOTNET_VERSION: '6.0.401' # The .NET SDK version to usejobs:  publish:    runs-on: ubuntu-latest    steps:    - uses: actions/checkout@v3    - name: Setup .NET Core      uses: actions/setup-dotnet@v3      with:        dotnet-version: ${{ env.DOTNET_VERSION }}    - name: Install dependencies      run: dotnet restore          - name: Build      run: |        cd DotNet.WebApp        dotnet build --configuration Release --no-restore        dotnet publish -c Release -o ../dotnet-webapp -r linux-x64 --self-contained true /p:UseAppHost=true    - name: Test      run: |        cd DotNet.WebApp.Tests        dotnet test --no-restore --verbosity normal          - uses: azure/webapps-deploy@v2      name: Deploy      with:        app-name: ${{ env.AZURE_WEBAPP_NAME }}        publish-profile: ${{ secrets.AZURE_PUBLISH_PROFILE }}        package: '${{ env.AZURE_WEBAPP_PACKAGE_PATH }}/dotnet-webapp'

In the preceding workflow composition:

  • Thename: publish defines the name, "publish" will appear in workflow status badges.

    name: publish
  • Theon node signifies the events that trigger the workflow:

    on:  push:    branches: [ production ]
    • Triggered when apush occurs on theproduction branch.
  • Theenv node defines named environment variables (env var).

    env:  AZURE_WEBAPP_NAME: DotNetWeb  AZURE_WEBAPP_PACKAGE_PATH: '.' # Set this to the path to your web app project, defaults to the repository root:  DOTNET_VERSION: '6.0.401' # The .NET SDK version to use
    • The environment variableAZURE_WEBAPP_NAME is assigned the valueDotNetWeb.
    • The environment variableAZURE_WEBAPP_PACKAGE_PATH is assigned the value'.'.
    • The environment variableDOTNET_VERSION is assigned the value'6.0.401'. The environment variable is later referenced to specify thedotnet-version of theactions/setup-dotnet@v3 GitHub Action.
  • Thejobs node builds out the steps for the workflow to take.

    jobs:  publish:    runs-on: ubuntu-latest    steps:    - uses: actions/checkout@v3    - name: Setup .NET Core      uses: actions/setup-dotnet@v3      with:        dotnet-version: ${{ env.DOTNET_VERSION }}    - name: Install dependencies      run: dotnet restore          - name: Build      run: |        cd DotNet.WebApp        dotnet build --configuration Release --no-restore        dotnet publish -c Release -o ../dotnet-webapp -r linux-x64 --self-contained true /p:UseAppHost=true    - name: Test      run: |        cd DotNet.WebApp.Tests        dotnet test --no-restore --verbosity normal          - uses: azure/webapps-deploy@v2      name: Deploy      with:        app-name: ${{ env.AZURE_WEBAPP_NAME }}        publish-profile: ${{ secrets.AZURE_PUBLISH_PROFILE }}        package: '${{ env.AZURE_WEBAPP_PACKAGE_PATH }}/dotnet-webapp'
    • There is a single job, namedpublish that will run on the latest version of Ubuntu.
    • Theactions/setup-dotnet@v3 GitHub Action is used to set up the .NET SDK with the specified version from theDOTNET_VERSION environment variable.
    • Thedotnet restore command is called.
    • Thedotnet build command is called.
    • Thedotnet publish command is called.
    • Thedotnet test command is called.
    • Theazure/webapps-deploy@v2 GitHub Action deploys the app with the givenpublish-profile andpackage.
      • Thepublish-profile is assigned from theAZURE_PUBLISH_PROFILE repository secret.

Create a workflow status badge

It's common nomenclature for GitHub repositories to have aREADME.md file at the root of the repository directory. Likewise, it's nice to report the latest status for various workflows. All workflows can generate a status badge, which are visually appealing within theREADME.md file. To add the workflow status badge:

  1. From the GitHub repository select theActions navigation option.

  2. All repository workflows are displayed on the left-side, select the desired workflow and the ellipsis (...) button.

    • The ellipsis (...) button expands the menu options for the selected workflow.
  3. Select theCreate status badge menu option.

    GitHub: Create status badge

  4. Select theCopy status badge Markdown button.

    GitHub: Copy status badge Markdown

  5. Paste the Markdown into theREADME.md file, save the file, commit and push the changes.

For more, seeAdding a workflow status badge.

Example publish workflow status badge

PassingFailingNo status
GitHub: publish passing badgeGitHub: publish failing badgeGitHub: publish no-status badge

See also

Next steps

Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, seeour contributor guide.

Feedback

Was this page helpful?

YesNoNo

Need help with this topic?

Want to try using Ask Learn to clarify or guide you through this topic?

Suggest a fix?

  • Last updated on

In this article

Was this page helpful?

YesNo
NoNeed help with this topic?

Want to try using Ask Learn to clarify or guide you through this topic?

Suggest a fix?