Skip to content

Commit 4c946c0

Browse files
Merge pull request #113522 from mumian/0501-github-actions
Create a new article on GitHub Actions
2 parents f9e1f9d + b305787 commit 4c946c0

File tree

2 files changed

+154
-0
lines changed

2 files changed

+154
-0
lines changed
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
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/05/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 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 file. Workflows have one or more jobs with each job containing 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](https://github.com/marketplace?type=actions) and customize them as needed. This article shows how to use [Azure CLI Action](https://github.com/marketplace/actions/azure-cli-action) to deploy Resource Manager templates.
11+
12+
Azure CLI Action has two dependent actions:
13+
14+
- **[Checkout](https://github.com/marketplace/actions/checkout)**: Check out your repository so the workflow can access any specified Resource Manager template.
15+
- **[Azure Login](https://github.com/marketplace/actions/azure-login)**: Log in with your Azure credentials
16+
17+
A basic workflow for deploying a Resource Manager template can have three steps:
18+
19+
1. Check out a template file.
20+
2. Sign in to Azure.
21+
3. Deploy a 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 deploys 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** in the script. The resource group name is the project name with **rg** appended. You need to specify the resource group name in your workflow.
43+
44+
The script outputs 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+
1. From your GitHub repository, select the **Settings** tab.
59+
1. Select **Secret** from the left menu.
60+
1. Enter the following values:
61+
62+
- **Name**: AZURE_CREDENTIALS
63+
- **Value**: (Paste the JSON output)
64+
1. Select **Add secret**.
65+
66+
You need to specify the secret name in the workflow.
67+
68+
## Add Resource Manager template
69+
70+
Add a 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.
71+
72+
```url
73+
https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/101-storage-account-create/azuredeploy.json
74+
```
75+
76+
You can put the file anywhere in the repository. The workflow sample in the next section assumes the template file is named **azuredeploy.json**, and it is stored in a folder called **templates** at the root of your repository.
77+
78+
## Create workflow
79+
80+
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**.
81+
82+
You can either create a workflow file and then push/upload the file to the repository, or use the following procedure:
83+
84+
1. From your GitHub repository, select **Actions** from the top menu.
85+
1. Select **New workflow**.
86+
1. Select **set up a workflow yourself**.
87+
1. Rename the workflow file if you prefer a different name other than **main.yml**. For example: **deployStorageAccount.yml**.
88+
1. Replace the content of the yml file with the following:
89+
90+
```yml
91+
name: Deploy ARM Template
92+
93+
on:
94+
push:
95+
branches:
96+
- master
97+
paths:
98+
- ".github/workflows/deployStorageAccount.yml"
99+
- "templates/azuredeploy.json"
100+
101+
jobs:
102+
deploy-storage-account-template:
103+
runs-on: ubuntu-latest
104+
steps:
105+
- name: Checkout source code
106+
uses: actions/checkout@master
107+
108+
- name: Login to Azure
109+
uses: azure/login@v1
110+
with:
111+
creds: ${{ secrets.AZURE_CREDENTIALS }}
112+
113+
114+
- name: Deploy ARM Template
115+
uses: azure/CLI@v1
116+
with:
117+
inlineScript: |
118+
az deployment group create --resource-group myResourceGroup --template-file ./templates/azuredeploy.json
119+
```
120+
121+
The workflow file has three sections:
122+
123+
- **name**: The name of the workflow.
124+
- **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.
125+
126+
> [!IMPORTANT]
127+
> Verify the two files and their paths match yours.
128+
- **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:
129+
130+
- **Checkout source code**.
131+
- **Login to Azure**.
132+
133+
> [!IMPORTANT]
134+
> Verify the secret name matches to what you saved to your repository. See [Configure deployment credentials](#configure-deployment-credentials).
135+
- **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**.
136+
137+
1. Select **Start commit**.
138+
1. Select **Commit directly to the master branch**.
139+
1. Select **Commit new file** (or **Commit changes**).
140+
141+
Because the workflow is configured to be triggered by either the workflow file or the template file being updated, the workflow starts right after you commit the changes.
142+
143+
## Check workflow status
144+
145+
1. Select the **Actions** tab. You shall see a **Create deployStorageAccount.yml** workflow listed. It takes 1-2 minutes to execute the workflow.
146+
1. Select the workflow to open it.
147+
1. Select **deploy-storage-account-template** (job name) from the left menu. The job name is defined in the workflow.
148+
1. Select **Deploy ARM Template** (step name) to expand it. You can see the REST API response.
149+
150+
## Next steps
151+
152+
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).

articles/azure-resource-manager/templates/toc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,8 @@
247247
items:
248248
- name: VS project with pipelines
249249
href: add-template-to-azure-pipelines.md
250+
- name: Deploy - GitHub Actions
251+
href: deploy-github-actions.md
250252
- name: Export template
251253
href: export-template-portal.md
252254
- name: View deployment history

0 commit comments

Comments
 (0)