M4 GitHubActions
M4 GitHubActions
md 4/17/2023
Lab Guides
Conditions and Terms of Use
Microsoft Confidential
This training package is proprietary and confidential, and is intended only for uses described in the training
materials. Content and software is provided to you under a Non-Disclosure Agreement and cannot be
distributed. Copying or disclosing all or any portion of the content and/or software included in such packages
is strictly prohibited.
The contents of this package are for informational and training purposes only and are provided as is without
warranty of any kind, whether express or implied, including but not limited to the implied warranties of
merchantability, fitness for a particular purpose, and non-infringement.
Training package content, including URLs and other Internet Web site references, is subject to change without
notice. Because Microsoft must respond to changing market conditions, the content should not be
interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any
information presented after the date of publication. Unless otherwise noted, the companies, organizations,
products, domain names, e-mail addresses, logos, people, places, and events depicted herein are fictitious,
and no association with any real company, organization, product, domain name, e-mail address, logo, person,
place, or event is intended or should be inferred.
Microsoft may have patents, patent applications, trademarks, copyrights, or other intellectual property rights
covering subject matter in this document. Except as expressly provided in written license agreement from
Microsoft, the furnishing of this document does not give you any license to these patents, trademarks,
copyrights, or other intellectual property.
Complying with all applicable copyright laws is the responsibility of the user. Without limiting the rights under
copyright, no part of this document may be reproduced, stored in or introduced into a retrieval system, or
transmitted in any form or by any means (electronic, mechanical, photocopying, recording, or otherwise), or
for any purpose, without the express written permission of Microsoft Corporation.
http://www.microsoft.com/en-us/legal/intellectualproperty/Permissions/default.aspx
DirectX, Hyper-V, Internet Explorer, Microsoft, Outlook, OneDrive, SQL Server, Windows, Microsoft Azure,
Windows PowerShell, Windows Server, Windows Vista, and Zune are either registered trademarks or
trademarks of Microsoft Corporation in the United States and/or other countries. Other Microsoft products
mentioned herein may be either registered trademarks or trademarks of Microsoft Corporation in the United
States and/or other countries. All other trademarks are property of their respective owners.
1 / 21
WorkshopPLUS - Essentials on Azure DevOps Services and GitHub_M4_GitHubActions.md 4/17/2023
Introduction
In this lab, you will create a workflow using YAML and GitHub Actions to build your code and deploy a web
app to Azure. You'll learn:
Prerequisites
None
30 minutes
2 / 21
WorkshopPLUS - Essentials on Azure DevOps Services and GitHub_M4_GitHubActions.md 4/17/2023
Module 4 (Alternative): GitHub Actions, Lab 1: GitHub Actions, Exercise 1: Create a basic build and deploy
workflow
In this exercise you will setup a GitHub account and create a basic build and deploy workflow for a sample
.NET Core application to deploy it in Azure App Service.
Tasks
3 / 21
WorkshopPLUS - Essentials on Azure DevOps Services and GitHub_M4_GitHubActions.md 4/17/2023
Module 4 (Alternative): GitHub Actions, Lab 1: GitHub Actions, Exercise 1: Create a basic build and deploy
workflow
1. In the browser, navigate to https://github.com and click on sign in from the top-right.
If you haven't created a GitHub account previously, click on Create an account and enter
following information:
4 / 21
WorkshopPLUS - Essentials on Azure DevOps Services and GitHub_M4_GitHubActions.md 4/17/2023
On the screen that asks you to enter the code sent to your email ID, click update your email address.
Enter the code and setup your GitHub account.
4. In the top-right corner of the page, click Fork to fork the repository to your student account.
5. Once the fork is complete, you will see this repository in [email protected]
5 / 21
WorkshopPLUS - Essentials on Azure DevOps Services and GitHub_M4_GitHubActions.md 4/17/2023
6. Check if you have Actions tab. If not, click on Settings, navigate to Actions and select Allow all
actons. Click Save.
GitHub Actions is used CI/CD in GitHub and we need to enable this functionality for this lab.
6 / 21
WorkshopPLUS - Essentials on Azure DevOps Services and GitHub_M4_GitHubActions.md 4/17/2023
Module 4 (Alternative): GitHub Actions, Lab 1: GitHub Actions, Exercise 1: Create a basic build and deploy
workflow
2. Notice that there is not workflow configured right now. However, based on the repository, GitHub is
recommending a few workflows to choose from.
3. We will select .NET workflow as that fits best for our .NET Core project. Click on Configure for the .NET
workflow.
4. You should have a dotnet.yml file that contains the workflow we selected. This file is located inside your
repository under .github/workflows. Review the file.
7 / 21
WorkshopPLUS - Essentials on Azure DevOps Services and GitHub_M4_GitHubActions.md 4/17/2023
The on: attribute specifies that when a push or pull request is done to the repo on the master
branch the workflow should be triggered
The runs-on: attribute specifies that the hosted runner the build should use is ubuntu-latest
The first is a GitHub Action actions/checkout@v2 that gets the code from your repo to the
VM runner.
actions/setup-dotnet@v1 action sets up a .NET CLI environment for use. It also specifies
the version of dotnet by SDK version to use.
Three run steps are included which run dotnet restore, dotnet build, and dotnet test on the
.NET CLI with the appropriate arguments for each.
name attributes are included with the run steps to assist with logging during the run.
5. Commit the file to the master branch by clicking on Start commit and Commit new file. Since a on:
[push] trigger is defined for this branch, the commit should trigger the workflow to run.
8 / 21
WorkshopPLUS - Essentials on Azure DevOps Services and GitHub_M4_GitHubActions.md 4/17/2023
6. Navigate to the Actions section to see the status of the workflow run. Select the first run which should
be named by the commit message given in the previous step.
7. Click on the build job from the Summary page to view the logs. You can see that the build and test
steps were successful.
9 / 21
WorkshopPLUS - Essentials on Azure DevOps Services and GitHub_M4_GitHubActions.md 4/17/2023
10 / 21
WorkshopPLUS - Essentials on Azure DevOps Services and GitHub_M4_GitHubActions.md 4/17/2023
Module 4 (Alternative): GitHub Actions, Lab 1: GitHub Actions, Exercise 1: Create a basic build and deploy
workflow
1. Now that the code has been built and tested, the final step of the build job is to upload the artifacts to
be used in the deployment job.
4. You will add the following Publish step below the Test step to publish the web app via dotnet publish
CLI command.
- name: Publish
run: dotnet publish -c Release -o website
This publishes the web app to a folder website on the runner. Next, you will add a step to
Upload the app as an artifact that will be used for deployment.
5. In the Actions Helper pane on the right, search for upload. Select the Upload a Build Artifact action
11 / 21
WorkshopPLUS - Essentials on Azure DevOps Services and GitHub_M4_GitHubActions.md 4/17/2023
6. Select the clipboard icon to copy the yaml and paste it into the workflow yaml file below the Publish
step.
12 / 21
WorkshopPLUS - Essentials on Azure DevOps Services and GitHub_M4_GitHubActions.md 4/17/2023
The name attribute after with: specifies the name for the artifact. The default name is artifact.
The path attribute specifies the path/files to upload
The if-no-files-found: attributes tells Actions what behavior to take if the path or files are not
found. Default behavior is warn.
8. Commit your changes and wait for the workflow to finish. Once complete you will see the artifact
produced on the Summary page of the workflow run.
13 / 21
WorkshopPLUS - Essentials on Azure DevOps Services and GitHub_M4_GitHubActions.md 4/17/2023
Module 4 (Alternative): GitHub Actions, Lab 1: GitHub Actions, Exercise 1: Create a basic build and deploy
workflow
We will create a web app (App Service) in Azure where we will deploy the sample application built in
the previous tasks.
2. Click on Create a resource and select Web App from the Popular products list.
14 / 21
WorkshopPLUS - Essentials on Azure DevOps Services and GitHub_M4_GitHubActions.md 4/17/2023
15 / 21
WorkshopPLUS - Essentials on Azure DevOps Services and GitHub_M4_GitHubActions.md 4/17/2023
Module 4 (Alternative): GitHub Actions, Lab 1: GitHub Actions, Exercise 1: Create a basic build and deploy
workflow
Just as in Azure DevOps, we need a way for GitHub to authenticate into Azure App Service for
deployment. The recommended way to authenticate with Azure App Services for GitHub Actions is with
a publish profile. You can also authenticate with a service principal but the process requires more steps.
1. Navigate back to the Azure Portal and switch to the Azure App Service. Click on Get publish profile.
4. Scroll down and click on Secrets and click on New repository secret.
16 / 21
WorkshopPLUS - Essentials on Azure DevOps Services and GitHub_M4_GitHubActions.md 4/17/2023
5. Add secret Name as AZURE_WEBAPP_PUBLISH_PROFILE. Enter the copied profile as the Value and
click Add secret.
17 / 21
WorkshopPLUS - Essentials on Azure DevOps Services and GitHub_M4_GitHubActions.md 4/17/2023
Module 4 (Alternative): GitHub Actions, Lab 1: GitHub Actions, Exercise 1: Create a basic build and deploy
workflow
1. Navigate to Code in GitHub and to your workflow file in GitHub repo: .github/workflows/{filename}.yml
and select the pencil icon to edit it.
deploy:
runs-on: ubuntu-latest
needs: build
steps:
- name: Download a Build Artifact
uses: actions/[email protected]
with:
name: webapp
path: webapp
- name: Deploy web app
uses: azure/webapps-deploy@v2
with:
app-name: [email protected]
publish-profile: ${{ secrets.AZURE_WEBAPP_PUBLISH_PROFILE }}
package: webapp
18 / 21
WorkshopPLUS - Essentials on Azure DevOps Services and GitHub_M4_GitHubActions.md 4/17/2023
3. Commit the file with the changes. Switch to Actions and you will see two jobs.
4. Once the run completes, you will now see TWO successful jobs. You can click on each one to see more
detailed logs of the jobs.
5. Switch the Azure Portal and to the GitHubApp web app. Click on Browse. This should take you to a
web page for the application and it should now show your sample application.
19 / 21
WorkshopPLUS - Essentials on Azure DevOps Services and GitHub_M4_GitHubActions.md 4/17/2023
7. Scroll to the bottom and Commit changes and wait for the workflow to complete in GitHub Actions.
8. Refresh your web app URL and you should see the change. Your web app is successfully building and
deploying from GitHub Actions.
20 / 21
WorkshopPLUS - Essentials on Azure DevOps Services and GitHub_M4_GitHubActions.md 4/17/2023
21 / 21