Azure applications – GitHub Actions to deploy to Azure

Continuous Delivery (CD) is the practice of using automation to build, test, configure and deploy from the build environment all the way to the final production environment.

Suppose that your development team is working on the company’s product support website. You’ve already set up continuous integration (CI) by using GitHub Actions and workflows. Now you need to implement CD. Your CI workflow saves a container image. Your CD workflow will deploy this container to your staging and production environments. You’ve talked with IT about how to create and tear down these environments as needed and you’ve all decided to use GitHub Actions and workflows to support infrastructure as code.

In this module, you learn how to use GitHub Actions and workflows to implement a CD solution that deploys a container-based web app to Microsoft Azure Web Apps. You also automate the creation and tear-down of the deployment environments by using a workflow.

Here, we discuss how to use GitHub actions to deploy a container-based web app to Microsoft Azure Web Apps. First, we discuss some options for triggering a workflow. Next, we show you how to work with conditionals in the workflow. Finally, we talk about how to create and delete Azure resources by using GitHub Actions.

Options for triggering a CD workflow

There are several options for starting a CD workflow. Let’s talk about a few of them.

In the previous module on CI with GitHub Actions, you learned how to trigger a workflow from a push to the GitHub repository. However, for CD, you may want to trigger a deployment workflow on some other event.

One option is to trigger the workflow with ChatOps. ChatOps uses chat clients, chatbots and real-time communication tools to execute tasks. For example, you might leave a specific comment in a pull request that can kick off a bot. That bot might comment back with some statistics or run a workflow.

Another option, and the one we use in our example, is to use labels in your pull request. Different labels can start different workflows. For example, add a stage label to begin a deployment workflow to your staging environment, or add a spin up environment label to run the workflow that creates the Microsoft Azure resources you will deploy to best web hosting. To use labels, your workflow will look like this:

on:
  pull_request:
    types: [labeled]

Control execution with a job conditional

Often, you only want to run a workflow if some condition is true.

GitHub workflows provide the if conditional for this scenario. The conditional uses an expression that will be evaluated at run time. For example, we want to run this workflow if a stage label is added to the pull request.

Store credentials with GitHub Secrets

Of course, you never want to expose sensitive information in the workflow file. GitHub Secrets is a secure place to store sensitive information that your workflow will need. Here’s an example.

In order to deploy to an Azure resource, the GitHub Action must have permission to access the resource. You don’t want to store your Azure credentials in plain sight in the workflow file. Instead, you store your credentials in GitHub Secrets.

To store information in GitHub Secrets, you create a secret on the portal.

secretsThen, you use the name of the secret you created in your workflow wherever you need that information. For example, below we use the Azure credential that was stored in GitHub Secrets in the creds: attribute of an Azure login action. (We talk about that action in the next section.)

 

Create and delete Azure resources by using GitHub Actions

Because CD is an automated process, you’ve already decided to use infrastructure as code to create and take down the environments you deploy to web hosting . GitHub Actions can automate these tasks on Azure and you can include these actions in your workflow. (Remember that it’s important to tear down resources that you’re no longer using as soon as possible to avoid unnecessary charges.)

One option is to create a new workflow with two jobs, one that spins up resources and one that deletes them. Then, use a conditional to run only the job you want.

Notice that you use GitHub actions to check out the repository and to sign in to Azure. After that, you create the resources you need and deploy the container by using the Azure CLI.

What are pull requests?

Here, we discuss how pull requests can help you request, review, and incorporate changes across your repository.

The GitHub flow

In addition to providing a platform for collaborative software development, GitHub also offers a prescribed workflow designed to optimize use of its various features. While this unit focuses on pull requests, it is recommended that you first review Understanding the GitHub flow .

Working on branches

Developers working on independent branches has become a ubiquitous concept in modern software development. By having their own branch, a developer can make any changes they need to without worrying about how those changes will impact other developers working on their own branches.

You have to merge eventually

While working on a branch is great for individual developer productivity, it opens a new challenge. At some point, each developer’s branch will need to be merged into a common branch, like master. As projects scale, there can be many merges that need to happen, and it becomes increasingly important to track and review each . This is where pull requests come in.

What is a pull request?

A pull request is a mechanism used to communicate that changes from one branch are ready to be merged into another. They offer an opportunity for stakeholders to review and discuss the proposed changes to ensure that the code quality in the base branch is kept as high as possible.

Author responsibilities

The author of a pull request is the person who created the pull request. They’re hoping to ultimately have the changes from the compare branch merged into the base branch. They’re generally responsible for answering questions about the changes and applying feedback.

Before creating a pull request, the author is responsible for ensuring the merge can succeed. It’s best practice to often pull down changes from the remote to your local copy to confirm that the code builds with the latest base changes.

 

Reviewer responsibilities

Reviewers may be assigned to review the pull request based on their expertise with or responsibility for the code in question. For example, there may be a database guru who reviews any database code. Or there might be a lead developer who reviews any code before it gets merged into master anywhere in the project.reviewing pull request

GitHub offers a visual experience for reviewing code that allows reviewers to see code changes in a diff view where changes are highlighted. They can then leave comments inline, suggest changes, or approve. Reviewer feedback is kept as part of the repository history so that changes and decisions can be easily traced within their original context.

As the name implies, reviewers are responsible for reviewing changes. When feedback is minor, such as typos, reviewers may leave comments or edit the files directly to incorporate fixes. To recommend more substantive changes, a pull request should be opened against the author’s branch.

Reviewers may be automatically assigned to a pull request by using a CODEOWNERS file . This file uses the same path syntax as .gitignore to map parts of the codebase to users or teams responsible for reviewing their changes. Learn how to make these reviews mandatory .

Learn more about Reviewing changes in pull requests .

Assignee responsibilities

While not mandatory, pull requests may be assigned to a specific user. That person takes responsibility for determining if and when the pull request is merged. This may require them to corral stakeholders into agreeing whether the merge should be approved.

Once a pull request has been merged, it’s recommended that you delete the branch it was based on. Allowing the branch to linger can result in headaches down the road. Remember that all branch history will have been merged into the base branch, so its commits can always be accessed later on.

What is GitHub Pages?

GitHub Pages are free static websites hosted directly from your GitHub repository. Using standard technologies like YAML and Markdown, anyone can build and maintain a site in minutes.

Suppose you want to set up a basic web site to share information about the project you’re working on. It might be a personal site, like as resume or portfolio. Or it might be a professional site, like a user guide or developer reference. With GitHub Pages, you can spin the site up in minutes and enable anyone with a basic understanding of Markdown to contribute to it. You also get all the benefits of GitHub for source control, including branches and pull requests.

In this module, you learn how to set up and use GitHub Pages to host both personal and professional websites.

Learning objectives

In this module, you will:

  • Enable GitHub Pages
  • Choose a theme with Jekyll
  • Use YAML front matter
  • Customize your site
  • Create and edit blog posts

Prerequisites

  • A GitHub account
  • The ability to navigate and edit files in GitHub

 

What is GitHub Pages?

Here, we discuss the process of creating and maintaining a GitHub Pages web site.

GitHub Pages are static sites hosted directly from your GitHub repository. But they’re more than just a collection of static files. By making use of site generation technologies like Jekyll and Liquid, developers define dynamic templates that are processed into complete static web sites. Every time a change is committed to the source branch associated with the site, it is re-generated using the latest updates and automatically published to the target URL.

Learn more about Publishing sources for GitHub Pages sites

 

How to enabling GitHub Pages

The first step in using GitHub Pages is to enable it from your repository’s Settings tab. You can opt to use the master branch, or specify the docs folder within it. If you ever want to disable GitHub Pages, you can do so here.

enabling-github-pages

Choosing a theme with Jekyll

Jekyll is the static site generator used by GitHub to build your website from the contents of your repository. In addition to providing great content convenience, it also conforms to a standard design convention. This style standardization allows for swappable themes, which you can select from the GitHub Pages configuration.

choosing-jekyll-theme

There are a variety of themes provided by GitHub. There is also an array of commercial and open source themes available from the Jekyll community.

jekyll-themes

Learn more about Jekyll Themes .

Using YAML front matter

The term front matter refers to YAML metadata that prepends the content of a file. For Jekyll, this includes generator instructions to indicate the layout style of a Markdown page (post, page, and so on). It may also include page metadata, such as the document title, or page content variables, such as a blog post’s author.

Below is a simple example that would use the post layout. This assumes there is a _layouts/post.html file that defines the container HTML. Other layout options may be offered by adding their respective HTML files in the _layouts folder.

Learn more about Front Matter .

Customizing your site

Once your site is up and running, you can customize details about your site via _config.yml. This file includes virtually all site-wide configuration options, including site metadata, navigation menus, theme colors, compiler options, and more.

Learn more about _config.yml Configuration .

Creating and editing content

Creating and editing pages on your site follows the standard GitHub experience. The files you use for your GitHub Pages web site enjoy all of the same benefits as other files in your GitHub repository, so you can edit them with any tool, create and merge branches, and link with issues or pull requests.

In addition to Markdown and HTML, Jekyll supports the Liquid template language syntax. Liquid provides the ability for users to dynamically insert variables and basic logic flow constructs into their content files. When compiled, the final product is standard HTML.

The example below shows a combination of for looping and variable insertion.

<ul>
  {% for post in site.posts %}
    <li>
      <h2><a href="{{ post.url }}">{{ post.title }}</a></h2>
      {{ post.excerpt }}
    </li>
  {% endfor %}
</ul>

Learn more about Liquid template language .

Working with blog posts

Despite not having a database to work with, Jekyll still supports the concept of blogging using a specific convention: _posts/2020-06-25-blog-post-name.md. As you can likely infer, all blog posts are stored in the _posts folder and use the date and name convention as shown. During compilation, Jekyll processes the files in this folder to produce a list of HTML blog posts.

The example below illustrates the structure of a simple blog post –  best web hosting. It includes metadata for subtitle, tags, and comments, which may or may not be supported by the theme you choose.

thank you for taking the time to read this “GitHub Pages” article.