Skip to content

Commit

Permalink
fix: Aliased branch previews now work properly and improved the PR co…
Browse files Browse the repository at this point in the history
…mment summary (#5)
  • Loading branch information
AdrianGonz97 authored Jan 10, 2024
1 parent 849e9b7 commit 22a3ffc
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 57 deletions.
8 changes: 4 additions & 4 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ runs:
main: 'dist/index.js'
outputs:
id:
description: "The ID of the pages deployment"
description: 'The ID of the pages deployment'
url:
description: "The URL of the pages deployment"
description: 'The URL of the pages deployment'
alias:
description: "The alias, if it exists, otherwise the deployment URL"
description: 'The alias, if it exists, otherwise the deployment URL'
environment:
description: "The environment that was deployed to"
description: 'The environment that was deployed to'
42 changes: 19 additions & 23 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22261,15 +22261,13 @@ async function createPRComment(opts) {
if (!isPR)
return;
const messageId = `deployment-comment:${config.projectName}`;
const deploymentLogUrl = `${import_github2.context.serverUrl}/${import_github2.context.repo.owner}/${import_github2.context.repo.repo}/actions/runs/${import_github2.context.runId}`;
const body = `<!-- ${messageId} -->

### ${opts.title}
| Name | Link |
| :--- | :--- |
| Latest commit | ${import_github2.context.payload.pull_request?.head.sha || import_github2.context.ref} |
| Latest deploy log | ${import_github2.context.serverUrl}/${import_github2.context.repo.owner}/${import_github2.context.repo.repo}/actions/runs/${import_github2.context.runId} |
| Preview URL | ${opts.previewUrl} |
| Environment | ${opts.environment} |
### \u26A1 Cloudflare Pages Deployment
| Name | Status | Preview | Last Commit |
| :--- | :----- | :------ | :---------- |
| **${config.projectName}** | ${opts.status} ([View Log](${deploymentLogUrl})) | ${opts.previewUrl} | ${import_github2.context.payload.pull_request?.head.sha || import_github2.context.ref} |
`;
const existingComment = await findExistingComment({
octokit: opts.octokit,
Expand Down Expand Up @@ -23418,9 +23416,8 @@ async function main() {
const octokit = (0, import_github4.getOctokit)(config.githubToken);
await createPRComment({
octokit,
title: "\u26A1\uFE0F Preparing Cloudflare Pages deployment",
previewUrl: "\u{1F528} Building Preview",
environment: "..."
status: "\u{1F528} Building",
previewUrl: "..."
});
let githubDeployment;
if (config.deploymentName.length > 0) {
Expand All @@ -23431,15 +23428,8 @@ async function main() {
});
}
const pagesDeployment = await createPagesDeployment(productionEnvironment);
(0, import_core3.setOutput)("id", pagesDeployment.id);
(0, import_core3.setOutput)("url", pagesDeployment.url);
(0, import_core3.setOutput)("environment", pagesDeployment.environment);
let alias = pagesDeployment.url;
if (!productionEnvironment && pagesDeployment.aliases && pagesDeployment.aliases.length > 0) {
alias = pagesDeployment.aliases[0];
}
(0, import_core3.setOutput)("alias", alias);
await createJobSummary({ deployment: pagesDeployment, aliasUrl: alias });
await createJobSummary({ deployment: pagesDeployment, aliasUrl: pagesDeployment.url });
if (githubDeployment) {
await createGithubDeploymentStatus({
octokit,
Expand All @@ -23450,14 +23440,20 @@ async function main() {
cfDeploymentId: pagesDeployment.id
});
}
await new Promise((resolve) => setTimeout(resolve, 5e3));
const deployment = await getPagesDeployment();
if (!productionEnvironment && deployment.aliases && deployment.aliases.length > 0) {
alias = deployment.aliases[0];
}
await createPRComment({
octokit,
title: "\u2705 Successful Cloudflare Pages deployment",
previewUrl: pagesDeployment.url,
environment: pagesDeployment.environment
status: "\u2705 Ready",
previewUrl: `[Visit Preview](${alias})`
});
await new Promise((resolve) => setTimeout(resolve, 5e3));
const deployment = await getPagesDeployment();
(0, import_core3.setOutput)("id", deployment.id);
(0, import_core3.setOutput)("url", deployment.url);
(0, import_core3.setOutput)("environment", deployment.environment);
(0, import_core3.setOutput)("alias", alias);
await createJobSummary({ deployment, aliasUrl: alias });
}
try {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "refined-cf-pages-action",
"version": "1.0.0",
"version": "1.0.1",
"description": "Publish to Cloudflare Pages",
"main": "dist/index.js",
"scripts": {
Expand Down
19 changes: 8 additions & 11 deletions src/comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,27 +42,24 @@ export async function findExistingComment(opts: FindExistingCommentOpts) {

type CreatePRCommentOpts = {
octokit: Octokit;
title: string;
previewUrl: string;
environment: string;
status: string;
};

export async function createPRComment(opts: CreatePRCommentOpts) {
if (!isPR) return;

const messageId = `deployment-comment:${config.projectName}`;
const deploymentLogUrl = `${context.serverUrl}/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`;

const body = `<!-- ${messageId} -->
### ${opts.title}
| Name | Link |
| :--- | :--- |
| Latest commit | ${context.payload.pull_request?.head.sha || context.ref} |
| Latest deploy log | ${context.serverUrl}/${context.repo.owner}/${
context.repo.repo
}/actions/runs/${context.runId} |
| Preview URL | ${opts.previewUrl} |
| Environment | ${opts.environment} |
### ⚡ Cloudflare Pages Deployment
| Name | Status | Preview | Last Commit |
| :--- | :----- | :------ | :---------- |
| **${config.projectName}** | ${opts.status} ([View Log](${deploymentLogUrl})) | ${
opts.previewUrl
} | ${context.payload.pull_request?.head.sha || context.ref} |
`;

const existingComment = await findExistingComment({
Expand Down
36 changes: 18 additions & 18 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ async function main() {
const octokit = getOctokit(config.githubToken);
await createPRComment({
octokit,
title: '⚡️ Preparing Cloudflare Pages deployment',
previewUrl: '🔨 Building Preview',
environment: '...',
status: '🔨 Building',
previewUrl: '...',
});

let githubDeployment: Awaited<ReturnType<typeof createGithubDeployment>>;
Expand All @@ -34,17 +33,9 @@ async function main() {
}

const pagesDeployment = await createPagesDeployment(productionEnvironment);
setOutput('id', pagesDeployment.id);
setOutput('url', pagesDeployment.url);
setOutput('environment', pagesDeployment.environment);

let alias = pagesDeployment.url;
if (!productionEnvironment && pagesDeployment.aliases && pagesDeployment.aliases.length > 0) {
alias = pagesDeployment.aliases[0]!; // we can assert that idx 0 exists
}
setOutput('alias', alias);

await createJobSummary({ deployment: pagesDeployment, aliasUrl: alias });
await createJobSummary({ deployment: pagesDeployment, aliasUrl: pagesDeployment.url });

if (githubDeployment) {
await createGithubDeploymentStatus({
Expand All @@ -57,16 +48,25 @@ async function main() {
});
}

// we sleep to give CF enough time to update their deployment status
await new Promise((resolve) => setTimeout(resolve, 5000));
const deployment = await getPagesDeployment();

if (!productionEnvironment && deployment.aliases && deployment.aliases.length > 0) {
alias = deployment.aliases[0]!; // we can assert that idx 0 exists
}

await createPRComment({
octokit,
title: '✅ Successful Cloudflare Pages deployment',
previewUrl: pagesDeployment.url,
environment: pagesDeployment.environment,
status: '✅ Ready',
previewUrl: `[Visit Preview](${alias})`,
});

// we sleep to give CF enough time to update their deployment status
await new Promise((resolve) => setTimeout(resolve, 5000));
const deployment = await getPagesDeployment();
setOutput('id', deployment.id);
setOutput('url', deployment.url);
setOutput('environment', deployment.environment);

setOutput('alias', alias);
await createJobSummary({ deployment, aliasUrl: alias });
}

Expand Down

0 comments on commit 22a3ffc

Please sign in to comment.