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

feat DO-1593: add support for non-mono repo #8

Merged
merged 2 commits into from
Jan 11, 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
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ jobs:
- uses: actions/checkout@v2
with:
ref: ${{ github.event.release.target_commitish }}
- run: docker build --build-arg NODE_TAG=20-alpine .
- run: docker build --build-arg NODE_TAG=20 .

2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
ARG NODE_TAG
FROM node:${NODE_TAG}
FROM node:${NODE_TAG}-alpine

RUN mkdir /pipe
WORKDIR /pipe
Expand Down
37 changes: 31 additions & 6 deletions pipe/entrypoint.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,54 @@
import { glob } from "glob";
import { runCLICommand } from "./cmd";
import { env } from "./env";
import { findServerlessYaml } from "./findServerlessYaml";
import { injectCfnRole } from "./injectCfnRole";
import { uploadDeploymentBadge } from "./uploadDeploymentBadge";
import { nodeModulesDirectoryExist } from "./findNodeModules";

const cloneDir = process.env.BITBUCKET_CLONE_DIR || "";

async function main() {
let deploymentStatus = false;

try {
const rootServerlessYmlFile = await glob(
`${cloneDir}/serverless.{yml,yaml}`,
{}
);
const nxProject = rootServerlessYmlFile.length == 0;

if (!env.awsAccessKeyId || !env.awsSecretAccessKey) {
throw new Error("AWS_ACCESS_KEY_ID or AWS_SECRET_ACCESS_KEY not set");
}

const serverlessFiles = await findServerlessYaml(
`${process.env.BITBUCKET_CLONE_DIR}/services`
const servicesPath = nxProject ? env.servicesPath : "";
let serverlessFiles = await findServerlessYaml(
`${cloneDir}${servicesPath}`
);

await Promise.all(
serverlessFiles.map((file) => injectCfnRole(file, env.cfnRole))
);

await runCLICommand([
"npm ci",
const commands = [
`npx serverless config credentials --provider aws --profile ${env.profile} --key ${env.awsAccessKeyId} --secret ${env.awsSecretAccessKey}`,
`npx nx run-many -t deploy -- --verbose --stage ${env.stage} --aws-profile ${env.profile}`,
]);
];

const nodeModulesExists = await nodeModulesDirectoryExist(cloneDir);
if (!nodeModulesExists) {
commands.unshift("npm ci");
}

const nxCommand = nxProject
? `npx nx run-many -t ${env.cmd} --`
: `npx serverless ${env.cmd}`;
const verboseOption = env.debug ? "--verbose" : "";
const serverlessCommand = `${nxCommand} --stage ${env.stage} --aws-profile ${env.profile}${verboseOption}`;

commands.push(serverlessCommand);

await runCLICommand(commands);

deploymentStatus = true;
} catch (error) {
Expand Down
4 changes: 4 additions & 0 deletions pipe/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ interface Env {
debug: boolean;
stage: string;
profile: string;
cmd: string;
awsAccessKeyId?: string;
awsSecretAccessKey?: string;
cfnRole?: string;
Expand All @@ -12,12 +13,14 @@ interface Env {
bitbucketBranch?: string;
bitbucketRepoSlug?: string;
bitbucketWorkspace?: string;
servicesPath?: string;
}

export const env: Env = {
debug: process.env.DEBUG === "true",
stage: process.env.STAGE || "stg",
profile: process.env.PROFILE || "bitbucket-deployer",
cmd: process.env.cmd || "deploy",
awsAccessKeyId: process.env.AWS_ACCESS_KEY_ID,
awsSecretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
cfnRole: process.env.CFN_ROLE,
Expand All @@ -28,4 +31,5 @@ export const env: Env = {
bitbucketBranch: process.env.BITBUCKET_BRANCH,
bitbucketRepoSlug: process.env.BITBUCKET_REPO_SLUG,
bitbucketWorkspace: process.env.BITBUCKET_WORKSPACE,
servicesPath: process.env.servicesPath || "/services",
};
16 changes: 16 additions & 0 deletions pipe/findNodeModules.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import * as fs from "fs";

export async function nodeModulesDirectoryExist(
directoryPath: string
): Promise<boolean> {
return fs.promises
.access(`${directoryPath}/node_modules`, fs.constants.F_OK)
.then(() => true)
.catch((error) => {
if (error.code === "ENOENT") {
return false;
} else {
throw error;
}
});
}
2 changes: 1 addition & 1 deletion pipe/findServerlessYaml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export async function findServerlessYaml(basePath: string) {

console.log(`Fetching serverless configuration with pattern ${globPattern}`);

const files = await glob(globPattern, {});
const files = await glob(globPattern, { ignore: ["**/node_modules/**"] });

for (const file of files) {
console.log("Found serverless.yml at: ", file);
Expand Down
Loading