Skip to content

Commit

Permalink
Added a check in CI/CD environments for account_id, `CLOUDFLARE_ACC…
Browse files Browse the repository at this point in the history
…OUNT_ID` and `CLOUDFLARE_API_TOKEN`. If `account_id` exists in `wrangler.toml`

then `CLOUDFLARE_ACCOUNT_ID` is not needed in CI/CD scope. The `CLOUDFLARE_API_TOKEN` is necessary in CI/CD scope and will always error if missing.

resolves #827
  • Loading branch information
JacobMGEvans committed Apr 26, 2022
1 parent 0c582be commit 06f5e6d
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 3 deletions.
8 changes: 8 additions & 0 deletions .changeset/eighty-yaks-jump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"wrangler": patch
---

feat: Added a check in CI/CD environments for `account_id`, `CLOUDFLARE_ACCOUNT_ID` and `CLOUDFLARE_API_TOKEN`. If `account_id` exists in `wrangler.toml`
then `CLOUDFLARE_ACCOUNT_ID` is not needed in CI/CD scope. The `CLOUDFLARE_API_TOKEN` is necessary in CI/CD scope and will always error if missing.

resolves #827
9 changes: 7 additions & 2 deletions package-lock.json

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

1 change: 1 addition & 0 deletions packages/wrangler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"@types/ws": "^8.5.3",
"@types/yargs": "^17.0.10",
"chokidar": "^3.5.3",
"ci-info": "^3.3.0",
"clipboardy": "^3.0.0",
"cmd-shim": "^4.1.0",
"command-exists": "^1.2.9",
Expand Down
91 changes: 91 additions & 0 deletions packages/wrangler/src/__tests__/ci.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import ci from "ci-info";
import { mockConsoleMethods } from "./helpers/mock-console";
import { runInTempDir } from "./helpers/run-in-tmp";
import { runWrangler } from "./helpers/run-wrangler";
import writeWranglerToml from "./helpers/write-wrangler-toml";

const std = mockConsoleMethods();
void std; // Keeps the console quiet
runInTempDir();

const ENV_COPY = process.env;

beforeEach(() => {
jest.resetModules();
jest.mock("ci-info");
(ci.isCI as jest.Mocked<boolean>) = true;
});

afterEach(() => {
process.env = ENV_COPY;
});

afterAll(() => {
jest.unmock("ci-info");
});

it("should not throw an error in CI if 'CLOUDFLARE_API_TOKEN' & 'account_id' are in scope", async () => {
writeWranglerToml({
account_id: "IG-88",
});

process.env = {
CLOUDFLARE_API_TOKEN: "123456789",
};

await runWrangler().catch((err) => {
expect(err).toMatchInlineSnapshot(`""`);
});
});

it("should not throw an error if 'CLOUDFLARE_ACCOUNT_ID' & 'CLOUDFLARE_API_TOKEN' are in scope", async () => {
process.env = {
CLOUDFLARE_API_TOKEN: "hunter2",
CLOUDFLARE_ACCOUNT_ID: "IG-88",
};

await runWrangler().catch((err) => {
expect(err).toMatchInlineSnapshot(`""`);
});
});

it("should throw an error in CI if 'account_id' & 'CLOUDFLARE_ACCOUNT_ID' is missing", async () => {
writeWranglerToml({
account_id: undefined,
});

process.env = {
CLOUDFLARE_API_TOKEN: "hunter2",
CLOUDFLARE_ACCOUNT_ID: undefined,
};

await runWrangler().catch((err) => {
expect(err).toMatchInlineSnapshot(
`[Error: Missing "account_id" from "wrangler.toml" and "CLOUDFLARE_ACCOUNT_ID" from CI environment, one is required, please see docs for more info: TBD]`
);
});
});

it("should throw error in CI if 'CLOUDFLARE_API_TOKEN' is missing", async () => {
writeWranglerToml({
account_id: undefined,
});

process.env = {
CLOUDFLARE_API_TOKEN: undefined,
CLOUDFLARE_ACCOUNT_ID: "badwolf",
};
await runWrangler().catch((err) => {
expect(err).toMatchInlineSnapshot(
`[Error: Missing "CLOUDFLARE_API_TOKEN" from CI environment, please see docs for more info: TBD]`
);
});
});

it("should throw errors in CI if 'CLOUDFLARE_API_TOKEN', 'account_id' & 'CLOUDFLARE_ACCOUNT_ID is missing", async () => {
await runWrangler().catch((err) => {
expect(err).toMatchInlineSnapshot(
`[Error: Missing "account_id" from "wrangler.toml" and "CLOUDFLARE_ACCOUNT_ID" "CLOUDFLARE_API_TOKEN" from CI environment, please see docs for more info: TBD]`
);
});
});
27 changes: 27 additions & 0 deletions packages/wrangler/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import path from "node:path";
import { setTimeout } from "node:timers/promises";
import TOML from "@iarna/toml";
import chalk from "chalk";
import ci from "ci-info";
import { findUp } from "find-up";
import getPort from "get-port";
import { render } from "ink";
Expand Down Expand Up @@ -203,6 +204,32 @@ function demandOneOfOption(...options: string[]) {
class CommandLineArgsError extends Error {}

export async function main(argv: string[]): Promise<void> {
if (ci.isCI) {
const config = readConfig(undefined, {});

if (
!process.env.CLOUDFLARE_API_TOKEN &&
!config.account_id &&
!process.env.CLOUDFLARE_ACCOUNT_ID
) {
throw new Error(
`Missing "account_id" from "wrangler.toml" and "CLOUDFLARE_ACCOUNT_ID" "CLOUDFLARE_API_TOKEN" from CI environment, please see docs for more info: TBD`
);
}

if (!process.env.CLOUDFLARE_API_TOKEN) {
throw new Error(
`Missing "CLOUDFLARE_API_TOKEN" from CI environment, please see docs for more info: TBD`
);
}

if (!config.account_id && !process.env.CLOUDFLARE_ACCOUNT_ID) {
throw new Error(
`Missing "account_id" from "wrangler.toml" and "CLOUDFLARE_ACCOUNT_ID" from CI environment, one is required, please see docs for more info: TBD`
);
}
}

const wrangler = makeCLI(argv)
.strict()
// We handle errors ourselves in a try-catch around `yargs.parse`.
Expand Down
4 changes: 3 additions & 1 deletion packages/wrangler/src/reporting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
Integrations,
setContext,
} from "@sentry/node";
import ci from "ci-info";
import { execaSync } from "execa";
import prompts from "prompts";
import * as pkj from "../package.json";
Expand Down Expand Up @@ -92,7 +93,8 @@ function exceptionTransaction(error: Error, origin = "") {
}

export async function reportError(err: Error, origin = "") {
if (!process.stdout.isTTY) return await appendReportingDecision("false");
// If the user has not opted in to error reporting, we don't want to do anything in CI or non-interactive environments
if (!process.stdout.isTTY || ci.isCI) return;

const errorTrackingOpt = await reportingPermission();

Expand Down

0 comments on commit 06f5e6d

Please sign in to comment.