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

Create AWS plugin for s3 uploads deployments #61

Merged
merged 11 commits into from
Oct 8, 2023
5 changes: 5 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
"ignorePatterns": ["**/*"],
"plugins": ["@nx"],
"overrides": [
{
"files": "*.json",
"parser": "jsonc-eslint-parser",
"rules": {}
},
{
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
"rules": {
Expand Down
13 changes: 7 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -193,12 +193,13 @@ jobs:
aws-region: eu-central-1

- name: Deploy Dev Client
if: ${{ env.is-pull-request == 'true' }}
run: npx nx affected --target=deploy --exclude='*,!tag:frontend' --environment=dev
if: ${{ env.is-main-branch != 'true' }}
run: npx nx affected --target=deploy --exclude='*,!tag:frontend' --configuration=development


- name: Deploy Prod Client
if: ${{ env.is-main-branch == 'true' }}
run: npx nx affected --target=deploy --exclude='*,!tag:frontend' --environment=prod
run: npx nx affected --target=deploy --exclude='*,!tag:frontend' --configuration=production

deploy-server:
name: Deploy Server
Expand Down Expand Up @@ -232,9 +233,9 @@ jobs:
aws-region: eu-central-1

- name: Deploy Dev Server
if: ${{ env.is-pull-request == 'true' }}
run: npx nx affected --target=deploy --exclude='*,!tag:backend' --environment=dev
if: ${{ env.is-main-branch != 'true' }}
run: npx nx affected --target=deploy --exclude='*,!tag:backend' --configuration=development

- name: Deploy Prod Server
if: ${{ env.is-main-branch == 'true' }}
run: npx nx affected --target=deploy --exclude='*,!tag:backend' --environment=dev
run: npx nx affected --target=deploy --exclude='*,!tag:backend' --configuration=production
30 changes: 10 additions & 20 deletions apps/audit-runner/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,29 +71,19 @@
}
}
},
"s3-upload": {
"executor": "@app-speed/aws-deploy:s3-upload",
"dependsOn": ["build"],
"options": {
"upload": "dist/apps/audit-runner",
"bucket": "s3://ufo-runner-dev"
}
},
"deploy": {
"executor": "nx:run-commands",
"dependsOn": ["build"],
"dependsOn": ["s3-upload"],
"options": {
"parallel": false,
"commands": [
{
"command": "`[ {args.environment} != dev ]` && `[ {args.environment} != prod ]` && exit 1 || exit 0",
"forwardAllArgs": true
},
{
"command": "echo 'Deploying Server to {args.environment} environment'",
"forwardAllArgs": true
},
{
"command": "`[ {args.environment} == prod ]` && aws s3 sync ./dist/apps/audit-runner s3://ufo-runner-dev --delete && node ./scripts/deploy-runner.js || exit 0",
"forwardAllArgs": true
},
{
"command": "`[ {args.environment} == dev ]` && aws s3 sync ./dist/apps/audit-runner s3://ufo-runner-dev --delete && node ./scripts/deploy-runner.js || exit 0",
"forwardAllArgs": true
}
]
"command": "node ./scripts/deploy-runner.js"
}
}
},
Expand Down
32 changes: 12 additions & 20 deletions apps/user-flow-manager/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,28 +79,20 @@
}
},
"deploy": {
"executor": "nx:run-commands",
"executor": "@app-speed/aws-deploy:s3-upload",
"dependsOn": ["build"],
"options": {
"commands": [
{
"command": "`[ {args.environment} != dev ]` && `[ {args.environment} != prod ]` && exit 1 || exit 0",
"forwardAllArgs": true
},
{
"command": "echo 'Deploying Front-End to {args.environment} environment'",
"forwardAllArgs": true
},
{
"command": "`[ {args.environment} == prod ]` && aws s3 sync ./dist/apps/user-flow-manager s3://app.deep-blue.io --delete || exit 0",
"forwardAllArgs": true
},
{
"command": "`[ {args.environment} == dev ]` && aws s3 sync ./dist/apps/user-flow-manager s3://dev.deep-blue.io --delete || exit 0",
"forwardAllArgs": true
}
]
}
"upload": "dist/apps/user-flow-manager"
},
"configurations": {
"production": {
"bucket": "s3://app.deep-blue.io"
},
"development": {
"bucket": "s3://dev.deep-blue.io"
}
},
"defaultConfiguration": "development"
}
},
"tags": ["frontend"]
Expand Down
32 changes: 32 additions & 0 deletions libs/aws-deploy/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"extends": ["../../.eslintrc.json"],
"ignorePatterns": ["!**/*"],
"overrides": [
{
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
"rules": {}
},
{
"files": ["*.ts", "*.tsx"],
"rules": {}
},
{
"files": ["*.js", "*.jsx"],
"rules": {}
},
{
"files": ["*.json"],
"parser": "jsonc-eslint-parser",
"rules": {
"@nx/dependency-checks": "error"
}
},
{
"files": ["./package.json", "./executors.json"],
"parser": "jsonc-eslint-parser",
"rules": {
"@nx/nx-plugin-checks": "error"
}
}
]
}
11 changes: 11 additions & 0 deletions libs/aws-deploy/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# aws-deploy

This library was generated with [Nx](https://nx.dev).

## Building

Run `nx build aws-deploy` to build the library.

## Running unit tests

Run `nx test aws-deploy` to execute the unit tests via [Jest](https://jestjs.io).
9 changes: 9 additions & 0 deletions libs/aws-deploy/executors.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"executors": {
"s3-upload": {
"implementation": "./src/executors/s3-upload/executor",
"schema": "./src/executors/s3-upload/schema.json",
"description": "Executor to upload files to an s3 bucket"
}
}
}
10 changes: 10 additions & 0 deletions libs/aws-deploy/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* eslint-disable */
export default {
displayName: 'aws-deploy',
preset: '../../jest.preset.js',
transform: {
'^.+\\.[tj]s$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.spec.json' }],
},
moduleFileExtensions: ['ts', 'js', 'html'],
coverageDirectory: '../../coverage/libs/aws-deploy',
};
11 changes: 11 additions & 0 deletions libs/aws-deploy/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "@app-speed/aws-deploy",
"version": "0.0.1",
"dependencies": {
"@nx/devkit": "16.10.0"
},
"type": "commonjs",
"main": "./src/index.js",
"typings": "./src/index.d.ts",
"executors": "./executors.json"
}
66 changes: 66 additions & 0 deletions libs/aws-deploy/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
{
"name": "aws-deploy",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "libs/aws-deploy/src",
"projectType": "library",
"targets": {
"build": {
"executor": "@nx/js:tsc",
"outputs": ["{options.outputPath}"],
"options": {
"outputPath": "dist/libs/aws-deploy",
"main": "libs/aws-deploy/src/index.ts",
"tsConfig": "libs/aws-deploy/tsconfig.lib.json",
"assets": [
"libs/aws-deploy/*.md",
{
"input": "./libs/aws-deploy/src",
"glob": "**/!(*.ts)",
"output": "./src"
},
{
"input": "./libs/aws-deploy/src",
"glob": "**/*.d.ts",
"output": "./src"
},
{
"input": "./libs/aws-deploy",
"glob": "generators.json",
"output": "."
},
{
"input": "./libs/aws-deploy",
"glob": "executors.json",
"output": "."
}
]
}
},
"lint": {
"executor": "@nx/linter:eslint",
"outputs": ["{options.outputFile}"],
"options": {
"lintFilePatterns": [
"libs/aws-deploy/**/*.ts",
"libs/aws-deploy/package.json",
"libs/aws-deploy/executors.json"
]
}
},
"test": {
"executor": "@nx/jest:jest",
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
"options": {
"jestConfig": "libs/aws-deploy/jest.config.ts",
"passWithNoTests": true
},
"configurations": {
"ci": {
"ci": true,
"codeCoverage": true
}
}
}
},
"tags": []
}
16 changes: 16 additions & 0 deletions libs/aws-deploy/src/executors/s3-upload/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

const EXECUTOR_FAIL_BASE = 'Executor failed because';
const EXECUTOR_SUCCESS_BASE = 'Successfully ran executor';

const MISSING_BUCKET = 'bucket name was not found in options';
const INVALID_BUCKET = 'bucket name was invalid';

const MISSING_UPLOAD = 'upload was not found in options';

export {
EXECUTOR_FAIL_BASE,
EXECUTOR_SUCCESS_BASE,
MISSING_BUCKET,
INVALID_BUCKET,
MISSING_UPLOAD
};
46 changes: 46 additions & 0 deletions libs/aws-deploy/src/executors/s3-upload/executor.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { S3UploadExecutorSchema } from './schema';
import executor from './executor';
import { EXECUTOR_FAIL_BASE, EXECUTOR_SUCCESS_BASE, INVALID_BUCKET, MISSING_BUCKET, MISSING_UPLOAD } from './constants';

const options: S3UploadExecutorSchema = {
bucket: 'example-bucket',
upload: 'example-upload',
};

describe('S3Upload Executor', () => {
it('can run', async () => {
const output = await executor(options);
expect(output.success).toBe(true);
expect(output.message).toContain(EXECUTOR_SUCCESS_BASE);
});

it('fails if no bucket option', async () => {
const output = await executor({
...options,
bucket: undefined as unknown as string,
});
expect(output.success).toBe(false);
expect(output.message).toContain(EXECUTOR_FAIL_BASE);
expect(output.message).toContain(MISSING_BUCKET);
});

it('fails on invalid bucket option', async () => {
const output = await executor({
...options,
bucket: 'invalid bucket name',
});
expect(output.success).toBe(false);
expect(output.message).toContain(EXECUTOR_FAIL_BASE);
expect(output.message).toContain(INVALID_BUCKET);
});

it('fails if no upload option', async () => {
const output = await executor({
...options,
upload: undefined as unknown as string,
});
expect(output.success).toBe(false);
expect(output.message).toContain(EXECUTOR_FAIL_BASE);
expect(output.message).toContain(MISSING_UPLOAD);
});
});
Loading
Loading