Skip to content

Commit 104493b

Browse files
authoredSep 5, 2023
Load reusable workflows next to the actions in use (devops-actions#65)
1 parent e04d7a9 commit 104493b

File tree

6 files changed

+103
-21
lines changed

6 files changed

+103
-21
lines changed
 

‎.env

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
GITHUB_ACTOR=debugging
2+
GITHUB_WORKSPACE=github_workspace
3+
GITHUB_OUTPUT=github_output_file

‎.gitignore

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Src/PowerShell/summarized-actions.json
2+
Src/PowerShell/github_output
3+
Src/PowerShell/used-actions.json
4+
Src/PowerShell/github_output_file
5+
Src/PowerShell/entrypoint.ps1
6+
Src/PowerShell/github_workspace/used-actions.json

‎Src/PowerShell/entrypoint.ps1

+42-5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,23 @@ function Get-LocationInfo {
1111
}
1212
}
1313

14+
function Import-EnvironmentVariables {
15+
# load the environment variables from the .env file in the root of the repo:
16+
Get-Content "../../.env" | ForEach-Object {
17+
$name, $value = $_.split('=')
18+
# if name already exists, do not overwrite it:
19+
if ($false -eq (Test-Path env:$name)) {
20+
if ($null -ne $value -and "" -ne $value) {
21+
Write-Host "Setting environment variable [$name] to [$value] from the .env file"
22+
Set-Content env:\$name $value
23+
}
24+
}
25+
else {
26+
Write-Host "Environment variable [$name] was already set. Value is [$($env:name)]"
27+
}
28+
}
29+
}
30+
1431
function main {
1532

1633
if ($null -eq $organization -or "" -eq $organization) {
@@ -25,15 +42,25 @@ function main {
2542

2643
$actions = (.\load-used-actions.ps1 -orgName $organization -PAT $PAT)
2744

28-
# wite the file outside of the container so we can pick it up
45+
# write the file outside of the container so we can pick it up
2946
Write-Host "Found [$($actions.Count)] actions "
3047
$jsonObject = ($actions | ConvertTo-Json -Depth 10 -Compress)
3148

3249
# store the json in a file and write the path to the output variable
3350
$fileName = "used-actions.json"
3451
$filePath = "$($env:GITHUB_WORKSPACE)/$fileName"
35-
36-
Set-Content -Value "$jsonObject" -Path "$filePath"
52+
53+
if ($null -ne $env:GITHUB_WORKSPACE -and "" -ne $env:GITHUB_WORKSPACE) {
54+
Write-Host "Writing actions to file in workspace: [$($env:GITHUB_WORKSPACE)]"
55+
Set-Content -Value "$jsonObject" -Path "$filePath"
56+
}
57+
else {
58+
Write-Host "Writing actions to file in current folder: [$($pwd)]"
59+
$filePath = "./used-actions.json"
60+
Set-Content -Value "$jsonObject" -Path "$filePath"
61+
}
62+
63+
# write the name of the file to the output folder
3764
Set-Content -Value "actions-file=$fileName" -Path $env:GITHUB_OUTPUT
3865
Write-Host "Stored actions in the actions output. Use $${{ steps.<step id>.outputs.actions }} in next action to load the json"
3966
Write-Host "Stored actions file in the actions output. Use $${{ steps.<step id>.outputs.actions-file }} in next action to load the file from the $$GITHUB_WORKSPACE folder"
@@ -42,20 +69,30 @@ function main {
4269
Add-Content -Value "actions='$jsonObject'" -Path $env:GITHUB_OUTPUT
4370
}
4471

45-
try {
72+
$currentLocation = Get-Location
73+
try {
4674
# always run in the correct location, where our scripts are located:
4775
Set-Location $PSScriptRoot
76+
Import-EnvironmentVariables
4877

4978
# call main script:
5079
main
5180

81+
Write-Host "Going back to location before the run: [$currentLocation]"
82+
Set-Location $currentLocation
83+
5284
# return the container with the exit code = Ok:
5385
exit 0
5486
}
5587
catch {
5688
# return the container with the last exit code:
89+
$exitError = $_
5790
Write-Error "Error loading the actions:"
58-
Write-Error $_
91+
Write-Error $exitError
92+
93+
Write-Host "Going back to location before the run: [$currentLocation]"
94+
Set-Location $currentLocation
95+
5996
# return the container with an erroneous exit code:
6097
exit 1
6198
}

‎Src/PowerShell/generic.ps1

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,30 @@
1+
$moduleName = "powershell-yaml"
12

23
# install a yaml parsing module (already done in the container image)
34
if($env:computername -ne "ROB-XPS9700") {
45
Write-Host "PSHOME: [$pshome]"
56

7+
# check if module is installed locally:
8+
$module = Get-Module -name $moduleName
9+
if ($null -eq $module) {
10+
Write-Host "Module [$moduleName] not found, installing it"
11+
Install-Module -Name $moduleName -Force -Scope CurrentUser -AllowClobber
12+
}
13+
else {
14+
Write-Host "Module [$moduleName] found, skipping installation"
15+
}
16+
617
# add back the root folder to the modules path because GitHub runner seems to overwite it
7-
$env:PSModulePath += ":/root/.local/share/powershell/Modules"
18+
# first check if module path already has this value:
19+
if ($false -eq ($env:PSModulePath -like "/root/.local/share/powershell/Modules")) {
20+
$env:PSModulePath += ":/root/.local/share/powershell/Modules"
21+
}
822

923
Write-Host "PSModulePath:"
1024
foreach ($path in $env:PSModulePath -split ':') {
1125
Write-Host "- [$path]"
1226
}
27+
1328
try {
1429
Write-Host "Importing module for the yaml parsing"
1530
Import-Module powershell-yaml -Force

‎Src/PowerShell/github-calls.ps1

+1-1
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ function GetRawFile {
314314
[string] $PAT
315315
)
316316

317-
Write-Host "Loading file content from url [$url]"
317+
Write-Host "Loading file content from url [$($url.Replace($PAT, "****")))]"
318318

319319
$Headers = Get-Headers -userName $userName -PAT $PAT
320320
$result = Invoke-WebRequest -Uri $url -Headers $Headers -Method Get -ErrorAction Stop | Select-Object -Expand Content

‎Src/PowerShell/load-used-actions.ps1

+35-14
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,36 @@ function GetActionsFromWorkflow {
4444
foreach ($job in $parsedYaml["jobs"].GetEnumerator()) {
4545
Write-Host " Job found: [$($job.Key)]"
4646
$steps=$job.Value.Item("steps")
47-
foreach ($step in $steps) {
48-
$uses=$step.Item("uses")
47+
if ($null -ne $steps) {
48+
foreach ($step in $steps) {
49+
$uses=$step.Item("uses")
50+
if ($null -ne $uses) {
51+
Write-Host " Found action used: [$uses]"
52+
$actionLink = $uses.Split("@")[0]
53+
54+
$data = [PSCustomObject]@{
55+
actionLink = $actionLink
56+
workflowFileName = $workflowFileName
57+
repo = $repo
58+
type = "action"
59+
}
60+
61+
$actions += $data
62+
}
63+
}
64+
}
65+
else {
66+
# check for reusable workflow
67+
$uses = $job.Value.Item("uses")
4968
if ($null -ne $uses) {
50-
Write-Host " Found action used: [$uses]"
69+
Write-Host " Found reusable workflow used: [$uses]"
5170
$actionLink = $uses.Split("@")[0]
5271

5372
$data = [PSCustomObject]@{
5473
actionLink = $actionLink
5574
workflowFileName = $workflowFileName
5675
repo = $repo
76+
type = "reusable workflow"
5777
}
5878

5979
$actions += $data
@@ -93,7 +113,7 @@ function GetAllUsedActionsFromRepo {
93113
}
94114
catch {
95115
Write-Warning "Error handling this workflow file:"
96-
Write-Host $workflowFile | ConvertFrom-Json -Depth 10
116+
Write-Host $workflowFile.Replace($PAT, "****") | ConvertFrom-Json -Depth 10
97117
Write-Warning "----------------------------------"
98118
Write-Host "Error: [$_]"
99119
Write-Warning "----------------------------------"
@@ -110,9 +130,9 @@ function SummarizeActionsUsed {
110130

111131
$summarized = @()
112132
foreach ($action in $actions) {
113-
$found = $summarized | Where-Object { $_.actionLink -eq $action.actionLink }
133+
$found = $summarized | Where-Object { $_.actionLink -eq $action.actionLink -And $_.type -eq $action.type }
114134
if ($null -ne $found) {
115-
# action already found, add this info to it
135+
# item already found, add this info to it
116136
$newInfo = [PSCustomObject]@{
117137
repo = $action.repo
118138
workflowFileName = $action.workflowFileName
@@ -122,16 +142,17 @@ function SummarizeActionsUsed {
122142
$found.count++
123143
}
124144
else {
125-
# new action, create a new object
145+
# new item, create a new object
126146
$newItem = [PSCustomObject]@{
147+
type = $action.type
127148
actionLink = $action.actionLink
128149
count = 1
129150
workflows = @(
130151
[PSCustomObject]@{
131152
repo = $action.repo
132153
workflowFileName = $action.workflowFileName
133154
}
134-
)
155+
)
135156
}
136157
$summarized += $newItem
137158
}
@@ -150,7 +171,7 @@ function LoadAllUsedActionsFromRepos {
150171

151172
# create hastable
152173
$actions = @()
153-
$i=0
174+
#$i=0
154175
foreach ($repo in $repos) {
155176
if ($null -ne $repo -And $repo.full_name.Length -gt 0) {
156177
Write-Host "Loading actions from repo: [$($repo.full_name)]"
@@ -160,12 +181,12 @@ function LoadAllUsedActionsFromRepos {
160181

161182
# comment out code below to stop after a certain number of repos to prevent issues with
162183
# rate limiting on the load file count (that is not workin correctly)
163-
164-
#$i++
165-
#if ($i -eq 2) {
166-
# # break out on second result:
184+
# $i++
185+
# if ($i -eq 2) {
186+
# # break on second result:
187+
# Write-Host "Breaking after [$i] repos"
167188
# return $actions
168-
#}
189+
# }
169190
}
170191
}
171192

0 commit comments

Comments
 (0)
Please sign in to comment.