Skip to content

Commit

Permalink
try to import existing environments
Browse files Browse the repository at this point in the history
  • Loading branch information
jabez007 committed Jul 29, 2024
1 parent f08d3b7 commit 146fc4d
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 18 deletions.
14 changes: 14 additions & 0 deletions imports.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
data "github_repository_environments" "my_envs" {
# https://registry.terraform.io/providers/integrations/github/latest/docs/data-sources/repository_environments
for_each = toset(data.github_repositories.my_topics.names)
repository = each.key
}

import {
for_each = tomap({
for ext in local.existing_envs : "${ext.repository} - ${ext.env_name}" => ext
})

to = github_repository_environment.environments[each.key]
id = each.value.env_id
}
10 changes: 10 additions & 0 deletions locals.tf
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,14 @@ locals {
}
]
])

existing_envs = flatten([
for repo in data.github_repositories.my_topics.names : [
for env in data.github_repository_environments.my_envs[repo].environments : {
repository = repo
env_name = env.name
env_id = env.node_id
}
]
])
}
18 changes: 0 additions & 18 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,3 @@ provider "github" {
owner = var.github_owner
token = var.github_token
}

data "github_repositories" "my_topics" {
# https://docs.github.com/en/search-github/getting-started-with-searching-on-github/understanding-the-search-syntax
query = "user:${var.github_owner} topic:practice"
}

resource "github_repository_environment" "environments" {
# https://developer.hashicorp.com/terraform/language/meta-arguments/for_each
# local.repo_environments is a list of objects
# so we must project that into a map
# where each key is unique
for_each = tomap({
for env in local.repo_environments : "${env.repository} - ${env.environment}" => env
})

repository = each.value.repository
environment = each.value.environment
}
17 changes: 17 additions & 0 deletions resources.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
data "github_repositories" "my_topics" {
# https://docs.github.com/en/search-github/getting-started-with-searching-on-github/understanding-the-search-syntax
query = "user:${var.github_owner} topic:practice"
}

resource "github_repository_environment" "environments" {
# https://developer.hashicorp.com/terraform/language/meta-arguments/for_each
# local.repo_environments is a list of objects
# so we must project that into a map
# where each key is unique
for_each = tomap({
for env in local.repo_environments : "${env.repository} - ${env.environment}" => env
})

repository = each.value.repository
environment = each.value.environment
}

0 comments on commit 146fc4d

Please sign in to comment.