Skip to content

Commit e3040a6

Browse files
authored
Merge pull request #51 from meshcloud/feature/github-actions-terraform-data
Feature/GitHub actions terraform data
2 parents 8089321 + 38adfbd commit e3040a6

File tree

14 files changed

+120
-30
lines changed

14 files changed

+120
-30
lines changed

modules/azure/github-actions-terraform-setup/backplane/outputs.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ ARM_CLIENT_SECRET = "${azuread_service_principal_password.starterkit.value}"
1212
1313
# The Role definition ID to assign to the GitHub Actions App Service Managed Identity. This is used to deploy resources via Terraform.
1414
15+
role_definition_display_name = "${azuread_service_principal.starterkit.display_name}-deploy"
1516
var.deploy_role_definition_id = "${azurerm_role_definition.starterkit_deploy.role_definition_id}"
1617
1718
EOF

modules/azure/github-actions-terraform-setup/buildingblock/README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ description: |
99
# Azure GitHub Actions Terraform Setup
1010

1111
## Structure of this Kit module
12-
This kit module consists of three components, each enabling the deployment of the next. It serves as the foundational building block — a Terraform module that defines an instance of the starter kit for a specific application team. This includes setting up a GitHub repository and a GitHub Actions pipeline.
12+
This kit module consists of four components, each enabling the deployment of the next. It serves as the foundational building block — a Terraform module that defines an instance of the starter kit for a specific application team.
13+
This includes setting up a GitHub repository and configuring a GitHub Actions pipeline.
14+
15+
For the deployment of a role_assignment required by the GitHub pipeline, we use a helper or pre-executed building block. This step is performed before the main building block is executed. The challenge is that the login context must be refreshed —
16+
a separate login is required to run the automation correctly. This preliminary building block is located in the buildingblock/pre_role_assignment directory.
1317

1418
For more information, refer to the backplane documentation of the [Azure GitHub Actions Terraform Setup Module](https://github.com/meshcloud/meshstack-hub/modules/azure/github-actions-terraform-setup/backplane/README.md).
1519

@@ -40,7 +44,6 @@ No modules.
4044
| [azurerm_role_assignment.ghactions_app](https://registry.terraform.io/providers/hashicorp/azurerm/4.4.0/docs/resources/role_assignment) | resource |
4145
| [azurerm_role_assignment.ghactions_register](https://registry.terraform.io/providers/hashicorp/azurerm/4.4.0/docs/resources/role_assignment) | resource |
4246
| [azurerm_role_assignment.project_admins_blobs](https://registry.terraform.io/providers/hashicorp/azurerm/4.4.0/docs/resources/role_assignment) | resource |
43-
| [azurerm_role_assignment.starterkit_deploy](https://registry.terraform.io/providers/hashicorp/azurerm/4.4.0/docs/resources/role_assignment) | resource |
4447
| [azurerm_role_definition.ghactions](https://registry.terraform.io/providers/hashicorp/azurerm/4.4.0/docs/resources/role_definition) | resource |
4548
| [azurerm_role_definition.ghactions_register](https://registry.terraform.io/providers/hashicorp/azurerm/4.4.0/docs/resources/role_definition) | resource |
4649
| [azurerm_storage_account.tfstates](https://registry.terraform.io/providers/hashicorp/azurerm/4.4.0/docs/resources/storage_account) | resource |
Loading

modules/azure/github-actions-terraform-setup/buildingblock/main.tf

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,12 @@
11
# note: this building block is expected to be executed with pre configured output by the "backplane" module in the
22
# parent dir, this needs to provided in the BB execution enviornment
3+
#
4+
# additionaly:
5+
# there is pre_role_assignemnt Building Block which is expected to be executed before this BB
36

47
data "azurerm_subscription" "current" {}
58
data "azuread_client_config" "current" {}
69

7-
# note: it's important that all other azure resources transitively depend on this role assignment or else they will fail
8-
resource "azurerm_role_assignment" "starterkit_deploy" {
9-
# since the role is defined on MG level, we need to prefix the subscription id here to make terraform happy and not plan replacements
10-
# see https://github.com/hashicorp/terraform-provider-azurerm/issues/19847#issuecomment-1407262429
11-
role_definition_id = "${data.azurerm_subscription.current.id}/providers/Microsoft.Authorization/roleDefinitions/${var.deploy_role_definition_id}"
12-
13-
description = "Grant permissions to deploy a starterkit building block."
14-
principal_id = data.azuread_client_config.current.object_id
15-
scope = data.azurerm_subscription.current.id
16-
}
17-
18-
resource "time_sleep" "wait" {
19-
depends_on = [azurerm_role_assignment.starterkit_deploy]
20-
21-
create_duration = "2m"
22-
}
23-
2410
#
2511
# configure developer access
2612
#
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
---
2+
name: Role Assignments for GitHub Actions Terraform Setup
3+
supportedPlatforms:
4+
- azure
5+
description: |
6+
Helper building block used to assign the necessary Azure roles
7+
---
8+
9+
# Role Assignments for GitHub Actions Terraform Setup
10+
11+
This Terraform module is a helper building block used to assign the necessary Azure roles to the GitHub Actions pipeline **before** the main starter kit
12+
module is executed.
13+
14+
## Purpose
15+
16+
The GitHub Actions pipeline requires specific permissions to deploy resources. Since these permissions must exist prior to running the main automation,
17+
this module ensures that the correct `role_assignment` is created in advance.
18+
19+
## Usage Context
20+
21+
Due to changes in the access context after assigning roles, the automation must **re-authenticate** with a fresh login before proceeding with the main building block.
22+
This module should therefore be executed as a separate step at the beginning of the deployment flow.
23+
24+
<!-- BEGIN_TF_DOCS -->
25+
## Requirements
26+
27+
| Name | Version |
28+
|------|---------|
29+
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.0 |
30+
| <a name="requirement_azuread"></a> [azuread](#requirement\_azuread) | 3.0.2 |
31+
| <a name="requirement_azurerm"></a> [azurerm](#requirement\_azurerm) | 4.4.0 |
32+
33+
## Modules
34+
35+
No modules.
36+
37+
## Resources
38+
39+
| Name | Type |
40+
|------|------|
41+
| [azurerm_role_assignment.starterkit_deploy](https://registry.terraform.io/providers/hashicorp/azurerm/4.4.0/docs/resources/role_assignment) | resource |
42+
| [azuread_client_config.current](https://registry.terraform.io/providers/hashicorp/azuread/3.0.2/docs/data-sources/client_config) | data source |
43+
| [azurerm_subscription.current](https://registry.terraform.io/providers/hashicorp/azurerm/4.4.0/docs/data-sources/subscription) | data source |
44+
45+
## Inputs
46+
47+
| Name | Description | Type | Default | Required |
48+
|------|-------------|------|---------|:--------:|
49+
| <a name="input_deploy_role_definition_id"></a> [deploy\_role\_definition\_id](#input\_deploy\_role\_definition\_id) | Role definition ID to assign to the GitHub Actions App Service Managed Identity. This is used to deploy resources via Terraform. | `string` | n/a | yes |
50+
51+
## Outputs
52+
53+
No outputs.
54+
<!-- END_TF_DOCS -->
Loading
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# note: this building block is expected to be executed with pre configured output by the "backplane" module in the
2+
# parent dir, this needs to provided in the BB execution enviornment
3+
4+
data "azurerm_subscription" "current" {}
5+
data "azuread_client_config" "current" {}
6+
7+
# note: it's important that all other azure resources transitively depend on this role assignment or else they will fail
8+
resource "azurerm_role_assignment" "starterkit_deploy" {
9+
# since the role is defined on MG level, we need to prefix the subscription id here to make terraform happy and not plan replacements
10+
# see https://github.com/hashicorp/terraform-provider-azurerm/issues/19847#issuecomment-1407262429
11+
role_definition_id = "${data.azurerm_subscription.current.id}/providers/Microsoft.Authorization/roleDefinitions/${var.deploy_role_definition_id}"
12+
13+
description = "Grant permissions to deploy a starterkit building block."
14+
principal_id = data.azuread_client_config.current.object_id
15+
scope = data.azurerm_subscription.current.id
16+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
provider "azurerm" {
2+
features {
3+
resource_group {
4+
prevent_deletion_if_contains_resources = false # This allows the deletion of the building block without having to separately delete the app resources
5+
}
6+
}
7+
8+
resource_provider_registrations = "extended"
9+
10+
storage_use_azuread = true
11+
}
12+
13+
provider "azuread" {}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
variable "deploy_role_definition_id" {
2+
type = string
3+
description = "Role definition ID to assign to the GitHub Actions App Service Managed Identity. This is used to deploy resources via Terraform."
4+
}

0 commit comments

Comments
 (0)