Skip to content

Commit

Permalink
feat: implement GitHub deploy keys generation and add output for gene…
Browse files Browse the repository at this point in the history
…rated keys
  • Loading branch information
Dave Arnold committed Apr 1, 2025
1 parent 5e34ff0 commit 1a4ac48
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
23 changes: 18 additions & 5 deletions github_deploy_keys.tf
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
// This file implements GitHub Deploy Keys functionality for the repository
// Generate SSH keys when create is true
resource "tls_private_key" "deploy_key" {
for_each = {
for k, v in var.deploy_keys : k => v
if v.create == true
}

algorithm = "RSA"
rsa_bits = 4096
}

// Create GitHub deploy keys for all entries
resource "github_repository_deploy_key" "deploy_key" {
for_each = { for k, v in var.deploy_keys : k => v }
for_each = {
for k, v in var.deploy_keys : k => v
}

title = each.value.title
repository = local.github_repo.name
key = each.value.key
key = each.value.create ? tls_private_key.deploy_key[each.key].public_key_openssh : each.value.key
read_only = each.value.read_only

depends_on = [
github_repository.repo
github_repository.repo,
data.github_repository.existing
]
}
}
9 changes: 9 additions & 0 deletions outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,12 @@ output "template" {
description = "Template repository this repository was created from"
value = local.github_repo.template
}


output "generated_deploy_keys" {
description = "Generated private keys for deploy keys with create=true"
value = {
for k, v in tls_private_key.deploy_key : k => v.private_key_pem
}
sensitive = true
}
6 changes: 5 additions & 1 deletion variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -413,8 +413,12 @@ variable "deploy_keys" {
description = "List of SSH deploy keys to add to the repository"
type = list(object({
title = string
key = string
key = optional(string, "")
# The key is optional because it can be generated
# by the module itself if create is set to true
# and the key is not provided
read_only = optional(bool, true)
create = optional(bool, false)
}))
default = []
}

0 comments on commit 1a4ac48

Please sign in to comment.