diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..42e9fa8 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,9 @@ +.env +.github +build +Dockerfile +docker-compose.yml +node_modules +tmp +*.log +*.zip diff --git a/.gitignore b/.gitignore index 2f03154..fa474e1 100644 --- a/.gitignore +++ b/.gitignore @@ -131,3 +131,7 @@ dist .yarn/build-state.yml .yarn/install-state.gz .pnp.* + +# lambda build +build/ +build.zip diff --git a/.lambdaignore b/.lambdaignore new file mode 100644 index 0000000..00fc413 --- /dev/null +++ b/.lambdaignore @@ -0,0 +1,22 @@ +.dockerignore +.editorconfig +.env +.eslintrc.yml +.git +.github +.gitignore +.lambdaignore +.prettierignore +.prettierrc.yml +.tool-versions +buildspec.yml +docker-compose.yml +Dockerfile +env-example +/build* +runner.js +/tmp +tsconfig.json +*.test.js +*.log +*.zip diff --git a/.prettierignore b/.prettierignore new file mode 100644 index 0000000..342ac64 --- /dev/null +++ b/.prettierignore @@ -0,0 +1,2 @@ +# Ignore artifacts: +build diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..9df2c07 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,16 @@ +FROM public.ecr.aws/lambda/nodejs:20 + +LABEL maintainer="PRX " +LABEL org.prx.spire.publish.s3="LAMBDA_ZIP" + +WORKDIR /app + +ENTRYPOINT [ "yarn", "run" ] +CMD [ "test" ] + +RUN yum install -y rsync zip tar xz && yum clean all && rm -rf /var/cache/yum +ADD yarn.lock ./ +ADD package.json ./ +RUN yarn install +ADD . . +RUN yarn run build diff --git a/buildspec.yml b/buildspec.yml new file mode 100644 index 0000000..1aa39b2 --- /dev/null +++ b/buildspec.yml @@ -0,0 +1,18 @@ +version: 0.2 +env: + variables: + PRX_SPIRE_S3_PKG_PARAMETERS: LAMBDA_ZIP=/prx/stag/Spire/Dovetail-CDN_Usage/pkg/s3-object-key + PRX_S3_ARCHIVE_BUILD_PATH: /app/build.zip + exported-variables: + - PRX_SPIRE_S3_PKG_PARAMETERS + - LAMBDA_ZIP +phases: + build: + commands: + - docker-compose build + - docker-compose run test + post_build: + commands: + - curl -sO "https://raw.githubusercontent.com/PRX/Infrastructure/main/ci/utility/post_build.sh" + - chmod +x post_build.sh + - . ./post_build.sh diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..ef63be7 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,11 @@ +version: "2" +services: + test: + build: . + image: dovetailcdnusage + command: test + volumes: + - ./index.js:/app/index.js + - ./index.test.js:/app/index.test.js + - ./lib:/app/lib + - ./package.json:/app/package.json diff --git a/index.test.js b/index.test.js new file mode 100644 index 0000000..64a3c4b --- /dev/null +++ b/index.test.js @@ -0,0 +1,15 @@ +const log = require("lambda-log"); +const index = require("./index"); + +describe("index", () => { + describe("handler", () => { + it("validates dates", async () => { + jest.spyOn(log, "error").mockImplementation(() => null); + + expect(index.handler({ day: "n/a" })).rejects.toThrow(/invalid date/i); + + expect(log.error).toHaveBeenCalledTimes(1); + expect(log.error.mock.calls[0][0]).toMatch(/error running rollups/); + }); + }); +}); diff --git a/package.json b/package.json index 64f9b82..1374c41 100644 --- a/package.json +++ b/package.json @@ -8,6 +8,10 @@ }, "private": "true", "scripts": { + "build": "yarn run build-sync && yarn run build-prune && yarn run build-zip", + "build-sync": "rsync -crl --delete --exclude-from .lambdaignore . build", + "build-prune": "cd build && yarn --production", + "build-zip": "rm -f build.zip && cd build && zip -rq ../build.zip .", "lint": "npm run lint-prettier && npm run lint-es", "lint-prettier": "npx prettier --check **/*.{json,yml}", "lint-es": "npx eslint '**/*.{js,mjs}'",