How to: Variables Mapping on GitHub

Vinicius Moura
3 min readMar 14, 2023

--

This script and report extract all variables (organization, repositories, and environments) within the GitHub organization

Many customers on GitHub use variables to store shared information in your organization, repository, or repository environments in GitHub Actions workflows. Thinking about it, this report maps all variables between these three levels (organization, repositories, and environments) and presents them below.

An original script is available on my GitHub repository. See below this script:

Let’s go understand each command used.

  1. The PowerShell script will receive the following parameters:
  • $PAT = Personal Access token to connect on GitHub organization;
  • $Organization = GitHub Organization name;
  • $Connstr = connection string to Azure SQL Database that stores the report information. To create this report, it’s necessary to create previously a Azure SQL Server and Database and run the script below:

2. Get organization = use this REST API to get organization id through the organization name parameter

$UriOrganization = "https://api.github.com/orgs/$($organization)"
$OrganizationResult = Invoke-RestMethod -Headers $headers -Uri $UriOrganization
Write-Host $OrganizationResult.id

3. List organization variables = use this REST API to list all variables available in an organization

#Organization Variables
$uriOrganizationVariables = "$($UriOrganization)/actions/variables"
$OrganizationVariablesResult = Invoke-RestMethod -Headers $headers -Uri $uriOrganizationVariables
foreach ($orgVariable in $OrganizationVariablesResult.variables)
{
Write-Host $orgVariable.name
}

4. List organization repositories = use this REST API to list repositories for the specified organization

$uriRepositories = "$($UriOrganization)/repos"
$RepositoriesResult = Invoke-RestMethod -Headers $headers -Uri $uriRepositories
foreach ($repo in $RepositoriesResult)
{
Write-Host $repo.name
}

5. List repositories variables = use this REST API to list all variables available on each repository

$uriRepositoriesVariables = "$($UriRepositoriesOwner)/$($repo.name)/actions/variables"
$RepositoriesVariablesResult = Invoke-RestMethod -Headers $headers -Uri $uriRepositoriesVariables
foreach ($repoVariable in $RepositoriesVariablesResult.variables)
{
Write-Host $repoVariable.name
}

6. List repositories environments = use this REST API to list all environments on each repository

$uriRepositoriesEnvironments = "$($UriRepositoriesOwner)/$($repo.name)/environments"    
$RepositoriesEnvironmentsResult = Invoke-RestMethod -Headers $headers -Uri $uriRepositoriesEnvironments
foreach ($repoenvironment in $RepositoriesEnvironmentsResult.environments)
{
Write-Host $repoenvironment.name
}

7. List environment variables = use this REST API to list all variables available in an environment

$uriRepositoriesEnvironmentsVariables = "https://api.github.com/repositories/$($repo.id)/environments/$($repoenvironment.name)/variables"
$RepositoriesEnvironmentsVariables = Invoke-RestMethod -Headers $headers -Uri $uriRepositoriesEnvironmentsVariables
foreach ($repoenvironmentvariable in $RepositoriesEnvironmentsVariables.variables)
{
Write-Host $repoenvironmentvariable.name
}

8. After extracting all variables (organization, repositories, and environments), this information is stored in a table in Azure SQL.

9. After inserting information into a table, I connect this database to Power BI:

  • Repositories (1) = Filter report using Repositories field;
  • Environments (2) = Filter report using Environments field;
  • Variables Name (3) = Filter report using Variable name field;
  • Organization Variables (4) = list all organization variables;
  • Repositories Variables (5) = List all information about repositories and variables. If the respective variable is mapped on a repository, the same will be marked (green ticks on a grid);
  • Repositories Environments Variables (6) = List all information about repositories, environments, and variables. If the respective variable (rows) is mapped on a repository and environment (columns), the same will be marked (green ticks on a grid).

--

--