Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Devops ebook #23137

Merged
merged 50 commits into from
Mar 16, 2021
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
20b5383
Initial import of chapter and images
colindembovsky Mar 4, 2021
8a2b50f
Updating image paths and styling
colindembovsky Mar 5, 2021
2cb9bbb
Update master to main
colindembovsky Mar 5, 2021
0e14897
Remove update of .NET framework after upgrade
colindembovsky Mar 5, 2021
de23c70
Linting updates
colindembovsky Mar 5, 2021
039de3d
Linting updates
colindembovsky Mar 5, 2021
5a4dc27
Linting update
colindembovsky Mar 5, 2021
e4d9577
Merge branch 'devops-ebook' of github.com:dotnet/docs into devops-ebook
colindembovsky Mar 7, 2021
0cb753f
Update page url in index
colindembovsky Mar 7, 2021
8f53bec
Fix grammar
colindembovsky Mar 10, 2021
086c797
Fix grammar
colindembovsky Mar 10, 2021
ec71c56
Fix grammar
colindembovsky Mar 10, 2021
e7be073
Fix grammar
colindembovsky Mar 10, 2021
49f8216
Fix spelling
colindembovsky Mar 10, 2021
b164b53
Fix grammar
colindembovsky Mar 10, 2021
0b0e492
Fix grammar
colindembovsky Mar 10, 2021
2a8ee69
Fix spelling
colindembovsky Mar 10, 2021
102524e
Merge branch 'devops-ebook' of github.com:colindembovsky/docs into de…
colindembovsky Mar 7, 2021
154ae58
Update docs/architecture/devops-for-aspnet-developers/actions-build.md
colindembovsky Mar 11, 2021
8347e7f
Update spelling
colindembovsky Mar 11, 2021
135987d
Adding comma
colindembovsky Mar 11, 2021
14a1f43
Fix casing
colindembovsky Mar 11, 2021
72772dd
Fix spelling
colindembovsky Mar 11, 2021
7f6f9fd
Fix spelling
colindembovsky Mar 11, 2021
63ca06d
Fix spelling
colindembovsky Mar 11, 2021
6260ddc
Change click to select
colindembovsky Mar 11, 2021
59e8d83
Change click to select
colindembovsky Mar 11, 2021
f92a781
Contractions
colindembovsky Mar 12, 2021
9285d94
Contractions and update of "click" to "select"
colindembovsky Mar 12, 2021
019c378
Grammar fix
colindembovsky Mar 12, 2021
7886e77
Fix spelling
colindembovsky Mar 12, 2021
68c3e6d
Fix casing
colindembovsky Mar 12, 2021
50a7d13
Fix casing
colindembovsky Mar 12, 2021
2aa6fcb
Fix emphasis
colindembovsky Mar 12, 2021
d256dbd
Add emphasis
colindembovsky Mar 12, 2021
97b5320
Fix grammar
colindembovsky Mar 12, 2021
e9de20f
Make active
colindembovsky Mar 12, 2021
2d8a9f8
Add comma
colindembovsky Mar 12, 2021
6b5b328
Fix grammar
colindembovsky Mar 12, 2021
7f5ee98
Update case
colindembovsky Mar 12, 2021
4894186
Add missing word
colindembovsky Mar 12, 2021
263c7a2
Fix spelling
colindembovsky Mar 12, 2021
8983d11
Fix URL
colindembovsky Mar 12, 2021
c29d261
Fix spelling
colindembovsky Mar 12, 2021
a3305d2
Indenting snippets
colindembovsky Mar 9, 2021
0d126e1
Various review changes
colindembovsky Mar 12, 2021
e07cd2d
Update grammar
colindembovsky Mar 12, 2021
0e5b082
Change "click" to "select"
colindembovsky Mar 12, 2021
a618ac6
Merge branch 'devops-ebook' of github.com:colindembovsky/docs into de…
colindembovsky Mar 12, 2021
a605f5c
Various final edits
colindembovsky Mar 15, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
193 changes: 193 additions & 0 deletions docs/architecture/devops-for-aspnet-developers/actions-build.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
---
title: Deploy an app to App Service - DevOps with .NET and GitHub Actions
description: Deploy a .NET app to Azure App Service, the first step for DevOps with .NET and GitHub Actions.
author: colindembovsky
ms.date: 03/04/2021
---
# Deploy an app to App Service - DevOps with .NET and GitHub Actions

[GitHub Actions](https://github.com/features/actions) allow you to automate workflows in response to events that are triggered in GitHub. Common workflows include Continuous Integration (CI) workflows, but Actions can be used to automate other processes such as sending out welcome emails when people join a repo.
colindembovsky marked this conversation as resolved.
Show resolved Hide resolved

To explore how you can move code to cloud, you are going to build a GitHub Actions workflow file for the Simple Feed Reader application you have already deployed to Azure App Services.
colindembovsky marked this conversation as resolved.
Show resolved Hide resolved

In this article you will:
colindembovsky marked this conversation as resolved.
Show resolved Hide resolved
> [!div class="checklist"]

> * learn the basic structure of a GitHub Action workflow YAML file
colindembovsky marked this conversation as resolved.
Show resolved Hide resolved
> * Use a template to create a basic build workflow that build the .NET application and executes unit tests
colindembovsky marked this conversation as resolved.
Show resolved Hide resolved
> * Publish the compiled application so that it is ready for deployment
colindembovsky marked this conversation as resolved.
Show resolved Hide resolved

## Workflow Structure
colindembovsky marked this conversation as resolved.
Show resolved Hide resolved

Workflows are captured in YML files and all have:
colindembovsky marked this conversation as resolved.
Show resolved Hide resolved

- a `name`
- a trigger, defined by an `on` section
- one or more `job` sections, composed of one or more `steps`
colindembovsky marked this conversation as resolved.
Show resolved Hide resolved
- optional attributes such as `environment` variables

Jobs are run on _runners_. You can use _hosted runners_ which are spun up by GitHub for the duration of the workflow and then thrown away. Hosted runners are great because you don't have to maintain your own build infrastructure. For workflows that require specific build environment, or for running workflows on a private network, you can also use _private_ runners. To create a private runner, you can install the runner on any machine that supports .NET.
colindembovsky marked this conversation as resolved.
Show resolved Hide resolved

Each `job` will specify what runner GitHub should use to execute the `steps`. You can also specify dependencies between jobs using the `needs` attribute. Deployments jobs can also specify an `environment` to target.
colindembovsky marked this conversation as resolved.
Show resolved Hide resolved

`Steps` can be as simple as inline commands or they can be Actions. Most CI workflows will have a combination of `run` steps (for executing scripts) and Actions. Actions are pulled into the workflow by referencing the Action repo (and optionally a tag or commit hash for specific verions) and specifying any parameters using the `with` keyword.
colindembovsky marked this conversation as resolved.
Show resolved Hide resolved

> [!TIP]
> You can read more about GitHub Actions YAML syntax [here](https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions).
colindembovsky marked this conversation as resolved.
Show resolved Hide resolved

## Create a Basic Build Workflow
colindembovsky marked this conversation as resolved.
Show resolved Hide resolved

One important principle of effective DevOps is to build once, deploy many times. You are going to start by creating a workflow that will build a simple .NET application. In the next step, you will publish the output so that you are ready to deploy.
colindembovsky marked this conversation as resolved.
Show resolved Hide resolved

1. Navigate to your GitHub repo and click on Actions tab.
colindembovsky marked this conversation as resolved.
Show resolved Hide resolved
1. GitHub detects that there is .NET code in the repo and suggests a .NET workflow template. Click `Set up this workflow` to create a new YAML workflow file:
colindembovsky marked this conversation as resolved.
Show resolved Hide resolved

![Creating a new workflow](./media/actions/build/new-action.jpg)
**Figure 1** Creating a new workflow.
colindembovsky marked this conversation as resolved.
Show resolved Hide resolved

1. Commit the file onto the main branch. Since you have defined a trigger condition for _commits to main_, this commit should trigger the workflow to run.
colindembovsky marked this conversation as resolved.
Show resolved Hide resolved

![Commit the YAML file](./media/actions/build/commit-workflow.jpg)
**Figure 2** Commit the YAML file.
colindembovsky marked this conversation as resolved.
Show resolved Hide resolved

1. Click on the Actions tab again. You should see a running workflow. Once the workflow has completed, you should see a successful run.
colindembovsky marked this conversation as resolved.
Show resolved Hide resolved

![Successful build view](./media/actions/build/build-action-success.jpg)
**Figure 3** Successful build view.
colindembovsky marked this conversation as resolved.
Show resolved Hide resolved

1. Opening the logs, you can see that the .NET build succeeded and the tests ran and passed.

![Checking the logs](./media/actions/build/build-action-success-logs.jpg)
**Figure 4** Checking the logs.
colindembovsky marked this conversation as resolved.
Show resolved Hide resolved

> [!NOTE]
> If any of the tests fail, the workflow will fail.

## Disecting the Workflow File
colindembovsky marked this conversation as resolved.
Show resolved Hide resolved

Let's have a look at the workflow YAML file that you have so far:
colindembovsky marked this conversation as resolved.
Show resolved Hide resolved

```yml
name: .NET

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Setup .NET
uses: actions/setup-dotnet@v1
with:
dotnet-version: 5.0.x
- name: Restore dependencies
run: dotnet restore
- name: Build
run: dotnet build --no-restore
- name: Test
run: dotnet test --no-build --verbosity normal
```

You can see the following:

1. There is a `name` that names the workflow.
1. The `on` object specifies when this workflow should run. This workflow has two events that trigger it: `push` to `main` and `pull_request` to `main`. Any time anyone commits to main or creates a Pull Request (PR) to main, this workflow will execute.
colindembovsky marked this conversation as resolved.
Show resolved Hide resolved
1. There is a single `job` called `build`. This build should run on a hosted agent - `ubuntu_latest` specifies the most recent ubuntu hosted agent.
colindembovsky marked this conversation as resolved.
Show resolved Hide resolved
1. There are five steps:
1. `acions/checkout@2` is an action that checks out the code in the repo onto the runner.
colindembovsky marked this conversation as resolved.
Show resolved Hide resolved
1. `actions/setup-dotnet@v1` is an action that sets up the .NET CLI. This step also specifies a `name` attribute for the logs as well as the `dotnet-version` parameter within the `with` object.
colindembovsky marked this conversation as resolved.
Show resolved Hide resolved
1. Three `run` steps that execute `dotnet restore`, `dotnet build` and `dotnet test`. `name` attributes are also specified for thest `run` steps to make the logs look pretty.
colindembovsky marked this conversation as resolved.
Show resolved Hide resolved

## Publishing the output
colindembovsky marked this conversation as resolved.
Show resolved Hide resolved

Now that you have successfully built and tested the code, you will want to add steps that publish the output so that you can deploy the web app.
colindembovsky marked this conversation as resolved.
Show resolved Hide resolved

1. Navigate to the `.github/workflows/dotnet.yml` file and click the pencil icon to edit it.
colindembovsky marked this conversation as resolved.
Show resolved Hide resolved

![Edit the YML file](./media/actions/build/click-edit.jpg)
**Figure 5** Edit the YML file.
colindembovsky marked this conversation as resolved.
Show resolved Hide resolved

1. Add the following step at the bottom of the file, below the `Test` step, to run the `dotnet publish` command to publish the website:
colindembovsky marked this conversation as resolved.
Show resolved Hide resolved

```yml
colindembovsky marked this conversation as resolved.
Show resolved Hide resolved
- name: Test
run: dotnet test --no-build --verbosity normal # <-- this is the current bottom line

- name: Publish
run: dotnet publish SimpleFeedReader/SimpleFeedReader.csproj -c Release -o website
```

1. This publishes the web app to a folder on the hosted agent. We now want to _upload_ the site as a build artifact that we can deploy to Azure. To do this, we are going to use an existing action.
colindembovsky marked this conversation as resolved.
Show resolved Hide resolved
1. On the list of Actions in the Actions Helper pane on the right, search for `artifact` and click on the `Upload a Build Artifact (By actions)` action.
colindembovsky marked this conversation as resolved.
Show resolved Hide resolved

![Accessing the Actions helper](./media/actions/build/search-upload-artifact.jpg)
**Figure 6** Accessing the snippet helper.
colindembovsky marked this conversation as resolved.
Show resolved Hide resolved

1. Edit the version to `v2.2.2` to display a sample snippet. Click the clipboard icon to copy the snippet and paste it into the workflow below the publish step.
colindembovsky marked this conversation as resolved.
Show resolved Hide resolved

![Copying a snippet](./media/actions/build/copy-snippet.jpg)
**Figure 8** Copying a snippet.
colindembovsky marked this conversation as resolved.
Show resolved Hide resolved

1. Edit the YAML for this step to look as follows:

```yml
colindembovsky marked this conversation as resolved.
Show resolved Hide resolved
- name: Upload a Build Artifact
uses: actions/upload-artifact@v2.2.2
with:
name: website
path: SimpleFeedReader/website/**
if-no-files-found: error
```

1. Commit the file.
1. Once the workflow completes, you will be able to see the artifact from the Home tab:
colindembovsky marked this conversation as resolved.
Show resolved Hide resolved

![Viewing artifacts in the summary page](./media/actions/build/view-uploaded-artifact.jpg)
**Figure 8** Viewing artifacts in the summary page.
colindembovsky marked this conversation as resolved.
Show resolved Hide resolved

### Final Workflow File
colindembovsky marked this conversation as resolved.
Show resolved Hide resolved

The final workflow file should look something like this:

```yml
name: .NET

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Setup .NET
uses: actions/setup-dotnet@v1
with:
dotnet-version: 5.0.x
- name: Restore dependencies
run: dotnet restore
- name: Build
run: dotnet build --no-restore
- name: Test
run: dotnet test --no-build --verbosity normal
- name: Publish
run: dotnet publish SimpleFeedReader/SimpleFeedReader.csproj -c Release -o website
- name: Upload a Build Artifact
uses: actions/upload-artifact@v2.2.2
with:
name: website
path: SimpleFeedReader/website/**
if-no-files-found: error
```
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.