🔧 create github pull script generator
This commit is contained in:
parent
82aa1cc4fd
commit
996beb2558
3 changed files with 102 additions and 0 deletions
terraform/jobrad
3
terraform/jobrad/.gitignore
vendored
Normal file
3
terraform/jobrad/.gitignore
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
repositories.*
|
||||
|
||||
*.tfstate*
|
81
terraform/jobrad/github.tf
Normal file
81
terraform/jobrad/github.tf
Normal file
|
@ -0,0 +1,81 @@
|
|||
terraform {
|
||||
required_providers {
|
||||
github = {
|
||||
source = "integrations/github"
|
||||
version = "~> 5.0"
|
||||
}
|
||||
local = {
|
||||
source = "hashicorp/local"
|
||||
version = "~> 2.0"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# Configure the GitHub Provider
|
||||
provider "github" {
|
||||
token = var.github_token
|
||||
owner = var.github_organization
|
||||
}
|
||||
|
||||
# Variable for GitHub token
|
||||
variable "github_token" {
|
||||
description = "GitHub personal access token"
|
||||
type = string
|
||||
sensitive = true
|
||||
}
|
||||
|
||||
# Variable for GitHub organization
|
||||
variable "github_organization" {
|
||||
description = "GitHub organization name"
|
||||
type = string
|
||||
}
|
||||
|
||||
# Variable for output file path
|
||||
variable "output_json_path" {
|
||||
description = "Path where the JSON file will be created"
|
||||
type = string
|
||||
default = "./repositories.json"
|
||||
}
|
||||
|
||||
# Variable for output file path
|
||||
variable "output_script_path" {
|
||||
description = "Path where the clone Script file will be created"
|
||||
type = string
|
||||
default = "./repositories.sh"
|
||||
}
|
||||
|
||||
# Data source to get all repositories in the organization
|
||||
data "github_repositories" "org_repos" {
|
||||
query = "org:${var.github_organization}"
|
||||
}
|
||||
|
||||
# Create local JSON file with repository information
|
||||
resource "local_file" "repositories_json" {
|
||||
filename = var.output_json_path
|
||||
content = jsonencode(data.github_repositories.org_repos.full_names)
|
||||
}
|
||||
|
||||
# Create local Script file with repository information
|
||||
resource "local_file" "repositories_script" {
|
||||
filename = var.output_script_path
|
||||
content = <<-EOF
|
||||
cd ~/dev
|
||||
|
||||
%{for repo in sort(data.github_repositories.org_repos.full_names) ~}
|
||||
git clone ${repo}
|
||||
%{endfor}
|
||||
EOF
|
||||
}
|
||||
|
||||
|
||||
# Output the file path
|
||||
output "output_file_path" {
|
||||
description = "Path to the generated JSON file"
|
||||
value = local_file.repositories_json.filename
|
||||
}
|
||||
|
||||
# Output repository count
|
||||
output "repository_count" {
|
||||
description = "Total number of repositories found"
|
||||
value = length(data.github_repositories.org_repos.names)
|
||||
}
|
18
terraform/jobrad/terragrunt.hcl
Normal file
18
terraform/jobrad/terragrunt.hcl
Normal file
|
@ -0,0 +1,18 @@
|
|||
inputs = {
|
||||
github_token = get_env("GITHUB_TOKEN", "")
|
||||
github_organization = "JobRad-SRE"
|
||||
output_json_path = "./repositories.json"
|
||||
}
|
||||
|
||||
# Optional: Add validation to ensure GITHUB_TOKEN is set
|
||||
locals {
|
||||
github_token = get_env("GITHUB_TOKEN", "")
|
||||
}
|
||||
|
||||
# Hook to validate environment variable
|
||||
terraform {
|
||||
before_hook "validate_github_token" {
|
||||
commands = ["plan", "apply"]
|
||||
execute = ["bash", "-c", "if [ -z \"$GITHUB_TOKEN\" ]; then echo 'Error: GITHUB_TOKEN environment variable is not set'; exit 1; fi"]
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue