- Create Azure Web App
- Configure GitHub Actions
- Run GitHub Actions
- Continuous Deployment of Changes
- Unit Testing
The third lab will deploy a NodeJS Web App using GitHub Actions.
Note: Lab 3 uses the same secret
AZURE_CREDENTIALSas in Lab 1
Create the Azure Web App that the pipeline will deploy to. Open Azure Cloud Shell and run the following PowerShell cmdlets:
- Create the Resource Group
rg-lab-3
New-AzResourceGroup -Name 'rg-lab-3' -Location 'eastus2'- Create the Azure Web App
New-AzResourceGroupDeployment -ResourceGroupName 'rg-lab-3' -TemplateUri https://raw.githubusercontent.com/softchoice-corp/DevOpsBootcamp/master/lab_3/webapps.deploy.json -VerboseThe ARM template deployment will create a unique name for the Web App and return it on the console under Outputs. Make a note of the uniqueWebAppName value, we will use it later to configure our GitHub Actions pipeline.
-
Browse to the
workflows-templates/lab_3_webapps.yamlfile and copy all of the text. -
Navigate to Actions and click New Workflow. If prompted to start with a sample workflow click the
Set up a workflow yourselfbutton in the top right. -
Replace all of the sample workflow code in the editor by pasting all the code you copied from
workflows-templates/lab_3_webapps.yaml. -
Modify the
AZURE_WEBAPP_NAMEvariable to use the name of the Web App you created in the previous step.
env:
AZURE_WEBAPP_NAME: azure-webapp-unique-name
AZURE_WEBAPP_PACKAGE_PATH: ./lab_3/app-
GitHub Actions files must be saved in a directory in your repo named
.github/workflows/. The directory structure.github/workflows/should already exist in the path, name your workflow filelab_3_webapps.yamland clickStart Commit. -
Add a short commit message and click
Commit new file.
The workflow we just created is triggered by changes made to the files in the lab_3/ directory. Let's make a change here to kick off the workflow. The readme.txt can be modified by simply adding a new line or some text. The act of committing this change to the master branch will instruct GitHub Actions to kick off our workflow.
-
Navigate to Code, and browse to the
lab_3/readme.txtfile. Click the pencil icon to edit the file, and add a new line. Provide a commit message and commit your change. -
Navigate to Actions and you should see your
Lab_3_WebAppworkflow executing. -
When the Workflow successfully finishes, Open up a browser and go to
<Azure Web App name>.azurewebsites.net. Replace<Azure Web App name>with the unique name.
Note: It is possible to find the URL in the Workflow's
Deploy to Azure WebApptask's output
GitHub provides an action on GitHub marketplace to simplify deployments to Azure Web App application called
webapps-deploy. Go to https://github.com/marketplace/actions/azure-webapp for more information.
-
Navigate to Code, open the
lab_3/app/publicdirectory and open theindex.htmlfile. -
Find the Octodex image element (
imgtag) identified with theidattributeoctodex.
-
Go to https://octodex.github.com/ and copy the address of an Octodex that you like.
-
Update the
altandsrcattribute of the octodeximgtag with description and copied address, respectively.
- Enter a commit message and click
Commit changes.
- Navigate to Actions and observe the workflow.
- When the workflow finishes executing, open your browser and refresh or go to the
<Azure Web App name>.azurewebsites.netwebsite to observe the change in the application.
We have deployed our application successfully due to the defined unit tests passing successfully. We will now intentionally introduce an error into the application.
- Navigate to Code, open the
lab_3/app/directory and open theindex.jsfile.
- Change the
Hello, World!text in the service/pathresponse. It can be any misspelling or a completely different text, as long as it is different.
- Enter a commit message and click
Commit changes.
- Navigate to Actions to observe the Workflow. Which will fail.
- Click on the Workflow to take a look at the more granular tasks the Workflow is running and expand the
npm testtask.
- Notice that the
API testunit test failed and theDeploy to Azure WebAppwas skipped. This is the default behaviour when an error occurs and the workflow is in a failed state.
- Notice that the
test pass messagewas skipped, but thetest fail messagetask did execute. This is because thetest fail messagehas anifconditional execution on Workflow failure.
- name: test fail message
if: failure()
run: |
echo "npm tests failed! please check your code"This demonstrates some of the flexibility of GitHub Action Workflows. Go to https://help.github.com/en/actions/reference/workflow-syntax-for-github-actions for more information on other constructs and Workflow syntax.
- Fix the Workflow by correcting the
lab_3/app/index.jsfile with the proper text and commit the change.
To mimimize billing usage in your subscription we can remove all of the resources we deployed with GitHub Actions by deleting the Resource Group they are held in. From Azure Cloud Shell run the following command:
az group delete --name rg-lab-3Links to more learning:
- Azure App Service: https://docs.microsoft.com/en-us/azure/app-service/overview
- Node.js in Azure: https://docs.microsoft.com/en-us/azure/app-service/app-service-web-get-started-nodejs
- Unit Testing: https://en.wikipedia.org/wiki/Unit_testing















