Whole bunch of general improvements (#33)

* Whole bunch of general improvements

* debug

* Fix env

* Add all outputs to workflow

* pls fail

* Fix deployment saying prod

* remove debug

* Fix alias issue
pull/35/head
Daniel Walsh 2 years ago committed by GitHub
parent 364c7ca09a
commit 5a764d10b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -19,5 +19,9 @@ jobs:
directory: example
gitHubToken: ${{ secrets.GITHUB_TOKEN }}
id: publish
- name: Deployment URL
run: echo "${{ steps.publish.outputs.url }}"
- name: Outputs
run: |
echo "ID: ${{ steps.publish.outputs.id }}"
echo "URL: ${{ steps.publish.outputs.url }}"
echo "Environment: ${{ steps.publish.outputs.environment }}"
echo "Alias: ${{ steps.publish.outputs.alias }}"

@ -31,6 +31,7 @@ GitHub Action for creating Cloudflare Pages deployments, using the new [Direct U
accountId: YOUR_ACCOUNT_ID
projectName: YOUR_PROJECT_NAME
directory: YOUR_ASSET_DIRECTORY
# Optional: Enable this if you want to have GitHub Deployments triggered
gitHubToken: ${{ secrets.GITHUB_TOKEN }}
```

@ -18,7 +18,7 @@ inputs:
required: true
gitHubToken:
description: "GitHub Token"
required: true
required: false
branch:
description: "The name of the branch you want to deploy to"
required: false

9224
index.js

File diff suppressed because one or more lines are too long

795
package-lock.json generated

File diff suppressed because it is too large Load Diff

@ -21,13 +21,15 @@
},
"homepage": "https://github.com/cloudflare/pages-action#readme",
"dependencies": {
"@actions/core": "^1.8.0",
"@actions/github": "^5.0.1",
"shellac": "^0.7.0",
"undici": "^5.1.1"
"@actions/core": "^1.10.0",
"@actions/github": "^5.1.1",
"@cloudflare/types": "^6.18.16",
"shellac": "^0.7.2",
"undici": "^5.11.0"
},
"devDependencies": {
"esbuild": "^0.14.38",
"@types/node": "^18.11.3",
"esbuild": "^0.15.12",
"husky": "^8.0.1",
"prettier": "^2.6.2",
"typescript": "^4.6.4"

@ -2,61 +2,28 @@ import { getInput, setOutput, setFailed } from "@actions/core";
import { context, getOctokit } from "@actions/github";
import shellac from "shellac";
import { fetch } from "undici";
// TODO: Confirm types
interface Stage {
name: string;
started_on: null | string;
ended_on: null | string;
status: string;
}
interface Deployment {
id: string;
short_id: string;
project_id: string;
project_name: string;
environment: string;
url: string;
created_on: string;
modified_on: string;
latest_stage: Stage;
deployment_trigger: {
type: string;
metadata: {
branch: string;
commit_hash: string;
commit_message: string;
commit_dirty: boolean;
};
};
stages: Stage[];
build_config: {
build_command: null | string;
destination_dir: null | string;
root_dir: null | string;
web_analytics_tag: null | string;
web_analytics_token: null | string;
fast_builds: boolean;
};
env_vars: unknown;
kv_namespaces: Record<string, { namespace_id: string }>;
aliases: null | string[];
is_skipped: boolean;
production_branch: string;
}
import { env } from "process";
import type { Project, Deployment } from '@cloudflare/types';
try {
const apiToken = getInput("apiToken", { required: true });
const accountId = getInput("accountId", { required: true });
const projectName = getInput("projectName", { required: true });
const directory = getInput("directory", { required: true });
const gitHubToken = getInput("gitHubToken", { required: true });
const gitHubToken = getInput("gitHubToken", { required: false });
const branch = getInput("branch", { required: false });
const octokit = getOctokit(gitHubToken);
const getProject = async () => {
const response = await fetch(
`https://api.cloudflare.com/client/v4/accounts/${accountId}/pages/projects/${projectName}`,
{ headers: { Authorization: `Bearer ${apiToken}` } }
);
const { result } = await response.json() as { result: Project };
return result;
}
const createPagesDeployment = async () => {
// TODO: Replace this with an API call to wrangler so we can get back a full deployment response object
await shellac`
@ -79,7 +46,7 @@ try {
return deployment;
};
const createGitHubDeployment = async () => {
const createGitHubDeployment = async (productionEnvironment: boolean, environment: string) => {
const deployment = await octokit.rest.repos.createDeployment({
owner: context.repo.owner,
repo: context.repo.repo,
@ -87,6 +54,8 @@ try {
auto_merge: false,
description: "Cloudflare Pages",
required_contexts: [],
environment,
production_environment: productionEnvironment,
});
if (deployment.status === 201) {
@ -97,11 +66,13 @@ try {
const createGitHubDeploymentStatus = async ({
id,
url,
deploymentId,
environmentName,
productionEnvironment,
}: {
id: number;
url: string;
deploymentId: string;
environmentName: string;
productionEnvironment: boolean;
}) => {
@ -113,14 +84,31 @@ try {
environment: environmentName,
environment_url: url,
production_environment: productionEnvironment,
log_url: `https://dash.cloudflare.com/${accountId}/pages/view/${projectName}/${id}`,
log_url: `https://dash.cloudflare.com/${accountId}/pages/view/${projectName}/${deploymentId}`,
description: "Cloudflare Pages",
state: "success",
});
};
(async () => {
const gitHubDeployment = await createGitHubDeployment();
if (gitHubToken === "") {
return;
}
const project = await getProject();
const githubBranch = env.GITHUB_REF_NAME;
const productionEnvironment = githubBranch === project.production_branch;
let environmentName: string;
if (productionEnvironment) {
environmentName = "Production"
} else {
// Use the branch name
environmentName = `Preview (${githubBranch})`;
}
const gitHubDeployment = await createGitHubDeployment(productionEnvironment, environmentName);
const pagesDeployment = await createPagesDeployment();
@ -128,16 +116,17 @@ try {
setOutput("url", pagesDeployment.url);
setOutput("environment", pagesDeployment.environment);
const url = new URL(pagesDeployment.url);
const productionEnvironment = pagesDeployment.environment === "production";
const environmentName = productionEnvironment
? "Production"
: `Preview (${url.host.split(".")[0]})`;
let alias = pagesDeployment.url;
if (!productionEnvironment && pagesDeployment.aliases && pagesDeployment.aliases.length > 0) {
alias = pagesDeployment.aliases[0];
}
setOutput("alias", alias);
if (gitHubDeployment) {
await createGitHubDeploymentStatus({
id: gitHubDeployment.id,
url: pagesDeployment.url,
deploymentId: pagesDeployment.id,
environmentName,
productionEnvironment,
});

Loading…
Cancel
Save