You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/guided-workshop/exercises/7-create-environment.md
+51-33Lines changed: 51 additions & 33 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,22 +1,22 @@
1
1
# Create a deployment environment
2
2
3
-
When we think about creating a deployment environment, we know this is something which won't be done regularly. You might spin up a new staging environment for testing, or when a new instance of the application is created. As a result, this might not seem like something we would want to automate. Tasks which are run frequently, like unit testing, are obvious candidates for automation. But what about those which are run sporadically?
3
+
Creating the environment where our application will be deployed to is something which likely won't be done regularly. You'll create the environment when you first look to deploy the project, and maybe when it comes time to do some testing. As a result, this might not seem like something we would want to automate. Tasks which are run frequently, like unit testing, are obvious candidates for automation. But what about those which are run sporadically?
4
4
5
5
As it turns out, it can be argued that those which are run infrequently are just as important to be automated, if not more so. The reason is if a task isn't run regularly it's easier to miss steps or to lose time investigating what needs to be done. It's typically worth the initial investment up front building out an automated process which will payoff in the future by ensuring consistency and ease of use. Specific to creating a deployment environment, ensuring it's created correctly allows for the automated tasks to actually perform the deployment to run successfully.
6
6
7
7
With GitHub Actions, you can use `workflow_dispatch` as a trigger for [manual execution of workflows](https://docs.github.com/en/actions/using-workflows/manually-running-a-workflow). This is perfect for scenarios like creating a deployment environment.
8
8
9
-
## Infrastructure as code
10
-
11
-
[Infrastructure as code (IaC)](https://en.wikipedia.org/wiki/Infrastructure_as_code), also sometimes referred to as config as code, is a mechanism where the infrastructure required for an application is defined in a configuration file. There are numerous languages which support IaC, such as [Terraform](https://www.terraform.io/) and [Bicep](https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/overview?tabs=bicep). By using IaC, the definition is created once and reused multiple times ensuring consistency. Rather than providing a list of instructions for a human to follow, a code file contains all of the necessary settings which is then used by an automated process (like GitHub Actions).
12
-
13
9
## Scenario
14
10
15
11
With the project created, the code supply chain secured, and end-to-end testing implemented, the shelter is ready to begin deploying the project. They've selected [Azure](https://azure.microsoft.com/en-us/free) as the cloud provider. Specifically, they want to use [Azure Container Apps](https://learn.microsoft.com/en-us/azure/container-apps/overview) to host the website, and [Azure Cosmos DB for MongoDB](https://learn.microsoft.com/en-us/azure/cosmos-db/mongodb/introduction) as the backend database. A [Bicep file](../../../config/main.bicep) has already been created by another contractor. You want to create a new workflow to execute on demand to create the deployment environment.
16
12
17
13
> **NOTE:** For this exercise, a small amount of Azure credit will be required to store the website's image and the database. For the purposes of this workshop, the total amount should be less than $10US if you keep the website up for an entire month. At the end of the workshop, delete the resource group to ensure all billing stops.
18
14
19
-
## Exploring the Bicep file
15
+
## Infrastructure as code
16
+
17
+
[Infrastructure as code (IaC)](https://en.wikipedia.org/wiki/Infrastructure_as_code), also sometimes referred to as config as code, is a mechanism where the infrastructure required for an application is defined in a configuration file. There are numerous languages which support IaC, such as [Terraform](https://www.terraform.io/) and [Bicep](https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/overview?tabs=bicep). By using IaC, the definition is created once and reused multiple times ensuring consistency. Rather than providing a list of instructions for a human to follow, a code file contains all of the necessary settings which is then used by an automated process (like GitHub Actions).
18
+
19
+
### Exploring the Bicep file
20
20
21
21
[Bicep](https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/overview?tabs=bicep) is a domain specific language (DSL) created by Microsoft to describe and deploy Azure resources. With a Bicep file you can establish the services required, their configuration, and even set variables. This allows for flexibility and reuse, ensuring the environment is created correctly each time.
22
22
@@ -42,27 +42,29 @@ With the project created, the code supply chain secured, and end-to-end testing
42
42
All resources created in Azure are contained in resource groups. As the name implies, this allows you to group resources together. In our situation, this allows for streamlined management and permissions, and to speed cleanup as deleting the resource group will delete all associated resources. Let's create the resource group using the [Azure command-line interface (CLI)](https://learn.microsoft.com/en-us/cli/azure/what-is-azure-cli), and create a security principal with permissions to the resource group. This account will be used in the future to create the resources and deploy the website.
43
43
44
44
1. Return to your codespace.
45
-
1.Open a terminal window by pressing <kbd>Ctl</kbd> - <kbd>`</kbd>.
46
-
1. Log into Azure via the Azure CLI by entering the following command and pressing <kbd>Enter</kbd> (or <kbd>Return</kbd> on a Mac):
45
+
1.If a terminal window isn't already open, open one by pressing <kbd>Ctl</kbd> - <kbd>`</kbd>.
46
+
1. Log into Azure via the Azure CLI by entering the following command:
47
47
48
48
```bash
49
49
az login --use-device-code
50
50
```
51
51
52
52
1. Follow the on-screen prompts to complete the authentication process.
53
-
1. Create a resource group named **pets-workshop** by entering the following command and pressing <kbd>Enter</kbd> (or <kbd>Return</kbd> on a Mac):
53
+
1. Create a resource group named **pets-workshop** by entering the following command:
54
54
55
55
```bash
56
56
az group create -n pets-workshop -l westus
57
57
```
58
58
59
-
1. Obtain your Azure subscription ID (used in the next step) by entering the following command and pressing <kbd>Enter</kbd> (or <kbd>Return</kbd> on a Mac):
59
+
>**NOTE:** If prompted to allow pasting through your browser, select**Allow**.
60
+
61
+
1. Obtain your Azure subscription ID (used in the next step) by entering the following command:
60
62
61
63
```bash
62
64
az account show --query id -o tsv
63
65
```
64
66
65
-
1. Create the service principal to be used to manage the resource group by entering the following command, replacing **<SUBSCRIPTION_ID>** with your subscription ID obtained in the prior step, and pressing <kbd>Enter</kbd> (or <kbd>Return</kbd> on a Mac):
67
+
1. Create the service principal to be used to manage the resource group by entering the following command, replacing **<SUBSCRIPTION_ID>** with your subscription ID obtained in the prior step,:
66
68
67
69
```bash
68
70
az ad sp create-for-rbac --name pets-workshop-app --role contributor --scopes /subscriptions/<SUBSCRIPTION_ID>/resourceGroups/pets-workshop --sdk-auth
@@ -72,7 +74,7 @@ All resources created in Azure are contained in resource groups. As the name imp
72
74
73
75
1. Copy the JSON to a scratchpad such as Notepad or Notes. You will use this object in the next step.
74
76
75
-
>**IMPORTANT:**In the real world, this should be treated the same as any credential or username and password. It should be properly secured and not shared with anyone.
77
+
>**IMPORTANT:**The credentials provided from this step should be treated the same as any credential or username and password. It should be properly secured and not shared with anyone.
76
78
77
79
## Securing secrets in a repository
78
80
@@ -85,6 +87,9 @@ Let's create the secrets required for our workflow.
85
87
1. In a new browser tab, navigate to your repository.
86
88
1. Select the **Settings** tab.
87
89
1. On the left side, expand **Secrets and variables** and select**Actions**.
90
+
91
+

92
+
88
93
1. Create a new repository secret to store the credentials by selecting **New repository secret**, entering the following values (replacing `<THE JSON FROM THE PRIOR STEP>` with the JSON you created previously), and selecting **Add secret**:
89
94
90
95
- **Name**: `AZURE_CREDENTIALS`
@@ -95,17 +100,19 @@ Let's create the secrets required for our workflow.
95
100
- **Name**: `AZURE_SUBSCRIPTION`
96
101
- **Secret**: `<SUBSCRIPTION_ID>`
97
102
103
+
## Creating variables for workflows
104
+
98
105
Not all values need to be secured. For sensitive information, like credentials or your subscription ID, it's best to store those properly. But other values, like the name of your resource group and the prefix you'll use forthe other resources to be created, don't need to be hidden. These are perfect for variables. Variables behavein much the same way as secrets, except they're not encrypted or hidden from repository owners.
99
106
100
107
Let's create variables for the name of the resource group and your prefix:
101
108
102
-
1. On the **Actions secrets and variables** screen, selectthe**Variables** tab.
109
+
1. On the **Actions secrets and variables** screen (the same screen you were on previously), selectthe**Variables** tab.
103
110
1. Create a variable for the name of the resource group by selecting **New variable**, entering the following values, and selecting **Add variable**:
104
111
105
112
- **Name**: `AZURE_RG`
106
113
- **Value**: `pets-workshop`
107
114
108
-
1. Create a variable for the prefix to use for naming other resources by selecting **New variable**, entering the following values, replacing `<PREFIX_NAME>` with five random letters, and selecting **Add variable**:
115
+
1. Create a variable for the prefix to use for naming other resources by selecting **New variable**, entering the following values, replacing `<PREFIX_NAME>` with five random letters (such as **aetel**), and selecting **Add variable**:
109
116
110
117
- **Name**: `AZURE_PREFIX`
111
118
- **Value**: `<PREFIX_NAME>`
@@ -116,7 +123,7 @@ You've now configured Azure and added secrets & variables to your repository. Yo
116
123
117
124
1. Return to your codespace.
118
125
1. If the **Terminal** window isn't already open, open it by pressing <kbd>Ctl</kbd> - <kbd>`</kbd> on your keyboard.
119
-
1. Switch to the `main` branch, pull any changes currently on the server to your codespace, and create a new branch by entering the following commandin the terminal window and pressing <kbd>Enter</kbd> (or <kbd>Return</kbd> on a Mac):
126
+
1. Switch to the `main` branch, pull any changes currently on the server to your codespace, and create a new branch by entering the following commandin the terminal window:
120
127
121
128
```bash
122
129
git checkout main
@@ -133,56 +140,60 @@ You've now configured Azure and added secrets & variables to your repository. Yo
The workflow is set to run on `workflow_dispatch`, which is a manual trigger. The steps checkout the code, log into Azure using the credentials you created and stored previously, then create the resources defined in the **main.bicep**in the resource group you created with the prefix you defined.
165
+
The workflow is set to run on `workflow_dispatch`, which is a manual trigger. The steps checkout the code, log into Azure using the credentials you created and stored previously, then create the resources defined in the **main.bicep**in the resource group you created with the prefix you defined. Notice how secrets are read by using `${{ secrets.NAME }}` and variables with `${{ variables.NAME }}`.
159
166
160
-
1. Stage, commit and push all changes to the repository by entering the following commandin the terminal window and pressing <kbd>Enter</kbd> (or <kbd>Return</kbd> on a Mac):
167
+
1. Stage, commit and push all changes to the repository by entering the following commandin the terminal window:
161
168
162
169
```bash
163
170
git add .
164
171
git commit -m "Defined workflow"
165
172
git push -u origin add-resource-workflow
166
173
```
167
174
168
-
1. Obtain the number forthe issue you created for creating deployment environment by entering the following commandin the terminal window and pressing <kbd>Enter</kbd> (or <kbd>Return</kbd> on a Mac):
175
+
1. Obtain the number forthe issue you created for creating deployment environment by entering the following commandin the terminal window:
169
176
170
177
```bash
171
178
gh issue list
172
179
```
173
180
174
-
1. Create a pull request (PR) forthe newly created branch referencing the issue, replacing <ISSUE_NUMBER> with the issue you obtainedin the prior step by entering the following commandin the terminal window and pressing <kbd>Enter</kbd> (or <kbd>Return</kbd> on a Mac):
181
+
1. Create a pull request (PR) forthe newly created branch referencing the issue, replacing <ISSUE_NUMBER> with the issue you obtainedin the prior step by entering the following commandin the terminal window:
1. Merge the PR you just created by entering the following command, replacing <PR_NUMBER> with the newly generated PR number, in the terminal window and pressing <kbd>Enter</kbd> (or <kbd>Return</kbd> on a Mac):
187
+
1. Merge the PR you just created by entering the following command, replacing <PR_NUMBER> with the newly generated PR number, in the terminal window:
181
188
182
189
```bash
183
190
gh pr merge <PR_NUMBER>
184
191
```
185
192
193
+
1. When prompted, press <kbd>Enter</kbd> (or <kbd>return</kbd> on a Mac) to create a merge commit.
194
+
1. When prompted, press <kbd>y</kbd> and press <kbd>Enter</kbd> (or <kbd>return</kbd> on a Mac) to delete the branch and return to `main`.
195
+
1. When prompted, press <kbd>Enter</kbd> (or <kbd>return</kbd> on a Mac) to submit the command.
196
+
186
197
>**IMPORTANT:** Normally you would go through a standard review flow before merging a PR. Because we're working through a set of exercises as part of a workshop we're going to shortcut a couple of steps.
187
198
188
199
## Running the workflow
@@ -191,23 +202,30 @@ You've prepped everything on both Azure and your repository, and created the wor
191
202
192
203
1. Navigate to your repository.
193
204
1. Select the **Actions** tab.
194
-
1. On the list of workflows, select**Create Azure resources**.
195
-
1. Select the ellipsis (**...**) next to **Create Azure resources** and select**Run workflow**.
205
+
1. On the list of workflows, select**Create Azure resources** to open the workflow page.
206
+
207
+

196
208
197
-
The workflow will now run and create the resources! This will take several minutes. You can navigate into the workflow run to view the log and track the progress.
209
+
1. Run the workflow by selecting the **Run workflow** dropdown box then the **Run workflow** button**.
210
+
211
+
The workflow will now run and create the resources! This will take several minutes. You may need to refresh the page to see it start running. Once running, you can navigate into the workflow run to view the log and track the progress.
198
212
199
213
1. When the workflow completes, return to your codespace.
200
-
1. Obtain the URL forthe newly created Azure Container App by entering the following commandin the terminal window and pressing <kbd>Enter</kbd> (or <kbd>Return</kbd> on a Mac):
214
+
1. Obtain the URL forthe newly created Azure Container App by entering the following commandin the terminal window:
201
215
202
216
```bash
203
217
az containerapp list --query "[].properties.configuration.ingress.fqdn" -o tsv
204
218
```
205
219
206
-
1. Navigate to the site by using <kbd>Ctl</kbd>- **Click**(or <kbd>Cmd</kbd>- **Click**on a Mac) on the URL displayed.
207
-
1. You will be presented with a "Hello, world" page. (Don't worry - you'll deploy your site shortly!)
220
+
1. When prompted to install the extension, press <kbd>Enter</kbd> (or <kbd>return</kbd> on a Mac) to approve the installation.
221
+
1. Note the URL provided; you'll use it in the next exercise when you deploy your website!
208
222
209
223
## Summary and next steps
210
224
211
225
Congratulations! You have new defined a workflow which uses infrastructure as code (IaC) to create the resources necessary for deployment. This allows you to quickly create a consistent environment, reducing overhead and errors. Let's close everything out by [implementing continuous deployment](8-deployment.md).
0 commit comments