Skip to content

Commit 4f353e4

Browse files
authored
Merge pull request #198450 from hhunter-ms/container-store-tutorial-aca
[Container Apps] GitHub actions tutorial on a Dapr app
2 parents b45eb6e + 06f5c2f commit 4f353e4

22 files changed

+400
-10
lines changed

articles/container-apps/TOC.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@
124124
href: microservices-dapr.md
125125
- name: Microservices with Dapr using ARM or Bicep
126126
href: microservices-dapr-azure-resource-manager.md
127+
- name: Deploy a Dapr application using GitHub Actions
128+
href: dapr-github-actions.md
127129
- name: Background processing
128130
href: background-processing.md
129131
- name: Create an Azure Files storage mount
Lines changed: 390 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,390 @@
1+
---
2+
title: Tutorial - Deploy a Dapr application with GitHub Actions for Azure Container Apps
3+
description: Learn about multiple revision management by deploying a Dapr application with GitHub Actions and Azure Container Apps.
4+
author: cebundy
5+
ms.author: v-bcatherine
6+
ms.reviewer: keroden
7+
ms.service: container-apps
8+
ms.topic: tutorial
9+
ms.date: 09/02/2022
10+
ms.custom: template-tutorial
11+
---
12+
13+
# Tutorial: Deploy a Dapr application with GitHub Actions for Azure Container Apps
14+
15+
[GitHub Actions](https://docs.github.com/en/actions) gives you the flexibility to build an automated software development lifecycle workflow. In this tutorial, you'll see how revision-scope changes to a Container App using [Dapr](https://docs.dapr.io) can be deployed using a GitHub Actions workflow.
16+
17+
Dapr is an open source project that helps developers with the inherent challenges presented by distributed applications, such as state management and service invocation. Azure Container Apps integrates with a [managed version of Dapr](./dapr-overview.md).
18+
19+
In this tutorial, you'll:
20+
21+
> [!div class="checklist"]
22+
> - Configure a GitHub Actions workflow for deploying the end-to-end solution to Azure Container Apps.
23+
> - Modify the source code with a [revision-scope change](revisions.md#revision-scope-changes) to trigger the Build and Deploy GitHub workflow.
24+
> - Learn how revisions are created for container apps in multi-revision mode.
25+
26+
The [sample solution](https://github.com/Azure-Samples/container-apps-store-api-microservice) consists of three Dapr-enabled microservices and uses Dapr APIs for service-to-service communication and state management.
27+
28+
:::image type="content" source="media/dapr-github-actions/arch.png" alt-text="Diagram demonstrating microservices app.":::
29+
30+
> [!NOTE]
31+
> This tutorial focuses on the solution deployment outlined below. If you're interested in building and running the solution on your own, [follow the README instructions within the repo](https://github.com/azure-samples/container-apps-store-api-microservice/build-and-run.md).
32+
33+
## Prerequisites
34+
35+
- An Azure account with an active subscription.
36+
- [Create an account for free](https://azure.microsoft.com/free/?WT.mc_id=A261C142F).
37+
- Contributor or Owner permissions on the Azure subscription.
38+
- A GitHub account.
39+
- If you don't have one, sign up for [free](https://github.com/join).
40+
- Install [Git](https://github.com/git-guides/install-git).
41+
- Install the [Azure CLI](/cli/azure/install-azure-cli).
42+
43+
## Set up the environment
44+
45+
In the console, set the following environment variables:
46+
47+
# [Bash](#tab/bash)
48+
49+
```bash
50+
RESOURCE_GROUP="my-containerapp-store"
51+
LOCATION="canadacentral"
52+
GITHUB_USERNAME="your-GitHub-username"
53+
SUBSCRIPTION_ID="your-subscription-id"
54+
```
55+
56+
# [PowerShell](#tab/powershell)
57+
58+
```powershell
59+
$RESOURCE_GROUP="my-containerapp-store"
60+
$LOCATION="canadacentral"
61+
$GITHUB_USERNAME="<YOUR_GITHUB_USERNAME>"
62+
$SUBSCRIPTION_ID="<YOUR_SUBSCRIPTION_ID>"
63+
```
64+
65+
---
66+
67+
Sign in to Azure from the CLI using the following command, and follow the prompts in your browser to complete the authentication process.
68+
69+
# [Bash](#tab/bash)
70+
71+
```azurecli
72+
az login
73+
```
74+
75+
# [PowerShell](#tab/powershell)
76+
77+
```azurecli
78+
az login
79+
```
80+
81+
---
82+
83+
Ensure you're running the latest version of the CLI via the upgrade command.
84+
85+
# [Bash](#tab/bash)
86+
87+
```azurecli
88+
az upgrade
89+
```
90+
91+
# [PowerShell](#tab/powershell)
92+
93+
```azurecli
94+
az upgrade
95+
```
96+
97+
---
98+
99+
Now that you've validated your Azure CLI setup, bring the application code to your local machine.
100+
101+
## Get application code
102+
103+
1. Navigate to the [sample GitHub repo](https://github.com/Azure-Samples/container-apps-store-api-microservice.git) and click **Fork** in the top-right corner of the page.
104+
105+
1. Use the following [git](https://git-scm.com/downloads) command with your GitHub username to clone **your fork** of the repo to your development environment:
106+
107+
# [Bash](#tab/bash)
108+
109+
```git
110+
git clone https://github.com/$GITHUB_USERNAME/container-apps-store-api-microservice.git
111+
```
112+
113+
# [PowerShell](#tab/powershell)
114+
115+
```git
116+
git clone https://github.com/$GITHUB_USERNAME/container-apps-store-api-microservice.git
117+
```
118+
119+
---
120+
121+
Navigate into the cloned directory.
122+
123+
```console
124+
cd container-apps-store-api-microservice
125+
```
126+
127+
The repository includes the following resources:
128+
129+
- The source code for each application
130+
- Deployment manifests
131+
- A GitHub Actions workflow file
132+
133+
## Deploy Dapr solution using GitHub Actions
134+
135+
The GitHub Actions workflow YAML file in the `/.github/workflows/` folder executes the following steps in the background as you work through this tutorial:
136+
137+
| Section | Tasks |
138+
| ---------------------- | ----------------------------------------------------------------- |
139+
| **Authentication** | Log in to a private container registry (GitHub Container Registry) |
140+
| **Build** | Build & push the container images for each microservice |
141+
| **Authentication** | Log in to Azure |
142+
| **Deploy using bicep** | 1. Create a resource group <br>2. Deploy Azure Resources for the solution using bicep |
143+
144+
The following resources are deployed via the bicep template in the `/deploy` path of the repository:
145+
146+
- Log Analytics workspace
147+
- Application Insights
148+
- Container apps environment
149+
- Order service container app
150+
- Inventory container app
151+
- Azure Cosmos DB
152+
153+
### Create a service principal
154+
155+
The workflow requires a [service principal](../active-directory/develop/app-objects-and-service-principals.md#service-principal-object) to authenticate to Azure. In the console, run the following command and replace `<SERVICE_PRINCIPAL_NAME>` with your own unique value.
156+
157+
# [Bash](#tab/bash)
158+
159+
```azurecli
160+
az ad sp create-for-rbac \
161+
--name <SERVICE_PRINCIPAL_NAME> \
162+
--role "contributor" \
163+
--scopes /subscriptions/$SUBSCRIPTION_ID \
164+
--sdk-auth
165+
```
166+
167+
# [PowerShell](#tab/powershell)
168+
169+
```azurecli
170+
az ad sp create-for-rbac `
171+
--name <SERVICE_PRINCIPAL_NAME> `
172+
--role "contributor" `
173+
--scopes /subscriptions/$SUBSCRIPTION_ID `
174+
--sdk-auth
175+
```
176+
---
177+
178+
The output is the role assignment credentials that provide access to your resource. The command should output a JSON object similar to:
179+
180+
```json
181+
{
182+
"clientId": "<GUID>",
183+
"clientSecret": "<GUID>",
184+
"subscriptionId": "<GUID>",
185+
"tenantId": "<GUID>"
186+
(...)
187+
}
188+
```
189+
190+
Copy the JSON object output and save it to a file on your machine. You use this file as you authenticate from GitHub.
191+
192+
### Configure GitHub Secrets
193+
194+
1. While in GitHub, browse to your forked repository for this tutorial.
195+
1. Select the **Settings** tab.
196+
1. Select **Secrets** > **Actions**.
197+
1. On the **Actions secrets** page, select **New repository secret**.
198+
199+
:::image type="content" source="media/dapr-github-actions/secrets-actions.png" alt-text="Screenshot of selecting settings, then actions from under secrets in the menu, then the new repository secret button.":::
200+
201+
1. Create the following secrets:
202+
203+
| Name | Value |
204+
| ---- | ----- |
205+
| `AZURE_CREDENTIALS` | The JSON output you saved earlier from the service principal creation |
206+
| `RESOURCE_GROUP` | Set as **my-containerapp-store** |
207+
208+
:::image type="content" source="media/dapr-github-actions/secrets.png" alt-text="Screenshot of all three secrets once created.":::
209+
210+
### Trigger the GitHub Action
211+
212+
To build and deploy the initial solution to Azure Container Apps, run the "Build and deploy" workflow.
213+
214+
1. Open the **Actions** tab in your GitHub repository.
215+
1. In the left side menu, select the **Build and Deploy** workflow.
216+
217+
:::image type="content" source="media/dapr-github-actions/run-workflow.png" alt-text="Screenshot of the Actions tab in GitHub and running the workflow.":::
218+
219+
1. Select **Run workflow**.
220+
1. In the prompt, leave the *Use workflow from* value as **Branch: main**.
221+
1. Select **Run workflow**.
222+
223+
### Verify the deployment
224+
225+
After the workflow successfully completes, verify the application is running in Azure Container Apps.
226+
227+
1. Navigate to the [Azure portal](https://portal.azure.com).
228+
1. In the search field, enter **my-containerapp-store** and select the **my-containerapp-store** resource group.
229+
230+
:::image type="content" source="media/dapr-github-actions/search-resource-group.png" alt-text="Screenshot of searching for and finding my container app store resource group.":::
231+
232+
1. Navigate to the container app called **node-app**.
233+
234+
:::image type="content" source="media/dapr-github-actions/node-app.png" alt-text="Screenshot of the node app container app in the resource group list of resources.":::
235+
236+
1. Select the **Application Url**.
237+
238+
:::image type="content" source="media/dapr-github-actions/app-url.png" alt-text="Screenshot of the application url.":::
239+
240+
1. Ensure the application was deployed successfully by creating a new order:
241+
1. Enter an **Id** and **Item**.
242+
1. Select **Create**.
243+
244+
:::image type="content" source="media/dapr-github-actions/create-order.png" alt-text="Screenshot of creating an order via the application url.":::
245+
246+
If the order is persisted, you're redirected to a page that says "Order created!"
247+
248+
1. Navigate back to the previous page.
249+
250+
1. View the item you created via the **View Order** form:
251+
1. Enter the item **Id**.
252+
1. Select **View**.
253+
254+
:::image type="content" source="media/dapr-github-actions/view-order.png" alt-text="Screenshot of viewing the order via the view order form.":::
255+
256+
The page is redirected to a new page displaying the order object.
257+
258+
1. In the Azure portal, navigate to **Application** > **Revision Management** in the **node-app** container.
259+
260+
Note that, at this point, only one revision is available for this app.
261+
262+
:::image type="content" source="media/dapr-github-actions/single-revision-view.png" alt-text="Screenshot of checking the number of revisions at this point of the tutorial.":::
263+
264+
265+
## Modify the source code to trigger a new revision
266+
267+
Container Apps run in single-revision mode by default. In the Container Apps bicep module, we explicitly set the revision mode to multiple. This means that once the source code is changed and committed, the GitHub build/deploy workflow builds and pushes a new container image to GitHub Container Registry. Changing the container image is considered a [revision-scope](revisions.md#revision-scope-changes) change and results in a new container app revision.
268+
269+
> [!NOTE]
270+
> [Application-scope](revisions.md#application-scope-changes) changes do not create a new revision.
271+
272+
To demonstrate the inner-loop experience for creating revisions via GitHub actions, you'll make a change to the frontend application and commit this change to your repo.
273+
274+
1. Return to the console, and navigate into the *node-service/views* directory in the forked repository.
275+
276+
277+
# [Bash](#tab/bash)
278+
279+
```azurecli
280+
cd node-service/views
281+
```
282+
283+
# [PowerShell](#tab/powershell)
284+
285+
```azurecli
286+
cd node-service/views
287+
```
288+
---
289+
290+
1. Open the *index.jade* file in your editor of choice.
291+
292+
293+
# [Bash](#tab/bash)
294+
295+
```azurecli
296+
code index.jade .
297+
```
298+
299+
# [PowerShell](#tab/powershell)
300+
301+
```azurecli
302+
code index.jade .
303+
```
304+
---
305+
306+
1. At the bottom of the file, uncomment the following code to enable deleting an order from the Dapr state store.
307+
308+
```jade
309+
h2= 'Delete Order'
310+
br
311+
br
312+
form(action='/order/delete', method='post')
313+
div.input
314+
span.label Id
315+
input(type='text', name='id', placeholder='foo', required='required')
316+
div.actions
317+
input(type='submit', value='View')
318+
```
319+
320+
1. Stage the changes and push to the `main` branch of your fork using git.
321+
322+
# [Bash](#tab/bash)
323+
324+
```azurecli
325+
git add .
326+
git commit -m '<commit message>'
327+
git push origin main
328+
```
329+
330+
# [PowerShell](#tab/powershell)
331+
332+
```azurecli
333+
git add .
334+
git commit -m '<commit message>'
335+
git push origin main
336+
```
337+
---
338+
339+
### View the new revision
340+
341+
1. In the GitHub UI of your fork, select the **Actions** tab to verify the GitHub **Build and Deploy** workflow is running.
342+
343+
1. Once the workflow is complete, navigate to the **my-containerapp-store** resource group in the Azure portal.
344+
345+
1. Select the **node-app** container app.
346+
347+
1. In the left side menu, select **Application** > **Revision Management**.
348+
349+
:::image type="content" source="media/dapr-github-actions/revision-mgmt.png" alt-text="Screenshot that shows Revision Management in the left side menu.":::
350+
351+
Since our container app is in **multiple revision mode**, Container Apps created a new revision and automatically sets it to `active` with 100% traffic.
352+
353+
:::image type="content" source="media/dapr-github-actions/two-revisions.png" alt-text="Screenshot that shows both the inactive and active revisions on the node app.":::
354+
355+
1. Select each revision in the **Revision management** table to view revision details.
356+
357+
:::image type="content" source="media/dapr-github-actions/revision-details.png" alt-text="Screenshot of the revision details for the active node app revision.":::
358+
359+
1. View the new revision in action by refreshing the node-app UI.
360+
361+
1. Test the application further by deleting the order you created in the container app.
362+
363+
:::image type="content" source="media/dapr-github-actions/delete-order.png" alt-text="Screenshot of deleting the order created earlier in the tutorial.":::
364+
365+
The page is redirected to a page indicating that the order is removed.
366+
367+
## Clean up resources
368+
369+
Once you've finished the tutorial, run the following command to delete your resource group, along with all the resources you created in this tutorial.
370+
371+
# [Bash](#tab/bash)
372+
373+
```azurecli
374+
az group delete \
375+
--resource-group $RESOURCE_GROUP
376+
```
377+
378+
# [PowerShell](#tab/powershell)
379+
380+
```powershell
381+
Remove-AzResourceGroup -Name $RESOURCE_GROUP -Force
382+
```
383+
384+
---
385+
386+
387+
## Next steps
388+
389+
Learn more about how [Dapr integrates with Azure Container Apps](./dapr-overview.md).
390+

0 commit comments

Comments
 (0)