0% found this document useful (0 votes)
29 views21 pages

M4 GitHubActions

m4 github

Uploaded by

b.n.reddy.0441
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views21 pages

M4 GitHubActions

m4 github

Uploaded by

b.n.reddy.0441
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

WorkshopPLUS - Essentials on Azure DevOps Services and GitHub_M4_GitHubActions.

md 4/17/2023

WorkshopPLUS - Essentials on Azure DevOps Services and GitHub

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.

Copyright and Trademarks

© 2017 Microsoft Corporation. All rights reserved.

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.

For more information, see Use of Microsoft Copyrighted Content at

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

Module 4 (Alternative): GitHub Actions

Lab 1: GitHub Actions


This module is an alternative to the previous Azure Pipelines module. Please proceed with this lab
only if you are using / interested in GitHub Actions instead of Azure Pipelines.

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:

The basic structure of GitHub Action YAML workflows


How to configure a workflow to build and deploy a web app

Exercise 1: Create a basic build and deploy workflow

Prerequisites

None

Estimated Time to Complete This Lab

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

Exercise 1: Create a basic build and deploy workflow


Objectives

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

Task 1: Set up GitHub account and repository


Task 2: Create a basic build workflow
Task 3: Edit the build workflow
Task 4: Create Azure App Service
Task 5: Generate deployment credentials
Task 6: Deploy to Azure App Service

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

Task 1: Set up GitHub account and repository

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:

Email: Enter your email


Password: create a password
Username: create a username
Enter n for product updates and click Continue.
Complete Captcha and click Create account.

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.

2. Enter your credentials to sign into GitHub.com.

3. In the same browser instance, open a new tab and navigate to


https://github.com/MicrosoftDocs/pipelines-dotnet-core/ to go to the MicrosoftDocs/pipelines-
dotnet-core repository. We will use this repository for this lab.

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

Task 2: Create a basic build workflow

1. Staying in the pipelines-dotnet-core repository in GitHub, click on Actions tab.

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 name: attribute names the workflow

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 workflow currently has one job called build

The runs-on: attribute specifies that the hosted runner the build should use is ubuntu-latest

There are five steps in the job.

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

Task 3: Edit the build 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.

2. Navigate to your workflow file under Code: .github/workflows/{filename}.yml.

3. Select the pencil icon to edit the file.

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.

Tab can be used to ensure proper indentation in the yaml file

7. Edit the step as below:

- name: Upload artifact for deployment job


uses: actions/[email protected]
with:
name: webapp
path: website/**
if-no-files-found: warn

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

Task 4: Create Azure App Service

We will create a web app (App Service) in Azure where we will deploy the sample application built in
the previous tasks.

1. Navigate to https://portal.azure.com. If asked to sign in with your credentials.

2. Click on Create a resource and select Web App from the Popular products list.

3. Enter following information to create a web app:

Subscription: Use your subscription


Resource Group: yamlrg
Name: github-yaml-[YourInitials]
Publish: Code
Runtime stack: .NET 5
Operating System: Windows
Regiod: Select the closest to you
Select the default Windows Plan and Sku and size.
Click Review + create and click Create.

4. Click on Go to resource to navigate to the App Service.

14 / 21
WorkshopPLUS - Essentials on Azure DevOps Services and GitHub_M4_GitHubActions.md 4/17/2023

5. Click on Browse to see the empty web app.

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

Task 5: Generate deployment credentials

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.

2. Open the downloaded profile in notepad and copy the profile.

3. Switch to GitHub and navigate to the pipelines-dotnet-core repository. Click on Settings.

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

Task 6: Deploy to Azure App Service

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.

2. Add the following below build job.

Tab can be used to ensure proper indentation in the yaml file

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

The yaml above does the following:

Creates a new job called deploy


Specifies the job should be run on a ubuntu-latest hosted runner
Creates a dependency on the build job to complete successfully before the deploy job can begin
The first step to execute is the actions/[email protected] which downloads the artifacts
from the build to the path provided
The deployment is done via the azure/webapps-deploy@v2 action which takes in the app-name,
publish profile and the package location.

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

6. Navigate back to GitHub and go to pipeline-dotnet-core/Views/Shared/_Layout.cshtml. Click on pencil to


edit the file and modify line # 24 to add v2.0 at the end of pipelines_dotnet_core.

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

You might also like