|
8 | 8 | "strings"
|
9 | 9 | "time"
|
10 | 10 |
|
| 11 | + "github.com/expr-lang/expr" |
11 | 12 | "github.com/go-playground/validator/v10"
|
12 | 13 | "github.com/opnlabs/dot/pkg/artifacts"
|
13 | 14 | "github.com/opnlabs/dot/pkg/models"
|
@@ -44,7 +45,7 @@ concurrently.`,
|
44 | 45 | log.Fatalf("variables should be defined as KEY=VALUE: %s", v)
|
45 | 46 | }
|
46 | 47 |
|
47 |
| - m := make(map[string]string) |
| 48 | + m := make(map[string]any) |
48 | 49 | m[variables[0]] = variables[1]
|
49 | 50 | environmentVariables = append(environmentVariables, m)
|
50 | 51 | }
|
@@ -98,7 +99,36 @@ func run() {
|
98 | 99 | if _, ok := stageMap[v.Stage]; !ok {
|
99 | 100 | log.Fatalf("stage not defined: %s", v.Stage)
|
100 | 101 | }
|
101 |
| - stageMap[v.Stage] = append(stageMap[v.Stage], v) |
| 102 | + |
| 103 | + // Create expr program with the variables passed as env |
| 104 | + if len(v.Condition) == 0 { |
| 105 | + v.Condition = `true` |
| 106 | + } |
| 107 | + |
| 108 | + env := make(map[string]any) |
| 109 | + for _, entries := range v.Variables { |
| 110 | + if len(entries) > 1 { |
| 111 | + log.Fatal("variables should be defined as a key value pair") |
| 112 | + } |
| 113 | + for k, value := range entries { |
| 114 | + env[k] = value |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + p, err := expr.Compile(v.Condition, expr.Env(env), expr.AsBool()) |
| 119 | + if err != nil { |
| 120 | + log.Fatalf("condition evaluation failed for job %s: %v", v.Name, err) |
| 121 | + } |
| 122 | + output, err := expr.Run(p, env) |
| 123 | + if err != nil { |
| 124 | + log.Fatalf("condition evaluation failed for job %s: %v", v.Name, err) |
| 125 | + } |
| 126 | + |
| 127 | + // Only append to stageMap if the condition evaluates to true |
| 128 | + if output.(bool) { |
| 129 | + stageMap[v.Stage] = append(stageMap[v.Stage], v) |
| 130 | + } |
| 131 | + |
102 | 132 | }
|
103 | 133 |
|
104 | 134 | dockerArtifactManager := artifacts.NewDockerArtifactsManager(".artifacts")
|
|
0 commit comments