Skip to content

Commit

Permalink
fix(ci): Run db migrations in CI task container with SSL #4354
Browse files Browse the repository at this point in the history
  • Loading branch information
apburnes committed Jan 10, 2024
1 parent 859775b commit 3973006
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,4 @@ blob-report/
playwright/.cache/
playwright/.auth/*
!playwright/.auth/.gitkeep
database.json
10 changes: 10 additions & 0 deletions ci/partials/run-database-migrations.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
platform: linux
inputs: [name: src]
outputs: [name: src]
run:
dir: src
path: bash
args:
- -c
- node --env-file=.env ./ci/tasks/configure-database-migrations.js
- node node_modules/.bin/db-migrate up --config database.json -e production
15 changes: 11 additions & 4 deletions ci/pipeline-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ jobs:
params: {save: true}
- get: postgres
- get: node
params: {save: true}
- task: install-deps-api
file: src/ci/partials/install-deps-api.yml
image: node
Expand Down Expand Up @@ -88,9 +87,17 @@ jobs:
<<: *env-cf
CF_APP_NAME: pages-((deploy-env))

- task: run-database-migrations
file: src/ci/partials/run-task.yml
- task: get-app-env
file: src/ci/partials/get-app-env.yml
image: cf-image
params:
<<: *env-cf
APP_ENV: ((deploy-env))
CF_APP_NAME: pages-((deploy-env))

- task: run-database-migrations
file: src/ci/partials/run-database-migrations.yml
image: node
params:
<<: *env-cf
APP_ENV: ((deploy-env))
Expand Down Expand Up @@ -481,7 +488,7 @@ resources:
tag: 15-alpine

- name: node
type: docker-image
type: registry-image
source:
repository: node
tag: 20.9-bullseye
Expand Down
46 changes: 46 additions & 0 deletions ci/tasks/configure-database-migrations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const fs = require('node:fs');
const cfenv = require('cfenv');

const { APP_ENV } = process.env;
const fileName = 'database.json';

const baseConfig = {
driver: 'pg',
port: '5432',
ssl: {
rejectUnauthorized: false,
},
schema: 'public',
};

function main() {
try {
const appEnv = cfenv.getAppEnv();
const creds = appEnv.getServiceCreds(`federalist-${APP_ENV}-rds`);

const config = {
production: {
...baseConfig,
user: creds.username,
password: creds.password,
host: creds.host,
database: creds.name,
port: creds.port,
},
};

const data = JSON.stringify(config);

fs.writeFileSync(fileName, data);

console.log('\n\nCreated database configuration.\n\n');

process.exit(0);
} catch (error) {
console.error('\n\n!!! ERROR CONFIGURING DATABASE MIGRATIONS !!!\n\n');
console.error(error);
process.exit(1);
}
}

main();

0 comments on commit 3973006

Please sign in to comment.