Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ECR Image Builder #367

Merged
merged 10 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions .github/workflows/ecr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
name: AWS ECR

on:
push:
branches:
- main
pull_request:
types:
- opened
- synchronize
- reopened
- ready_for_review

permissions:
id-token: write
contents: read

jobs:
build:
runs-on: ubuntu-latest
if: github.event.pull_request.draft == false
strategy:
matrix:
task: [api, data, pmtiles, hooks, events]
steps:
- uses: actions/checkout@v4
with:
ref: ${{github.event.pull_request.head.sha || github.sha}}

- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws-us-gov:iam::${{secrets.AWS_ACCOUNT_ID}}:role/Github-ECR-Builder
role-session-name: GithubECRBuilder
aws-region: ${{secrets.AWS_REGION}}

- name: Docker Build Task
run: npm run build -- ${{matrix.task}}
env:
AWS_ACCOUNT_ID: ${{secrets.AWS_ACCOUNT_ID}}
AWS_REGION: ${{secrets.AWS_REGION}}

- name: Configure AWS Credentials (STAGING)
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::${{secrets.AWS_STAGING_ACCOUNT_ID}}:role/Github-ECR-Builder
role-session-name: GithubECRBuilder
aws-region: ${{secrets.AWS_STAGING_REGION}}

- name: Docker Build Task (STAGING)
run: npm run build -- ${{matrix.task}}
env:
AWS_ACCOUNT_ID: ${{secrets.AWS_STAGING_ACCOUNT_ID}}
AWS_REGION: ${{secrets.AWS_STAGING_REGION}}

62 changes: 0 additions & 62 deletions .github/workflows/ecr_api.yml

This file was deleted.

65 changes: 0 additions & 65 deletions .github/workflows/ecr_task.yml

This file was deleted.

98 changes: 98 additions & 0 deletions build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#! /usr/bin/env node

Check warning on line 1 in build.js

View workflow job for this annotation

GitHub Actions / test

This file needs no shebang

import fs from 'node:fs/promises';
import CP from 'child_process';

process.env.GITSHA = sha();

for (const env of ['GITSHA', 'AWS_REGION', 'AWS_ACCOUNT_ID']) {
if (!process.env[env]) {
console.error(`${env} Env Var must be set`);
process.exit();

Check warning on line 11 in build.js

View workflow job for this annotation

GitHub Actions / test

Don't use process.exit(); throw an error instead
}
}

await login();


if (!process.argv[2]) {
console.error('ok - building all containers');

await api();

for (const dir of await fs.readdir(new URL('./tasks/', import.meta.url))) {
await task(dir);
}
} else {
if (process.argv[2] === 'api') {
await api();
} else {
await task(process.argv[2]);
}
}

function login() {
return new Promise((resolve, reject) => {
const $ = CP.exec(`
aws ecr get-login-password \
--region $\{AWS_REGION\} \

Check warning on line 38 in build.js

View workflow job for this annotation

GitHub Actions / test

Unnecessary escape character: \}
| docker login \
--username AWS \
--password-stdin "$\{AWS_ACCOUNT_ID\}.dkr.ecr.$\{AWS_REGION\}.amazonaws.com"

Check warning on line 41 in build.js

View workflow job for this annotation

GitHub Actions / test

Unnecessary escape character: \}

Check warning on line 41 in build.js

View workflow job for this annotation

GitHub Actions / test

Unnecessary escape character: \}

`, (err) => {
if (err) return reject(err);
return resolve();
});

$.stdout.pipe(process.stdout);
$.stderr.pipe(process.stderr);
});

}

function api() {
return new Promise((resolve, reject) => {
const $ = CP.exec(`
docker compose build api \
&& docker tag cloudtak-api:latest "$\{AWS_ACCOUNT_ID\}.dkr.ecr.$\{AWS_REGION\}.amazonaws.com/coe-ecr-etl:$\{GITSHA\}" \

Check warning on line 58 in build.js

View workflow job for this annotation

GitHub Actions / test

Unnecessary escape character: \}

Check warning on line 58 in build.js

View workflow job for this annotation

GitHub Actions / test

Unnecessary escape character: \}

Check warning on line 58 in build.js

View workflow job for this annotation

GitHub Actions / test

Unnecessary escape character: \}
&& docker push "$\{AWS_ACCOUNT_ID\}.dkr.ecr.$\{AWS_REGION\}.amazonaws.com/coe-ecr-etl:$\{GITSHA\}"

Check warning on line 59 in build.js

View workflow job for this annotation

GitHub Actions / test

Unnecessary escape character: \}

Check warning on line 59 in build.js

View workflow job for this annotation

GitHub Actions / test

Unnecessary escape character: \}
`, (err) => {
if (err) return reject(err);
return resolve();
});

$.stdout.pipe(process.stdout);
$.stderr.pipe(process.stderr);
});
}

async function task(task) {
process.env.TASK = task;

return new Promise((resolve, reject) => {
const $ = CP.exec(`
docker buildx build ./tasks/$\{TASK\}/ -t cloudtak-$\{TASK\} \
&& docker tag cloudtak-$\{TASK\}:latest "$\{AWS_ACCOUNT_ID\}.dkr.ecr.$\{AWS_REGION\}.amazonaws.com/coe-ecr-etl:$\{TASK\}-$\{GITSHA\}" \
&& docker push "$\{AWS_ACCOUNT_ID\}.dkr.ecr.$\{AWS_REGION\}.amazonaws.com/coe-ecr-etl:$\{TASK\}-$\{GITSHA\}"
`, (err) => {
if (err) return reject(err);
return resolve();
});

$.stdout.pipe(process.stdout);
$.stderr.pipe(process.stderr);
});

}

function sha() {
const git = CP.spawnSync('git', [
'--git-dir', new URL('.git', import.meta.url).pathname,
'rev-parse', 'HEAD'
]);

if (!git.stdout) throw Error('Is this a git repo? Could not determine GitSha');
return String(git.stdout).replace(/\n/g, '');

}
2 changes: 0 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
version: '3'

services:
api:
platform: linux/amd64
Expand Down
3 changes: 3 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ export default [
{
"rules": {
"no-console": 0,
"no-useless-escape": "warn",
"arrow-parens": [ "error", "always" ],
"n/no-process-exit": "warn",
"n/hashbang": "warn",
"no-var": "error",
"prefer-const": "error",
"array-bracket-spacing": [ "error", "never" ],
Expand Down
3 changes: 2 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"version": "4.8.0",
"description": "Facilitate ETL operations to bring non-TAK sources into a TAK Server",
"scripts": {
"lint": "eslint cloudformation/",
"lint": "eslint build.js cloudformation/",
"build": "./build.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"engines": {
Expand Down
4 changes: 2 additions & 2 deletions tasks/data/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ RUN apt-get update \
&& apt-get install -y build-essential curl cmake autoconf libtool software-properties-common git libsqlite3-dev \
&& apt-get install -y python3 python3-pip python3-dev python3-setuptools

RUN export NODEV='22.8.0' \
RUN export NODEV='22.9.0' \
&& curl "https://nodejs.org/dist/v${NODEV}/node-v${NODEV}-linux-x64.tar.gz" | tar -xzv > /dev/null \
&& cp ./node-v${NODEV}-linux-x64/bin/node /usr/bin/ \
&& ./node-v${NODEV}-linux-x64/bin/npm install -g npm

RUN npm install --global aws-lambda-ric
# RUN npm install --global aws-lambda-ric

# PMTiles: https://github.com/protomaps/go-pmtiles/releases
RUN curl -L https://github.com/protomaps/go-pmtiles/releases/download/v1.20.0/go-pmtiles_1.20.0_Linux_x86_64.tar.gz > /tmp/pmtiles.tar.gz && \
Expand Down
Loading