Skip to content

Commit 8203be8

Browse files
committed
Add support for conditions
1 parent 6fd80bc commit 8203be8

File tree

8 files changed

+45
-5
lines changed

8 files changed

+45
-5
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,11 @@ jobs:
3636
- name: Run tests
3737
stage: test
3838
image: "docker.io/golang:1.21.3"
39+
variables:
40+
- TEST: true
3941
script:
4042
- go test ./...
43+
condition: TEST
4144

4245
- name: Run checks
4346
stage: security

cmd/dot/root.go

+32-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"strings"
99
"time"
1010

11+
"github.com/expr-lang/expr"
1112
"github.com/go-playground/validator/v10"
1213
"github.com/opnlabs/dot/pkg/artifacts"
1314
"github.com/opnlabs/dot/pkg/models"
@@ -44,7 +45,7 @@ concurrently.`,
4445
log.Fatalf("variables should be defined as KEY=VALUE: %s", v)
4546
}
4647

47-
m := make(map[string]string)
48+
m := make(map[string]any)
4849
m[variables[0]] = variables[1]
4950
environmentVariables = append(environmentVariables, m)
5051
}
@@ -98,7 +99,36 @@ func run() {
9899
if _, ok := stageMap[v.Stage]; !ok {
99100
log.Fatalf("stage not defined: %s", v.Stage)
100101
}
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+
102132
}
103133

104134
dockerArtifactManager := artifacts.NewDockerArtifactsManager(".artifacts")

dot.yml

+3
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@ jobs:
77
- name: Run tests
88
stage: test
99
image: "docker.io/golang:1.21.3"
10+
variables:
11+
- TEST: false
1012
script:
1113
- go test ./...
14+
condition: TEST
1215

1316
- name: Run checks
1417
stage: security

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ require (
2121
github.com/docker/distribution v2.8.3+incompatible // indirect
2222
github.com/docker/go-connections v0.4.0 // indirect
2323
github.com/docker/go-units v0.5.0 // indirect
24+
github.com/expr-lang/expr v1.15.7 // indirect
2425
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
2526
github.com/go-playground/locales v0.14.1 // indirect
2627
github.com/go-playground/universal-translator v0.18.1 // indirect

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ github.com/docker/go-connections v0.4.0 h1:El9xVISelRB7BuFusrZozjnkIM5YnzCViNKoh
1616
github.com/docker/go-connections v0.4.0/go.mod h1:Gbd7IOopHjR8Iph03tsViu4nIes5XhDvyHbTtUxmeec=
1717
github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4=
1818
github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
19+
github.com/expr-lang/expr v1.15.7 h1:BK0JcWUkoW6nrbLBo6xCKhz4BvH5DSOOu1Gx5lucyZo=
20+
github.com/expr-lang/expr v1.15.7/go.mod h1:uCkhfG+x7fcZ5A5sXHKuQ07jGZRl6J0FCAaf2k4PtVQ=
1921
github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs=
2022
github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw=
2123
github.com/gabriel-vasile/mimetype v1.4.2 h1:w5qFW6JKBz9Y393Y4q372O9A7cUSequkh1Q7OhCmWKU=

pkg/models/models.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package models
22

33
type Stage string
4-
type Variable map[string]string
4+
type Variable map[string]any
55

66
type JobFile struct {
77
Stages []Stage `yaml:"stages" validate:"required,dive"`
@@ -17,4 +17,5 @@ type Job struct {
1717
Script []string `yaml:"script"`
1818
Entrypoint []string `yaml:"entrypoint"`
1919
Artifacts []string `yaml:"artifacts"`
20+
Condition string `yaml:"condition"`
2021
}

pkg/runner/docker.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func (d *DockerRunner) WithEnv(env []models.Variable) *DockerRunner {
9191
log.Fatal("variables should be defined as a key value pair")
9292
}
9393
for k, v := range v {
94-
variables = append(variables, fmt.Sprintf("%s=%s", k, v))
94+
variables = append(variables, fmt.Sprintf("%s=%s", k, fmt.Sprint(v)))
9595
}
9696
}
9797
d.env = variables

pkg/runner/docker_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func TestRun(t *testing.T) {
7272
Manager: manager,
7373
Image: "docker.io/alpine",
7474
Variables: []models.Variable{
75-
map[string]string{
75+
map[string]any{
7676
"TESTING_VARIABLE": "TESTING",
7777
},
7878
},

0 commit comments

Comments
 (0)