/
cibuild.erb
117 lines (104 loc) · 2.4 KB
/
cibuild.erb
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
#!/usr/bin/env bash
trap "exit" SIGINT SIGTERM
command -v docker >/dev/null 2>&1 && docker info >/dev/null || {
printf >&2 "\e[31mI require docker but it's not installed. Aborting.\e[0m\n"; exit 1;
}
DOCKER_COMPOSE_ARGS="-f docker-compose.ci.yml"
# Build Image
build(){
docker-compose $DOCKER_COMPOSE_ARGS build test
}
# Wait services to be ready
wait_services(){
function test_service {
docker-compose $DOCKER_COMPOSE_ARGS run --rm test sh -c "nc -z $1 $2"
}
count=0
# Chain tests together by using &&
until (
<% if(selected?(:database, :mysql) || selected?(:database, :postgresql))-%>
test_service '$DB_HOST' '$DB_PORT' && \
<% end-%>
echo "Services ready"
)
do
((count++))
if [ $count -gt 50 ]
then
echo "Services didn't become ready in time"
exit 1
else
echo "Waiting for services to become ready..."
fi
sleep 0.5
done
}
# Prepare dependencies
dependencies(){
docker-compose $DOCKER_COMPOSE_ARGS run --rm test bundle install
}
<% if selected?(:front_end, :vue) %>
yarn_dependencies(){
docker-compose $DOCKER_COMPOSE_ARGS run --rm test yarn install
}
<% end %>
assets() {
docker-compose $DOCKER_COMPOSE_ARGS run --rm test /bin/bash -c "bin/setup && bundle exec rake assets:precompile"
}
# Prepare database
database(){
docker-compose $DOCKER_COMPOSE_ARGS run --rm test bundle exec rake db:create db:schema:load
}
# Run the specs
tests(){
[ -n "$CI" ] && {
RSPEC_JUNIT_ARGS="-r rspec_junit_formatter --format RspecJunitFormatter -o $HOME/.rspec_reports/junit.xml"
RSPEC_FORMAT_ARGS="--format progress --no-color"
}
docker-compose $DOCKER_COMPOSE_ARGS run --rm test bundle exec rspec spec $RSPEC_FORMAT_ARGS $RSPEC_JUNIT_ARGS
}
<% if selected?(:front_end, :vue) %>
js_tests(){
docker-compose $DOCKER_COMPOSE_ARGS run --rm test yarn run test
}
<% end %>
# Run the complete ci build
no_ci(){
build
wait_services
dependencies
assets
database
tests
}
case "$1" in
"")
no_ci
;;
"services")
wait_services
;;
"deps"|"dependencies")
dependencies <% if selected?(:front_end, :vue) %>&& yarn_dependencies<% end %>
;;
"assets")
assets
;;
"db"|"database")
database
;;
<% if selected?(:front_end, :vue) %>
"js_tests")
js_tests
;;
<% end %>
"specs"|"tests")
tests
;;
"build")
build
;;
*)
echo "Usage: cibuild [services|deps|assets|db|specs|build]"
;;
esac