Skip to content

Commit f9c5cd2

Browse files
Merge pull request #213 from 18F/stage
Creating production workflow
2 parents d89937d + 9015518 commit f9c5cd2

File tree

8 files changed

+334
-48
lines changed

8 files changed

+334
-48
lines changed

.circleci/config.yml

Lines changed: 218 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
version: 2
1+
version: 2.1
22
jobs:
3-
build:
3+
develop_deploy:
44
docker:
55
- image: cimg/node:16.19.1-browsers
66
environment:
@@ -11,23 +11,233 @@ jobs:
1111
POSTGRES_DB: analytics-api-test
1212
steps:
1313
- checkout
14+
1415
- restore_cache:
1516
keys:
1617
- v1-dependencies-{{ checksum "package.json" }}
1718
- v1-dependencies-
19+
1820
- run:
1921
name: install dependencies
2022
command: npm install
23+
2124
- save_cache:
2225
paths:
2326
- ./node_modules
2427
key: v1-dependencies-{{ checksum "package.json" }}
28+
2529
- run:
26-
name: run tests
27-
command: npm test
30+
name: run tests
31+
command: npm test
32+
2833
- run:
29-
name: deploy site
34+
name: Replace Data URL in manifest.yml file when deploying to develop
3035
command: |
31-
if [ "${CIRCLE_BRANCH}" == "master" ]; then
32-
bin/deploy-ci.sh
33-
fi
36+
sudo apt-get update
37+
sudo apt-get install gettext
38+
sed -i 's@name: analytics-reporter-api@name: analytics-reporter-api-develop@g' manifest.yml
39+
sed -i 's@- analytics-reporter-database@- analytics-reporter-database-develop@g' manifest.yml
40+
mv manifest.yml manifest.yml.src
41+
envsubst < manifest.yml.src > manifest.yml
42+
43+
- run:
44+
name: Run sed on entrypoint.sh when deploying to develop
45+
command: |
46+
sed -i 's@NEW_RELIC_APP_NAME="analytics-reporter-api"@NEW_RELIC_APP_NAME="analytics-reporter-api-develop"@g' entrypoint.sh
47+
48+
- run:
49+
name: Delete Config.js and drop sufffix on Config.js.cloudgov
50+
command: |
51+
rm ./src/config.js
52+
mv ./src/config.js.cloudgov ./src/config.js
53+
rm knexfile.js
54+
mv knexfile.js.cloudgov knexfile.js
55+
56+
- run:
57+
name: Install CF CLI
58+
command: |
59+
sudo curl -v -L -o cf8-cli-installer_8.7.4_x86-64.deb 'https://packages.cloudfoundry.org/stable?release=debian64&version=8.7.4'
60+
sudo dpkg -i cf8-cli-installer_8.7.4_x86-64.deb
61+
62+
- run:
63+
name: deploy
64+
command: |
65+
set -e
66+
# Log into cloud.gov
67+
cf api api.fr.cloud.gov
68+
cf login -u $CF_USERNAME_DEV -p $CF_PASSWORD_DEV -o gsa-opp-analytics -s analytics-dev
69+
cat manifest.yml
70+
cf push -f "./manifest.yml"
71+
cf set-env analytics-reporter-api-develop API_DATA_GOV_SECRET "$API_SECRET"
72+
cf restage analytics-reporter-api-develop
73+
cf logout
74+
75+
staging_deploy:
76+
docker:
77+
- image: cimg/node:16.19.1-browsers
78+
environment:
79+
POSTGRES_USER: postgres
80+
NODE_ENV: test
81+
- image: circleci/postgres:9.5-alpine
82+
environment:
83+
POSTGRES_DB: analytics-api-test
84+
steps:
85+
- checkout
86+
87+
- restore_cache:
88+
keys:
89+
- v1-dependencies-{{ checksum "package.json" }}
90+
- v1-dependencies-
91+
92+
- run:
93+
name: install dependencies
94+
command: npm install
95+
96+
- save_cache:
97+
paths:
98+
- ./node_modules
99+
key: v1-dependencies-{{ checksum "package.json" }}
100+
101+
- run:
102+
name: run tests
103+
command: npm test
104+
105+
- run:
106+
name: Replace Data URL in manifest.yml file when deploying to staging
107+
command: |
108+
sudo apt-get update
109+
sudo apt-get install gettext
110+
sed -i 's@name: analytics-reporter-api@name: analytics-reporter-api-staging@g' manifest.yml
111+
sed -i 's@- analytics-reporter-database@- analytics-reporter-database-staging@g' manifest.yml
112+
mv manifest.yml manifest.yml.src
113+
envsubst < manifest.yml.src > manifest.yml
114+
115+
- run:
116+
name: Run sed on entrypoint.sh when deploying to staging
117+
command: |
118+
sed -i 's@NEW_RELIC_APP_NAME="analytics-reporter-api"@NEW_RELIC_APP_NAME="analytics-reporter-api-staging"@g' entrypoint.sh
119+
120+
- run:
121+
name: Delete Knexfile.js and drop sufffix on Knexfile.js.cloudgov
122+
command: |
123+
rm knexfile.js
124+
mv knexfile.js.cloudgov knexfile.js
125+
rm ./src/config.js
126+
mv ./src/config.js.cloudgov ./src/config.js
127+
128+
- run:
129+
name: Install CF CLI
130+
command: |
131+
sudo curl -v -L -o cf8-cli-installer_8.7.4_x86-64.deb 'https://packages.cloudfoundry.org/stable?release=debian64&version=8.7.4'
132+
sudo dpkg -i cf8-cli-installer_8.7.4_x86-64.deb
133+
134+
- run:
135+
name: deploy
136+
command: |
137+
set -e
138+
# Log into cloud.gov
139+
cf api api.fr.cloud.gov
140+
cf login -u $CF_STAGING_SPACE_DEPLOYER_USERNAME -p $CF_STAGING_SPACE_DEPLOYER_PASSWORD -o gsa-opp-analytics -s analytics-staging
141+
cat manifest.yml
142+
cf push -f "./manifest.yml"
143+
cf set-env analytics-reporter-api-staging API_DATA_GOV_SECRET "$API_SECRET"
144+
cf restage analytics-reporter-api-staging
145+
cf logout
146+
147+
main_deploy:
148+
docker:
149+
- image: cimg/node:16.19.1-browsers
150+
environment:
151+
POSTGRES_USER: postgres
152+
NODE_ENV: test
153+
- image: circleci/postgres:9.5-alpine
154+
environment:
155+
POSTGRES_DB: analytics-api-test
156+
steps:
157+
- checkout
158+
159+
- restore_cache:
160+
keys:
161+
- v1-dependencies-{{ checksum "package.json" }}
162+
- v1-dependencies-
163+
164+
- run:
165+
name: install dependencies
166+
command: npm install
167+
168+
- save_cache:
169+
paths:
170+
- ./node_modules
171+
key: v1-dependencies-{{ checksum "package.json" }}
172+
173+
- run:
174+
name: run tests
175+
command: npm test
176+
177+
- run:
178+
name: Replace Data URL in manifest.yml file when deploying to production
179+
command: |
180+
sudo apt-get update
181+
sudo apt-get install gettext
182+
sed -i 's@name: analytics-reporter-api@name: analytics-reporter-api-production@g' manifest.yml
183+
sed -i 's@- analytics-reporter-database@- analytics-reporter-database-production@g' manifest.yml
184+
mv manifest.yml manifest.yml.src
185+
envsubst < manifest.yml.src > manifest.yml
186+
187+
- run:
188+
name: Run sed on entrypoint.sh when deploying to main
189+
command: |
190+
sed -i 's@NEW_RELIC_APP_NAME="analytics-reporter-api"@NEW_RELIC_APP_NAME="analytics-reporter-api-production"@g' entrypoint.sh
191+
192+
- run:
193+
name: Delete Knexfile.js and drop sufffix on Knexfile.js.cloudgov
194+
command: |
195+
rm knexfile.js
196+
mv knexfile.js.cloudgov knexfile.js
197+
rm ./src/config.js
198+
mv ./src/config.js.cloudgov ./src/config.js
199+
200+
- run:
201+
name: Install CF CLI
202+
command: |
203+
sudo curl -v -L -o cf8-cli-installer_8.7.4_x86-64.deb 'https://packages.cloudfoundry.org/stable?release=debian64&version=8.7.4'
204+
sudo dpkg -i cf8-cli-installer_8.7.4_x86-64.deb
205+
206+
- run:
207+
name: deploy
208+
command: |
209+
set -e
210+
# Log into cloud.gov
211+
cf api api.fr.cloud.gov
212+
cf login -u $CF_PRODUCTION_SPACE_DEPLOYER_USERNAME -p $CF_PRODUCTION_SPACE_DEPLOYER_PASSWORD -o gsa-opp-analytics -s analytics-prod
213+
cat manifest.yml
214+
cf push -f "./manifest.yml"
215+
cf set-env analytics-reporter-api-production API_DATA_GOV_SECRET "$API_SECRET"
216+
cf restage analytics-reporter-api-production
217+
cf logout
218+
219+
workflows:
220+
develop_workflow:
221+
jobs:
222+
- develop_deploy:
223+
filters:
224+
branches:
225+
only:
226+
- develop
227+
228+
staging:
229+
jobs:
230+
- staging_deploy:
231+
filters:
232+
branches:
233+
only:
234+
- stage
235+
236+
main_workflow:
237+
jobs:
238+
- main_deploy:
239+
filters:
240+
branches:
241+
only:
242+
- master
243+

README.md

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,26 @@
55
# Analytics API
66

77
A system for publishing data retrieved from the Google Analytics API by the
8-
[Analytics Reporter](https://github.com/18F/analytics-reporter). The analytics
9-
API serves data written to a Progress database by the Analytics Reporter in
10-
response to HTTP requests.
8+
[Analytics Reporter](https://github.com/18F/analytics-reporter).
9+
This Analytics API serves data written to a PostgreSQL database by the Analytics Reporter,
10+
in response to HTTP requests.
1111

1212
# Setup
1313

14-
The Analytics API maintains the schema for the database that the Analytics
15-
Reporter writes to. Because of this, the Analytics API must be setup and
14+
This Analytics API maintains the schema for the database that the
15+
[Analytics Reporter](https://github.com/18F/analytics-reporter)
16+
writes to.
17+
Thus, the Analytics API must be setup and
1618
configured before the Analytics Reporter starts writing data.
1719

18-
First, the database needs to be created:
20+
First, create the database:
1921

2022
```shell
2123
createdb analytics-reporter
2224
```
2325

24-
Once the database is created, the app can be cloned and the dependencies can be
25-
installed via NPM. The install script has a postinstall hook that will migrate
26+
Once the database is created, clone the app and install the dependencies via NPM.
27+
The install script has a postinstall hook that will migrate
2628
the database.
2729

2830
```shell
@@ -31,17 +33,17 @@ cd analytics-reporter-api
3133
npm install
3234
```
3335

34-
Once all of the dependencies are installed, the app can be started.
36+
Once the dependencies are installed, the app can be started.
3537

3638
```shell
3739
npm start
3840
```
3941

40-
The API is not available at `http://localhost:4444/`
42+
The API should now be available at `http://localhost:4444/`
4143

42-
Note that the API will not render any data until Analytics Reporter is
43-
configured to write to the same database and run with the `--write-to-database`
44-
option.
44+
Note that the API will not render any data until
45+
[Analytics Reporter](https://github.com/18F/analytics-reporter)
46+
is configured to write to the same database and run with the `--write-to-database` option.
4547

4648
# Using the API
4749

@@ -106,14 +108,21 @@ If you need to migrate the database, you can create a new migration via `knex`,
106108
See [knex documentation](https://knexjs.org/#Installation-migrations) for more details.
107109

108110
# Running database migrations
111+
109112
## Locally
113+
110114
`npm run migrate`
115+
111116
## In production
117+
112118
In production, you can run database migrations via `cf run-task`. As with anything in production, be careful when doing this! First, try checking the current status of migrations using the `migrate:status` command
119+
113120
```
114121
cf run-task analytics-reporter-api --command "knex migrate:status" --name check_migration_status
115122
```
123+
116124
This will kick off a task - you can see the output by running:
125+
117126
```
118127
cf logs analytics-reporter-api --recent
119128
# the output will look something like...
@@ -127,6 +136,7 @@ cf logs analytics-reporter-api --recent
127136
```
128137

129138
To actually run the migration, you would run:
139+
130140
```
131141
cf run-task analytics-reporter-api --command "knex migrate:latest" --name run_db_migrations
132142
```

entrypoint.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/bash
2+
export NEW_RELIC_APP_NAME="analytics-reporter-api"
3+
export NODE_ENV="test"
4+
export PATH="$PATH:/home/vcap/deps/0/bin"
5+
npm start

knexfile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ module.exports = {
1111
host: process.env.POSTGRES_HOST,
1212
user: process.env.POSTGRES_USER,
1313
password: process.env.POSTGRES_PASSWORD,
14-
database: process.env.POSTGRES_DATABASE
14+
database: process.env.POSTGRES_DATABASE,
1515
},
1616
pool: {
1717
min: 2,

knexfile.js.cloudgov

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
const VCAP_SERVICES_JSON = JSON.parse(process.env.VCAP_SERVICES);
3+
4+
module.exports = {
5+
development: {
6+
client: 'postgresql',
7+
connection: {
8+
database: 'analytics-reporter'
9+
}
10+
},
11+
production: {
12+
client: 'postgresql',
13+
connection: {
14+
host : VCAP_SERVICES_JSON["aws-rds"][0]["credentials"]["host"],
15+
user : VCAP_SERVICES_JSON["aws-rds"][0]["credentials"]["username"],
16+
password : VCAP_SERVICES_JSON["aws-rds"][0]["credentials"]["password"],
17+
database : VCAP_SERVICES_JSON["aws-rds"][0]["credentials"]["db_name"],
18+
port: 5432,
19+
ssl : true
20+
},
21+
pool: {
22+
min: 2,
23+
max: 10
24+
},
25+
migrations: {
26+
tableName: 'knex_migrations'
27+
}
28+
},
29+
test: {
30+
client: 'postgresql',
31+
connection: {
32+
user: process.env.CIRCLECI ? 'postgres' : undefined,
33+
database: 'analytics-api-test'
34+
},
35+
migrations: {
36+
tableName: 'knex_migrations'
37+
}
38+
}
39+
};

0 commit comments

Comments
 (0)