-
Notifications
You must be signed in to change notification settings - Fork 0
/
ruby.go
158 lines (134 loc) · 3.83 KB
/
ruby.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package internal
import (
"github.com/CircleCI-Public/circleci-config/config"
"github.com/CircleCI-Public/circleci-config/labeling/labels"
)
func GenerateRubyJobs(ls labels.LabelSet) (jobs []*Job) {
if !ls[labels.DepsRuby].Valid {
return nil
}
if hasGem(ls, "rake") {
jobs = append(jobs, rakeJob(ls))
} else if hasGem(ls, "rspec") {
jobs = append(jobs, rspecJob(ls))
} else if hasGem(ls, "rails") {
jobs = append(jobs, railsTestJob(ls))
}
return jobs
}
func hasGem(ls labels.LabelSet, gem string) bool {
for _, label := range []string{labels.DepsRuby, labels.PackageManagerGemspec} {
if ls[label].Valid == true && ls[label].LabelData.Dependencies[gem] != "" {
return true
}
}
return false
}
func rubyInitialSteps(ls labels.LabelSet) []config.Step {
checkout := checkoutStep(ls[labels.DepsRuby])
installDeps := config.Step{Type: config.OrbCommand, Command: "ruby/install-deps"}
// ruby orb requires Gemfile.lockfile, so revert to basic bundle command when not found
if !ls[labels.DepsRuby].LabelData.HasLockFile && ls[labels.PackageManagerGemspec].Valid == true {
installDeps = config.Step{Type: config.Run, Command: "bundle install"}
}
return []config.Step{checkout, installDeps}
}
const rubyOrb = "circleci/[email protected]"
const postgresImage = "circleci/postgres:9.5-alpine"
func rspecJob(ls labels.LabelSet) *Job {
steps := rubyInitialSteps(ls)
images := []string{rubyImageVersion(ls)}
if ls[labels.DepsRuby].Dependencies["pg"] == "true" {
images = append(images, postgresImage)
steps = append(steps,
config.Step{
Type: config.Run,
Name: "wait for DB",
Command: "dockerize -wait tcp://localhost:5432 -timeout 1m"},
config.Step{
Type: config.Run,
Name: "Database setup",
Command: "bundle exec rake db:test:prepare"})
}
if hasGem(ls, "rspec_junit_formatter") {
steps = append(steps,
config.Step{
Type: config.OrbCommand,
Name: "rspec test",
Command: "ruby/rspec-test"})
} else {
steps = append(steps,
config.Step{
Type: config.Run,
Name: "rspec test",
Command: "bundle exec rspec"})
}
return &Job{
Job: config.Job{
Name: "test-ruby",
Comment: "Install gems, run rspec tests",
Steps: steps,
DockerImages: images,
WorkingDirectory: workingDirectory(ls[labels.DepsRuby]),
Environment: map[string]string{
"RAILS_ENV": "test"},
},
Orbs: map[string]string{
"ruby": rubyOrb,
},
}
}
func rakeJob(ls labels.LabelSet) *Job {
steps := rubyInitialSteps(ls)
steps = append(steps,
config.Step{
Type: config.Run,
Name: "rake test",
Command: "bundle exec rake test",
})
return &Job{
Job: config.Job{
Name: "test-ruby",
Comment: "Install gems, run rake tests",
Steps: steps,
DockerImages: []string{rubyImageVersion(ls)},
WorkingDirectory: workingDirectory(ls[labels.DepsRuby]),
},
Orbs: map[string]string{
"ruby": rubyOrb,
},
}
}
func railsTestJob(ls labels.LabelSet) *Job {
steps := rubyInitialSteps(ls)
steps = append(steps,
config.Step{
Type: config.Run,
Name: "rails test",
Command: "bundle exec rails test",
})
return &Job{
Job: config.Job{
Name: "test-ruby",
Comment: "Install gems, run rails tests",
Steps: steps,
DockerImages: []string{rubyImageVersion(ls)},
WorkingDirectory: workingDirectory(ls[labels.DepsRuby]),
},
Orbs: map[string]string{
"ruby": rubyOrb,
},
}
}
const rubyFallbackVersion = "3.2"
// Construct the ruby image tag based on the ruby version
func rubyImageVersion(ls labels.LabelSet) string {
prefix := "cimg/ruby:"
suffix := "-node"
version := rubyFallbackVersion
gemfileVersion := ls[labels.DepsRuby].Dependencies["ruby"]
if gemfileVersion != "" {
version = gemfileVersion
}
return prefix + version + suffix
}