Skip to content

Commit 8fd0f4a

Browse files
committed
FileConventions(Test),scripts: acknowledge .env
Take .env file into account in inconsistentVersionsInGitHubCI.fsx script when searching for vars referenced in Github workflow files, when .env file is present in the repository root. This is needed because workflows in pulumi-deploy now load vars from .env file.
1 parent 4f1c145 commit 8fd0f4a

File tree

2 files changed

+55
-21
lines changed

2 files changed

+55
-21
lines changed

scripts/inconsistentVersionsInGitHubCI.fsx

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,29 @@ let targetDir =
3333
|> fst
3434

3535
let inconsistentVersionsInGitHubCI =
36+
let repositoryRootDir = targetDir.Parent.Parent
37+
38+
let globalEnv =
39+
let dotEnvFile =
40+
Path.Join(repositoryRootDir.FullName, ".env") |> FileInfo
41+
42+
if dotEnvFile.Exists then
43+
let lines = File.ReadAllLines dotEnvFile.FullName
44+
45+
lines
46+
|> Seq.filter(fun line ->
47+
not(
48+
System.String.IsNullOrWhiteSpace line || line.StartsWith '#'
49+
)
50+
)
51+
|> Seq.map(fun line ->
52+
let [| key; value |] = line.Split('=', count = 2)
53+
key.Trim(), value.Trim()
54+
)
55+
|> Map.ofSeq
56+
else
57+
Map.empty
58+
3659
FileConventions.DetectInconsistentVersionsInGitHubCI targetDir globalEnv
3760

3861
if inconsistentVersionsInGitHubCI then

src/FileConventions/Library.fs

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ let private GetVersionsMapFromFiles
362362
let private DetectInconsistentVersionsInYamlFiles
363363
(fileInfos: seq<FileInfo>)
364364
(extractVersionsFunction: YamlNode -> seq<string * string>)
365-
(_globalEnv: Map<string, string>)
365+
(globalEnv: Map<string, string>)
366366
=
367367
let envVarRegex =
368368
Regex(@"\s*\$\{\{\s*([^\s\}]+)\s*\}\}\s*", RegexOptions.Compiled)
@@ -383,33 +383,44 @@ let private DetectInconsistentVersionsInYamlFiles
383383
let matches =
384384
Seq.collect extractVersionsFunction yamlDoc.AllNodes
385385

386+
let yamlDict = yamlDoc :?> YamlMappingNode
387+
388+
let localEnv =
389+
match yamlDict.Children.TryGetValue "env" with
390+
| true, (:? YamlMappingNode as node) -> node
391+
| _ -> YamlMappingNode()
392+
393+
let envDict =
394+
localEnv.Children
395+
|> Seq.fold
396+
(fun acc pair ->
397+
acc
398+
|> Map.add
399+
(pair.Key :?> YamlScalarNode).Value
400+
(pair.Value :?> YamlScalarNode).Value
401+
)
402+
globalEnv
403+
386404
matches
387405
|> Seq.fold
388406
(fun acc (key, value) ->
389407
let actualValue =
390408
let variableRegexMatch = envVarRegex.Match value
391409

392410
if variableRegexMatch.Success then
393-
let yamlDict = yamlDoc :?> YamlMappingNode
394-
395-
match yamlDict.Children.TryGetValue "env" with
396-
| true, (:? YamlMappingNode as envDict) ->
397-
let referenceString =
398-
variableRegexMatch.Groups.[1].Value
399-
400-
let envVarName =
401-
if referenceString.StartsWith "env." then
402-
referenceString.[4..]
403-
else
404-
referenceString
405-
406-
match
407-
envDict.Children.TryGetValue envVarName
408-
with
409-
| true, envVarValue ->
410-
(envVarValue :?> YamlScalarNode).Value
411-
| false, _ -> value
412-
| _ -> value
411+
let referenceString =
412+
variableRegexMatch.Groups.[1].Value
413+
414+
let envVarName =
415+
if referenceString.StartsWith "env." then
416+
referenceString.[4..]
417+
else
418+
referenceString
419+
420+
match envDict.TryGetValue envVarName with
421+
| true, envVarValue -> envVarValue
422+
| false, _ ->
423+
failwithf "env. var %s not found" envVarName
413424
else
414425
value
415426

0 commit comments

Comments
 (0)