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

Unreverts #235 and don't automatically install wrangler when checking if it present #271

Merged
merged 2 commits into from
Jun 20, 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
5 changes: 5 additions & 0 deletions .changeset/brown-spies-grab.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wrangler-action": minor
---

This unreverts #235 ensuring wrangler-action will re-use existing wrangler installations, thanks @AdiRishi! and ensures we don't automatically install wrangler when checking if it present
43 changes: 23 additions & 20 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
- name: Only build app
uses: ./
with:
workingDirectory: "./test/base"
workingDirectory: "./test/only-build"
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
command: deploy --dry-run
Expand All @@ -38,12 +38,11 @@ jobs:
uses: ./
with:
quiet: true
workingDirectory: "./test/base"
workingDirectory: "./test/build-quiet"
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
command: deploy --dry-run

# START Setup and teardown of Worker Environment Tests
- name: Environment support
uses: ./
with:
Expand All @@ -52,28 +51,20 @@ jobs:
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
environment: dev
preCommands: npx wrangler deploy --env dev # https://github.com/cloudflare/wrangler-action/issues/162
postCommands: npx wrangler delete --name wrangler-action-dev-environment-test --force
secrets: |
SECRET1
SECRET2
env:
SECRET1: ${{ secrets.SECRET1 }}
SECRET2: ${{ secrets.SECRET2 }}

- name: Clean up Deployed Environment Worker
uses: ./
with:
workingDirectory: "./test/base"
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
command: delete --name wrangler-action-dev-environment-test --force

# END Setup and teardown of Worker Environment Tests
# START Setup and teardown of Workers w/ Secrets Tests
- name: Deploy app secrets w/ hardcoded Wrangler v2
uses: ./
with:
wranglerVersion: "2.20.0"
workingDirectory: "./test/base"
workingDirectory: "./test/secrets-v2"
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
secrets: |
Expand All @@ -84,13 +75,13 @@ jobs:
SECRET2: ${{ secrets.SECRET2 }}

- name: Health Check Deployed Worker
run: node .github/workflows/workerHealthCheck.cjs
run: node .github/workflows/workerHealthCheck.cjs wrangler-action-test-secrets-v2
shell: bash

- name: Deploy app secrets w/ default version
uses: ./
with:
workingDirectory: "./test/base"
workingDirectory: "./test/secrets-default"
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
secrets: |
Expand All @@ -101,22 +92,23 @@ jobs:
SECRET2: ${{ secrets.SECRET2 }}

- name: Health Check Deployed Worker
run: node .github/workflows/workerHealthCheck.cjs
run: node .github/workflows/workerHealthCheck.cjs wrangler-action-test-secrets-default
shell: bash

- name: Clean Up Deployed Workers
uses: ./
with:
workingDirectory: "./test/base"
workingDirectory: "./test/secrets-default"
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
command: delete --name wrangler-action-test --force
command: delete --name wrangler-action-test-secrets-v2 --force
postCommands: npx wrangler delete --name wrangler-action-test-secrets-default --force
# END Setup and teardown of Workers w/ Secrets Tests

- name: Support packageManager variable
uses: ./
with:
workingDirectory: "./test/empty"
workingDirectory: "./test/specify-package-manager"
packageManager: "npm"
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
Expand All @@ -125,7 +117,7 @@ jobs:
- name: Support unspecified packageManager with no lockfile
uses: ./
with:
workingDirectory: "./test/empty"
workingDirectory: "./test/unspecified-package-manager"
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
command: deploy --dry-run
Expand Down Expand Up @@ -159,3 +151,14 @@ jobs:
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
command: deploy --dry-run

- name: Change directory to pre-installed-wrangler and install dependencies
run: |
cd ./test/pre-installed-wrangler
npm install

- name: Support pre-installed wrangler
uses: ./
with:
workingDirectory: "./test/pre-installed-wrangler"
command: action-test
16 changes: 12 additions & 4 deletions .github/workflows/workerHealthCheck.cjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
const { execSync } = require("child_process");

function workerHealthCheck() {
const url =
"https://wrangler-action-test.devprod-testing7928.workers.dev/secret-health-check";
function workerHealthCheck(workerName) {
const url = `https://${workerName}.devprod-testing7928.workers.dev/secret-health-check`;

const buffer = execSync(`curl ${url}`);

Expand All @@ -17,4 +16,13 @@ function workerHealthCheck() {
return response;
}

workerHealthCheck();
const args = Array.from(process.argv);
const workerName = args.pop();

if (!workerName) {
throw new Error(
"Please provide the worker name as an argument when calling this program.",
);
}

workerHealthCheck(workerName);
4 changes: 2 additions & 2 deletions package-lock.json

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

62 changes: 62 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {
setFailed,
setOutput,
} from "@actions/core";
import { getExecOutput } from "@actions/exec";
import semverEq from "semver/functions/eq";
import { exec, execShell } from "./exec";
import { checkWorkingDirectory, semverCompare } from "./utils";
import { getPackageManager } from "./packageManagers";
Expand All @@ -21,6 +23,7 @@ const DEFAULT_WRANGLER_VERSION = "3.13.2";
*/
const config = {
WRANGLER_VERSION: getInput("wranglerVersion") || DEFAULT_WRANGLER_VERSION,
didUserProvideWranglerVersion: Boolean(getInput("wranglerVersion")),
secrets: getMultilineInput("secrets"),
workingDirectory: checkWorkingDirectory(getInput("workingDirectory")),
CLOUDFLARE_API_TOKEN: getInput("apiToken"),
Expand Down Expand Up @@ -82,6 +85,65 @@ async function installWrangler() {
);
}

startGroup("🔍 Checking for existing Wrangler installation");
let installedVersion = "";
let installedVersionSatisfiesRequirement = false;
try {
const { stdout } = await getExecOutput(
// We want to simply invoke wrangler to check if it's installed, but don't want to auto-install it at this stage
packageManager.execNoInstall,
["wrangler", "--version"],
{
cwd: config["workingDirectory"],
silent: config.QUIET_MODE,
},
);
// There are two possible outputs from `wrangler --version`:
// ` ⛅️ wrangler 3.48.0 (update available 3.53.1)`
// and
// `3.48.0`
const versionMatch =
stdout.match(/wrangler (\d+\.\d+\.\d+)/) ??
stdout.match(/^(\d+\.\d+\.\d+)/);
if (versionMatch) {
installedVersion = versionMatch[1];
}
if (config.didUserProvideWranglerVersion) {
installedVersionSatisfiesRequirement = semverEq(
installedVersion,
config["WRANGLER_VERSION"],
);
}
if (!config.didUserProvideWranglerVersion && installedVersion) {
info(
`✅ No wrangler version specified, using pre-installed wrangler version ${installedVersion}`,
true,
);
endGroup();
return;
}
if (
config.didUserProvideWranglerVersion &&
installedVersionSatisfiesRequirement
) {
info(`✅ Using Wrangler ${installedVersion}`, true);
endGroup();
return;
}
info(
"⚠️ Wrangler not found or version is incompatible. Installing...",
true,
);
} catch (error) {
debug(`Error checking Wrangler version: ${error}`);
info(
"⚠️ Wrangler not found or version is incompatible. Installing...",
true,
);
} finally {
endGroup();
}

startGroup("📥 Installing Wrangler");
try {
await exec(
Expand Down
67 changes: 38 additions & 29 deletions src/packageManagers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,25 @@ describe("getPackageManager", () => {
.toMatchInlineSnapshot(`
{
"exec": "npx",
"execNoInstall": "npx --no-install",
"install": "npm i",
}
`);

expect(getPackageManager("yarn", { workingDirectory: "test/npm" }))
.toMatchInlineSnapshot(`
{
"exec": "yarn",
"install": "yarn add",
}
`);
{
"exec": "yarn",
"execNoInstall": "yarn",
"install": "yarn add",
}
`);

expect(getPackageManager("pnpm", { workingDirectory: "test/npm" }))
.toMatchInlineSnapshot(`
{
"exec": "pnpm exec",
"execNoInstall": "pnpm exec",
"install": "pnpm add",
}
`);
Expand All @@ -31,6 +34,7 @@ describe("getPackageManager", () => {
.toMatchInlineSnapshot(`
{
"exec": "bunx",
"execNoInstall": "bun run",
"install": "bun i",
}
`);
Expand All @@ -39,51 +43,56 @@ describe("getPackageManager", () => {
test("should use npm if no value provided and package-lock.json exists", () => {
expect(getPackageManager("", { workingDirectory: "test/npm" }))
.toMatchInlineSnapshot(`
{
"exec": "npx",
"install": "npm i",
}
`);
{
"exec": "npx",
"execNoInstall": "npx --no-install",
"install": "npm i",
}
`);
});

test("should use yarn if no value provided and yarn.lock exists", () => {
expect(getPackageManager("", { workingDirectory: "test/yarn" }))
.toMatchInlineSnapshot(`
{
"exec": "yarn",
"install": "yarn add",
}
`);
{
"exec": "yarn",
"execNoInstall": "yarn",
"install": "yarn add",
}
`);
});

test("should use pnpm if no value provided and pnpm-lock.yaml exists", () => {
expect(getPackageManager("", { workingDirectory: "test/pnpm" }))
.toMatchInlineSnapshot(`
{
"exec": "pnpm exec",
"install": "pnpm add",
}
`);
{
"exec": "pnpm exec",
"execNoInstall": "pnpm exec",
"install": "pnpm add",
}
`);
});

test("should use bun if no value provided and bun.lockb exists", () => {
expect(getPackageManager("", { workingDirectory: "test/bun" }))
.toMatchInlineSnapshot(`
{
"exec": "bunx",
"install": "bun i",
}
{
"exec": "bunx",
"execNoInstall": "bun run",
"install": "bun i",
}
`);
});

test("should use npm if no value provided and no lockfile is present", () => {
expect(getPackageManager("", { workingDirectory: "test/empty" }))
.toMatchInlineSnapshot(`
{
"exec": "npx",
"install": "npm i",
}
`);
{
"exec": "npx",
"execNoInstall": "npx --no-install",
"install": "npm i",
}
`);
});

test("should throw if an invalid value is provided", () => {
Expand Down
Loading
Loading