-
Notifications
You must be signed in to change notification settings - Fork 1
/
.gitlab-ci.yml
156 lines (135 loc) · 4.8 KB
/
.gitlab-ci.yml
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
image: guillaumebriday/ruby-node:2.6.3
stages:
- test
- build
- review
- deploy
variables:
POSTGRES_DB: app_production
IMAGE_TAG: guillaumebriday/rails-tailwindcss-starter:$CI_COMMIT_REF_SLUG
# Cache gems in between builds
cache:
paths:
- vendor/ruby
- node_modules
# Stage: Test
rubocop:
stage: test
script:
- bundle install -j $(nproc) --path vendor --quiet # Install dependencies into ./vendor/ruby
- bundle exec rubocop
tests:
stage: test
variables:
POSTGRES_DB: rails-tailwindcss-starter_test
DATABASE_URL: "postgresql://postgres:postgres@postgres:5432/$POSTGRES_DB"
RAILS_ENV: test
services:
- postgres:11-alpine
before_script:
- gem install bundler --no-document
- bundle install -j $(nproc) --path vendor --quiet # Install dependencies into ./vendor/ruby
script:
- bundle exec rails db:migrate
- bundle exec rails db:seed
- bundle exec rake assets:precompile
- bundle exec rails test
# Stage: Build
build_image:
stage: build
image: docker:stable
services:
- docker:dind
script:
- docker login -u ${CI_REGISTRY_USER} -p ${CI_REGISTRY_PASSWORD} ${CI_REGISTRY}
- echo "${RAILS_MASTER_KEY}" > config/master.key
- docker build -f .cloud/docker/Dockerfile -t ${IMAGE_TAG} .
- docker push $IMAGE_TAG
# Stage: review
.setup_deploy_env: &setup_deploy_env
image: kroniak/ssh-client
before_script:
- eval $(ssh-agent -s)
- echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null
- mkdir -p ~/.ssh
- chmod 700 ~/.ssh
- ssh-keyscan -H 'guillaumebriday.xyz' >> ~/.ssh/known_hosts
review_app:
<<: *setup_deploy_env
stage: review
script:
# log into Docker registry
- ssh [email protected] "docker login -u ${CI_REGISTRY_USER} -p ${CI_REGISTRY_PASSWORD} ${CI_REGISTRY}"
- ssh [email protected] "docker pull ${IMAGE_TAG}"
# stop containers, remove image.
- ssh [email protected] "docker stop app_${CI_COMMIT_REF_SLUG} db_${CI_COMMIT_REF_SLUG}" || true
# start new containers
- ssh [email protected] \
"docker run --rm --name db_${CI_COMMIT_REF_SLUG} -e POSTGRES_DB=${POSTGRES_DB} -e POSTGRES_PASSWORD=secret -d postgres:11"
- ssh [email protected] \
"docker run --rm --name app_${CI_COMMIT_REF_SLUG} --link db_${CI_COMMIT_REF_SLUG} -e DB_HOST=db_${CI_COMMIT_REF_SLUG} -d -l traefik.frontend.rule=Host:${CI_COMMIT_REF_SLUG}.guillaumebriday.xyz ${IMAGE_TAG}"
only:
- branches
except:
- master
environment:
name: review/$CI_COMMIT_REF_SLUG
url: https://$CI_COMMIT_REF_SLUG.guillaumebriday.xyz/
on_stop: stop_review_app
stop_review_app:
<<: *setup_deploy_env
stage: review
variables:
GIT_STRATEGY: none
script:
- ssh [email protected] "docker stop app_${CI_COMMIT_REF_SLUG} db_${CI_COMMIT_REF_SLUG}" || true
- ssh [email protected] "docker rmi ${IMAGE_TAG}" || true
when: manual
only:
- branches
except:
- master
environment:
name: review/$CI_COMMIT_REF_SLUG
action: stop
# Stage: deploy
deploy_staging:
<<: *setup_deploy_env
stage: deploy
script:
# log into Docker registry
- ssh [email protected] "docker login -u ${CI_REGISTRY_USER} -p ${CI_REGISTRY_PASSWORD} ${CI_REGISTRY}"
- ssh [email protected] "docker pull ${IMAGE_TAG}"
# stop containers, remove image.
- ssh [email protected] "docker stop app_staging db_staging" || true
# start new containers
- ssh [email protected] \
"docker run --rm --name db_staging -e POSTGRES_DB=${POSTGRES_DB} -e POSTGRES_PASSWORD=secret -d postgres:11"
- ssh [email protected] \
"docker run --rm --name app_staging --link db_staging -e DB_HOST=db_staging -d -l traefik.frontend.rule=Host:staging.guillaumebriday.xyz ${IMAGE_TAG}"
only:
- master
when: manual
environment:
name: staging
url: https://staging.guillaumebriday.xyz/
deploy_prod:
<<: *setup_deploy_env
stage: deploy
script:
# log into Docker registry
- ssh [email protected] "docker login -u ${CI_REGISTRY_USER} -p ${CI_REGISTRY_PASSWORD} ${CI_REGISTRY}"
- ssh [email protected] "docker pull ${IMAGE_TAG}"
# stop containers, remove image.
- ssh [email protected] "docker stop app_production db_production" || true
# start new containers
- ssh [email protected] \
"docker run --rm --name db_production -e POSTGRES_DB=${POSTGRES_DB} -e POSTGRES_PASSWORD=secret -d postgres:11"
- ssh [email protected] \
"docker run --rm --name app_production --link db_production -e DB_HOST=db_production -d -l traefik.frontend.rule=Host:rails-tailwindcss-starter.guillaumebriday.xyz ${IMAGE_TAG}"
only:
- master
when: manual
environment:
name: production
url: https://rails-tailwindcss-starter.guillaumebriday.xyz/