Skip to content

Commit

Permalink
Merge pull request #119 from ministryofjustice/jasonBirchall-patch-1
Browse files Browse the repository at this point in the history
♻️ Refactor GitHub Actions to Store ECR Registry URL as a Secret
  • Loading branch information
jasonBirchall authored Oct 7, 2024
2 parents f5e3e37 + b9c10e2 commit ad7eaaf
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ No modules.
| [github_actions_environment_secret.ecr_role_to_assume](https://registry.terraform.io/providers/integrations/github/latest/docs/resources/actions_environment_secret) | resource |
| [github_actions_environment_variable.ecr_region](https://registry.terraform.io/providers/integrations/github/latest/docs/resources/actions_environment_variable) | resource |
| [github_actions_environment_variable.ecr_repository](https://registry.terraform.io/providers/integrations/github/latest/docs/resources/actions_environment_variable) | resource |
| [github_actions_secret.ecr_registry_url](https://registry.terraform.io/providers/integrations/github/latest/docs/resources/actions_secret) | resource |
| [github_actions_secret.ecr_role_to_assume](https://registry.terraform.io/providers/integrations/github/latest/docs/resources/actions_secret) | resource |
| [github_actions_variable.ecr_region](https://registry.terraform.io/providers/integrations/github/latest/docs/resources/actions_variable) | resource |
| [github_actions_variable.ecr_repository](https://registry.terraform.io/providers/integrations/github/latest/docs/resources/actions_variable) | resource |
Expand Down
16 changes: 13 additions & 3 deletions main.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
data "aws_caller_identity" "current" {}
data "aws_region" "current" {}

locals {
ecr_registry_url = "${data.aws_caller_identity.current.account_id}.dkr.ecr.${data.aws_region.current.name}.amazonaws.com"

# GitHub configuration
github_repositories = toset([
for repository in var.github_repositories : {
Expand Down Expand Up @@ -32,9 +37,6 @@ locals {
}
}

data "aws_caller_identity" "current" {}
data "aws_region" "current" {}

# ECR repository
resource "aws_ecr_repository" "repo" {
name = "${var.team_name}/${var.repo_name}"
Expand Down Expand Up @@ -309,6 +311,14 @@ resource "github_actions_variable" "ecr_region" {
value = data.aws_region.current.name
}

resource "github_actions_secret" "ecr_registry_url" {
for_each = (length(var.github_environments) == 0 && local.enable_github) ? local.github_repos : []

repository = each.value
secret_name = "ECR_REGISTRY_URL"
plaintext_value = local.ecr_registry_url
}

resource "github_actions_variable" "ecr_repository" {
for_each = (length(var.github_environments) == 0 && local.enable_github) ? local.github_repos : []

Expand Down

0 comments on commit ad7eaaf

Please sign in to comment.