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(action): auto-discovery of deno.json and auto-populate defaults #351

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
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
28 changes: 19 additions & 9 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,41 +13,51 @@ jobs:
permissions:
id-token: write
contents: read
env:
PROJECT_NAME: happy-rat-64
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Deploy to Deno Deploy
uses: ./
with:
project: happy-rat-64
project: ${{ env.PROJECT_NAME }}
root: action/tests
entrypoint: hello.ts
import-map: ./import_map.json
- name: "Setup test: Deploy to Deno Deploy with deno.jsonc file"
run: sed -i 's/\$PROJECT_NAME/${{ env.PROJECT_NAME }}/g' action/tests/with-deno-config/deno.jsonc
- name: Deploy to Deno Deploy with deno.jsonc file
uses: ./
with:
root: action/tests/with-deno-config
- name: "Cleanup test: Deploy to Deno Deploy with deno.jsonc file"
run: git checkout action/tests/with-deno-config/deno.jsonc
- name: Deploy with single include
uses: ./
with:
project: happy-rat-64
project: ${{ env.PROJECT_NAME }}
root: action/tests
entrypoint: include_exclude.ts
include: include_exclude.ts
- name: Deploy with comma-separated include
uses: ./
with:
project: happy-rat-64
project: ${{ env.PROJECT_NAME }}
root: action
entrypoint: tests/include_exclude.ts
include: foo, tests/include_exclude.ts,bar
- name: Deploy with comma-separated exclude
uses: ./
with:
project: happy-rat-64
project: ${{ env.PROJECT_NAME }}
root: action/tests
entrypoint: include_exclude.ts
exclude: import_bomb1,import_bomb2
- name: Deploy with multiline exclude
uses: ./
with:
project: happy-rat-64
project: ${{ env.PROJECT_NAME }}
root: action/tests
entrypoint: include_exclude.ts
exclude: |
Expand All @@ -56,7 +66,7 @@ jobs:
- name: Deploy combine include and exclude
uses: ./
with:
project: happy-rat-64
project: ${{ env.PROJECT_NAME }}
root: action
entrypoint: tests/include_exclude.ts
include: tests
Expand All @@ -66,18 +76,18 @@ jobs:
- name: Always exclude node_modules directory
uses: ./
with:
project: happy-rat-64
project: ${{ env.PROJECT_NAME }}
root: action/tests/always_exclude_node_modules
entrypoint: main.ts
- name: Always exclude nested node_modules directory
uses: ./
with:
project: happy-rat-64
project: ${{ env.PROJECT_NAME }}
root: action/tests
entrypoint: always_exclude_node_modules/main.ts
- name: data URL entrypoint
uses: ./
with:
project: happy-rat-64
project: ${{ env.PROJECT_NAME }}
root: action/tests
entrypoint: "data:,Deno.serve(() => new Response())"
15 changes: 11 additions & 4 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@ branding:
icon: globe

inputs:
deno-config:
description: The path to a deno config file that will populate other inputs (optional, deno.json and deno.jsonc are auto-discovered)
required: false
project:
description: The name or ID of the project to deploy
required: true
description: The name or ID of the project to deploy (required)
required: false
entrypoint:
description: The path or URL to the entrypoint file
required: true
description: The path or URL to the entrypoint file (required)
required: false
import-map:
description: The path or URL to an import map file
required: false
Expand All @@ -25,6 +28,10 @@ inputs:
root:
description: The path to the directory containing the code and assets to upload
required: false
import-map-autogen-temp:
description: The path to store the auto-generated import map if deno config file is present (relative to root)
required: false
default: ".importMap.generated.json"

outputs:
deployment-id:
Expand Down
17 changes: 17 additions & 0 deletions action/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,22 @@ jobs:
- name: Deploy to Deno Deploy
uses: denoland/deployctl@v1
with:
# Path to the deno config file
# Auto-discovered from the root directory if it is named "deno.json" or "deno.jsonc".
# If present, other inputs will defaults to their value in the config file.
# Optional.
deno-config:

# Name of the project on Deno Deploy
# Required.
# Defaults to "deploy.project" of deno config file if present.
project:

# Entrypoint location executed by Deno Deploy
# The entrypoint can be a relative path or an absolute URL.
# If it is a relative path, it will be resolved relative to the `root` directory.
# Required.
# Defaults to "deploy.entrypoint" of deno config file if present.
entrypoint:

# Root directory to deploy
Expand All @@ -67,17 +75,26 @@ jobs:
# Filter which files to include in the deployment
# It supports a single file, multiple files separated by a comma or by a newline
# Optional.
# Defaults to "include" of deno config file if present.
include:

# Filter which files to exclude in the deployment
# It supports a single file, multiple files separated by a comma or by a newline
# Optional.
# Defaults to "exclude" of deno config file if present.
exclude:

# Location of an import map
# Must be relative to root directory
# Optional.
# Defaults to "importMap" of deno config file if present.
import-map:

# Location of the auto-generated import map if the deno config file contains
# an "imports" field (allowing support for jsonc config files).
# Relative to root directory
# Optional.
import-map-autogen-temp:
```

## Examples
Expand Down
69 changes: 64 additions & 5 deletions action/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,76 @@ import {
walk,
} from "./deps.js";
import process from "node:process";
import { jsonc } from "jsonc";
import { existsSync } from "node:fs";
import { readFile, writeFile } from "node:fs/promises";

// The origin of the server to make Deploy requests to.
const ORIGIN = process.env.DEPLOY_API_ENDPOINT ?? "https://dash.deno.com";

async function main() {
const projectId = core.getInput("project", { required: true });
const entrypoint = core.getInput("entrypoint", { required: true });
const importMap = core.getInput("import-map", {});
const include = core.getMultilineInput("include", {});
const exclude = core.getMultilineInput("exclude", {});
// Try to resolve and parse a deno config file
const cwd = resolve(process.cwd(), core.getInput("root", {}));
let denoConfig = core.getInput("deno-config", {});
let denoConfigHasDeployInfo = false;
const denoParsedConfig = {};
for (let path of [denoConfig, "deno.json", "deno.jsonc"].filter(Boolean)) {
path = resolve(cwd, path);
if (existsSync(path)) {
denoConfig = path;
break;
}
}
if (denoConfig) {
core.info(`Found a Deno configuration file: ${denoConfig}`);
Object.assign(
denoParsedConfig,
jsonc.parse(await readFile(denoConfig, "utf-8")),
);
// Defaults deno deploy inputs if present in configuration file
if (denoParsedConfig.deploy) {
core.info(`The configuration file has a "deploy" field`);
if (
denoParsedConfig.deploy.project && denoParsedConfig.deploy.entrypoint
) {
denoConfigHasDeployInfo = true;
core.info(`The "deploy" field seems to be valid`);
} else {
core.warning(
`Could not read "project" and "entrypoint" values from the "deploy" field of the configuration file`,
);
}
}
// Create an temporary import map if present in configuration file
// This lets user use deno.jsonc files as import-map since jsonc is not directly supported
if (denoParsedConfig.imports) {
core.info(`The configuration file has a "imports" field`);
denoParsedConfig.importMap = resolve(
cwd,
core.getInput("import-map-autogen-temp", {}),
);
await writeFile(
denoParsedConfig.importMap,
JSON.stringify({ imports: denoParsedConfig.imports }),
);
core.info(
`Created temporary import map in ${denoParsedConfig.importMap}`,
);
}
}

const projectId =
core.getInput("project", { required: !denoConfigHasDeployInfo }) ||
denoParsedConfig.deploy?.project;
const entrypoint =
core.getInput("entrypoint", { required: !denoConfigHasDeployInfo }) ||
denoParsedConfig.deploy?.entrypoint;
const importMap = core.getInput("import-map", {}) ||
(denoParsedConfig.importMap ?? "");
const include = denoParsedConfig.deploy?.include ||
core.getMultilineInput("include", {});
const exclude = denoParsedConfig.deploy?.exclude ||
core.getMultilineInput("exclude", {});

if (github.context.eventName === "pull_request") {
const pr = github.context.payload.pull_request;
Expand Down
1 change: 1 addition & 0 deletions action/node_modules/.bin/mkdirp

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

105 changes: 105 additions & 0 deletions action/node_modules/.package-lock.json

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

Loading
Loading