Skip to content

Commit f0745b9

Browse files
committed
Create a new article on GitHub Actions
1 parent ca21322 commit f0745b9

File tree

1 file changed

+147
-0
lines changed

1 file changed

+147
-0
lines changed
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
---
2+
title: Deploy Resource Manager templates by using GitHub Actions
3+
description: Describes how to deploy Azure Resource Manager templates by using GitHub Actions.
4+
ms.topic: conceptual
5+
ms.date: 05/01/2020
6+
---
7+
8+
# Deploy Azure Resource Manager templates by using GitHub Actions
9+
10+
[GitHub Actions](https://help.github.com/en/actions) enables you to create custom software development life cycle (SDLC) workflows directly in your GitHub repository where your Azure Resource Manager (ARM) templates are stored. A [workflow](https://help.github.com/actions/reference/workflow-syntax-for-github-actions) is defined by a YAML (.yml) file that is located inside the .github/workflows directory in your repository. Workflows must have at least one job, and jobs contain a set of steps that perform individual tasks. Steps can run commands or use an action. You can create your own actions or use actions shared by the GitHub community and customize them as needed. This article shows you how to deploy Resource Manager templates by using an Action called [Azure Resource Manager Template Deployment JS](https://github.com/marketplace/actions/azure-resource-manager-arm-template-deployment-js). You can find more actions from the [GitHub Marketplace](https://github.com/marketplace?type=actions).
11+
12+
The [ARM Template Deployment JS action](https://github.com/marketplace/actions/azure-resource-manager-arm-template-deployment-js) has two dependent actions:
13+
14+
- [Azure Login](https://github.com/marketplace/actions/azure-login): Login with your Azure credentials
15+
- [Checkout](https://github.com/marketplace/actions/checkout): To checks-out your repository so the workflow can access any specified Resource Manager template.
16+
17+
A basic workflow for deploying an Resource Manager template can have three steps:
18+
19+
1. Check out a template file.
20+
2. Sign on to Azure.
21+
3. Deploy an Resource Manager template
22+
23+
## Prerequisites
24+
25+
You need a GitHub repository to store your Resource Manager templates and your workflow files. To create one, see [Creating a new repository](https://help.github.com/en/enterprise/2.14/user/articles/creating-a-new-repository).
26+
27+
## Configure deployment credentials
28+
29+
The Azure login action uses a service principal to authenticate against Azure. The principal of a CI / CD workflow typically needs the built-in contributor right in order to deploy Azure resources.
30+
31+
The following Azure CLI script shows how to generate an Azure Service Principal with Contributor permissions on an Azure resource group. This resource group is where the workflow will deploy the resources defined in your Resource Manager template.
32+
33+
```azurecli
34+
$projectName="[EnterAProjectName]"
35+
$location="centralus"
36+
$resourceGroupName="${projectName}rg"
37+
$appName="http://${projectName}"
38+
$scope=$(az group create --name $resourceGroupName --location $location --query 'id')
39+
az ad sp create-for-rbac --name $appName --role Contributor --scopes $scope --sdk-auth
40+
```
41+
42+
Customize the value of $projectName and $location. The resource group name is the project name with **rg** appended.
43+
44+
The command should output a JSON object similar to this:
45+
46+
```json
47+
{
48+
"clientId": "<GUID>",
49+
"clientSecret": "<GUID>",
50+
"subscriptionId": "<GUID>",
51+
"tenantId": "<GUID>",
52+
(...)
53+
}
54+
```
55+
56+
Copy the JSON output and store it as a GitHub secret within your GitHub repository. See [Prerequisite](#prerequisites) if you don't have a repository yet.
57+
58+
From your GitHub repository:
59+
60+
1. Select **Settings** from the top menu.
61+
1. Select **Secret** from the left menu.
62+
1. Enter the following values:
63+
64+
- **Name**: AZURE_CREDENTIALS
65+
- **Value**: (Paste the JSON output)
66+
1. Select **Add secret**.
67+
68+
You need to specify the secret name in the workflow.
69+
70+
## Add Resource Manager template
71+
72+
Add an Resource Manager template to the GitHub repository. If you don't have one, you can use the following template. The template creates a storage account.
73+
74+
```url
75+
https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/101-storage-account-create/azuredeploy.json
76+
```
77+
78+
You can put the file anywhere in the repository. The workflow sample assumes the template file is named **azuredeploy.json**, and it is stored in a folder called **templates** at the root of your repository.
79+
80+
## Create workflow
81+
82+
The workflow file must be stored in the **.github/workflow** folder at the root of your repository. The workflow file extension can be either **.yml** or **.yaml**.
83+
84+
You can either create a workflow file and the push or upload the file to the repository, or use the following procedure:
85+
86+
1. From your GitHub repository, select **Actions** from the top menu.
87+
1. Select **New workflow**.
88+
1. Select **set up a workflow yourself**.
89+
1. Rename the workflow file if you prefer a different name other than **main.yml**. For example: **deployStorageAccount.yml**.
90+
1. Replace the content of the yml file with the following:
91+
92+
```yml
93+
name: Deploy ARM Template
94+
95+
on:
96+
push:
97+
branches:
98+
- master
99+
paths:
100+
- ".github/workflows/deployStorageAccount.yml"
101+
- "templates/azuredeploy.json"
102+
103+
jobs:
104+
deploy-storage-account-template:
105+
runs-on: ubuntu-latest
106+
steps:
107+
- name: Checkout source code
108+
uses: actions/checkout@master
109+
110+
- name: Login to Azure
111+
uses: azure/login@v1
112+
with:
113+
creds: ${{ secrets.AZURE_CREDENTIALS }}
114+
115+
- name: Deploy ARM Template
116+
uses: whiteducksoftware/azure-arm-action-js@v1
117+
with:
118+
resourceGroupName: myResourceGroup
119+
templateLocation: ./templates/azuredeploy.json
120+
```
121+
122+
There are three sections in the workflow:
123+
124+
- **name**: The name of the workflow.
125+
- **on**: The name of the GitHub events that triggers the workflow. The workflow is trigger when there is a push event on the master branch, which modifies at least one of the two files specified. The two files are the workflow file and the template file.
126+
127+
> ![NOTE]
128+
> Verify the two files and their paths match yours.
129+
- **jobs**: A workflow run is made up of one or more jobs. There is only one job called **deploy-storage-account-template**. This job has three steps:
130+
131+
- **Checkout source code**.
132+
- **Login to Azure**. Verify the secret name matches to what you saved to your repository. See [Configure deployment credentials](#configure-deployment-credentials).
133+
- **Deploy ARM template**. Replace the value of **resourceGroupName**. If you used the Azure CLI script in [Configure deployment credentials](#configure-deployment-credentials), the generated resource group name is the project name with **rg** appended. Verify the value of **templateLocation**.
134+
135+
1. Select **Start commit**.
136+
1. Select **Commit new file**.
137+
138+
## Check workflow status
139+
140+
1. Select the **Actions** tab. You shall see a **Create deployStorageAccount.yml** workflow listed. It takes 1-2 minutes to execute the workflow.
141+
1. Select the workflow to open it.
142+
1. Select **deploy-storage-account-template** (job name) from the left menu.
143+
1. Select **Deploy ARM Template** (step name) to expand it. You can see the REST API response.
144+
145+
## Next steps
146+
147+
For a step-by-step tutorial that guides you through the process of creating a template, see [Tutorial: Create and deploy your first ARM template](template-tutorial-create-first-template.md).

0 commit comments

Comments
 (0)