Skip to content

Commit

Permalink
Rewrite backend's build.sh script to node
Browse files Browse the repository at this point in the history
  • Loading branch information
pziemkowski committed Aug 31, 2023
1 parent f2b4065 commit 992c66f
Show file tree
Hide file tree
Showing 8 changed files with 858 additions and 83 deletions.
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ services:
- "5432:5432"
restart: unless-stopped
environment:
- PGUSER=backend
- POSTGRES_USER=backend
- POSTGRES_PASSWORD=backend
- POSTGRES_DB=backend
Expand Down
2 changes: 2 additions & 0 deletions packages/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
"@sb/infra-shared": "*",
"@sb/tools": "*",
"aws-cdk": "^2.84.0",
"@aws-sdk/client-ecr": "^3.398.0",
"@aws-sdk/client-sts": "^3.398.0",
"aws-cdk-lib": "^2.84.0",
"constructs": "^10.1.225"
}
Expand Down
2 changes: 1 addition & 1 deletion packages/backend/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"options": {
"cwd": "packages/backend",
"color": true,
"commands": ["/bin/bash scripts/build.sh"],
"commands": ["node scripts/build.js"],
"parallel": false
},
"dependsOn": ["test"]
Expand Down
108 changes: 108 additions & 0 deletions packages/backend/scripts/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
const { spawn } = require('child_process');
const {
ECRClient,
GetAuthorizationTokenCommand,
} = require('@aws-sdk/client-ecr');
const {
STSClient,
GetCallerIdentityCommand,
} = require('@aws-sdk/client-sts');

const AWS_DEFAULT_REGION = process.env.AWS_DEFAULT_REGION;
const PROJECT_NAME = process.env.PROJECT_NAME;
const VERSION = process.env.VERSION;
const BACKEND_BASE_IMAGE = process.env.BACKEND_BASE_IMAGE;

const stsClient = new STSClient();
const ecrClient = new ECRClient();

function runCommand(command, args) {
return new Promise((resolve, reject) => {
const child = spawn(command, args, {stdio: 'inherit'});

child.on('close', (code) => {
if (code !== 0) {
reject(
new Error(`"${command} ${args.join(' ')}" failed with code ${code}`)
);
} else {
resolve();
}
});
});
}

(async () => {
try {
const getCallerIdentityCommand = new GetCallerIdentityCommand();
const { Account: AWS_ACCOUNT_ID } = await stsClient.send(getCallerIdentityCommand);
const BACKEND_REPO_URI = `${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_DEFAULT_REGION}.amazonaws.com/${PROJECT_NAME}-backend`;

try {
const getAuthorizationTokenCommand = new GetAuthorizationTokenCommand();
const { authorizationData } = await ecrClient.send(
getAuthorizationTokenCommand
);
const decodedAuthToken = Buffer.from(
authorizationData[0].authorizationToken,
'base64'
).toString('utf8');
const password = decodedAuthToken.split(':')[1];

await runCommand(
'docker',
['login', '--username', 'AWS', '-p', password, BACKEND_REPO_URI]
);
} catch (error) {
console.error(error);
console.warn(
'get-login-password not supported by the AWS CLI, trying get-login instead...'
);
await runCommand('aws', [
'ecr',
'get-login',
'--no-include-email',
'--region',
AWS_DEFAULT_REGION,
]);
}

try {
await runCommand('docker', ['pull', `${BACKEND_REPO_URI}:latest`]);
} catch (error) {
console.warn(`Warning: ${error.message}`);
}

if (BACKEND_BASE_IMAGE) {
await runCommand('docker', [
'build',
'--target',
'backend',
'-t',
`${BACKEND_REPO_URI}:${VERSION}`,
'--build-arg',
`BACKEND_BASE_IMAGE=${BACKEND_BASE_IMAGE}`,
'.',
]);
} else {
await runCommand('docker', [
'build',
'--target',
'backend',
'-t',
`${BACKEND_REPO_URI}:${VERSION}`,
'.',
]);
}

await runCommand('docker', ['push', `${BACKEND_REPO_URI}:${VERSION}`]);
await runCommand('docker', [
'tag',
`${BACKEND_REPO_URI}:${VERSION}`,
`${BACKEND_REPO_URI}:latest`,
]);
await runCommand('docker', ['push', `${BACKEND_REPO_URI}:latest`]);
} catch (error) {
console.error(`Error: ${error.message}`);
}
})();
27 changes: 0 additions & 27 deletions packages/backend/scripts/build.sh

This file was deleted.

17 changes: 8 additions & 9 deletions packages/internal/core/scripts/docker-login.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

const { spawn } = require('node:child_process');

spawn('git', [
'describe',
'--tags',
'--first-parent',
'--abbrev=11',
'--long',
'--dirty',
'--always',
], {stdio: 'inherit'});
const username = process.env.DOCKER_USERNAME || '';
const password = process.env.DOCKER_PASSWORD || '';

if (username && password) {
spawn('docker', ['login', '-u', `"${username}"`, '-p', `"${password}"`], {
stdio: 'inherit',
});
}
23 changes: 13 additions & 10 deletions packages/internal/core/scripts/get-version.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

const { spawn } = require('node:child_process');

const username = process.env.DOCKER_USERNAME || '';
const password = process.env.DOCKER_PASSWORD || '';

if (username && password) {
spawn('docker', [
'login',
'-u', `"${username}"`,
'-p', `"${password}"`,
], {stdio: 'inherit'});
}
spawn(
'git',
[
'describe',
'--tags',
'--first-parent',
'--abbrev=11',
'--long',
'--dirty',
'--always',
],
{ stdio: 'inherit' }
);
Loading

0 comments on commit 992c66f

Please sign in to comment.