- 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_CREDENTIALS
as 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 -Verbose
The 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.yaml
file 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 yourself
button 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_NAME
variable 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.yaml
and 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.txt
file. 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_WebApp
workflow 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 WebApp
task'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/public
directory and open theindex.html
file. -
Find the Octodex image element (
img
tag) identified with theid
attributeoctodex
.
-
Go to https://octodex.github.com/ and copy the address of an Octodex that you like.
-
Update the
alt
andsrc
attribute of the octodeximg
tag 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.net
website 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.js
file.
- Change the
Hello, World!
text in the service/path
response. 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 test
task.
- Notice that the
API test
unit test failed and theDeploy to Azure WebApp
was not skipped. This is the default behaviour.
- Notice that the
test pass message
was skipped, but thetest fail message
task did execute. This is because thetest fail message
has anif
conditional 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.js
file 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-3
Links 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