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

Upgrade to Node v18, fix deploy-preview CI job #236

Merged
merged 6 commits into from
Oct 20, 2023
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
9 changes: 2 additions & 7 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ version: 2.1
references:
setup_env: &setup_env
docker:
- image: cimg/node:16.14
- image: cimg/node:18.18
save_cache: &save_cache
key: v2-dependency-cache-{{ checksum "yarn.lock" }}
paths:
Expand Down Expand Up @@ -76,12 +76,7 @@ jobs:
- restore_cache: *restore_cache
- attach_workspace: { at: "." }
- store_artifacts: { path: packages/docs/dist }
- run:
name: Submit Github comment with links to built artifacts
command: |
artifacts=$(curl -X GET "https://circleci.com/api/v2/project/github/palantir/documentalist/$CIRCLE_BUILD_NUM/artifacts" -H "Accept: application/json" -u "$CIRCLE_AUTH_TOKEN:")
echo $artifacts > ./scripts/artifacts.json
node ./scripts/circle-build-preview.js
- run: ./scripts/submit-preview-comment.sh
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this PR supposed to hav ea comment on it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good Q, the preview comment is not working since we don't have the API token being injected right now but we do have docs built and saved as an artifact, which is most of the important part: https://app.circleci.com/pipelines/github/palantir/documentalist/389/workflows/8aba4d88-d87e-4dd5-a45f-ffdda3fad7a5/jobs/2679/artifacts


deploy-npm:
<<: *setup_env
Expand Down
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v16.14
v18.18
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"gh-pages": "^5.0.0",
"lerna": "^6.6.1",
"npm-run-all": "^4.1.5",
"octokit": "^3.1.1",
"prettier": "^2.8.4",
"tslint-plugin-prettier": "^2.3.0",
"yarn-deduplicate": "^6.0.1"
Expand Down
2 changes: 1 addition & 1 deletion packages/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"typescript": "~4.6.2"
},
"engines": {
"node": ">=12"
"node": ">=18"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think Node 18 is ok to declare as a minimum engine, since Node 16 maintenance LTS is about to end at the end of this month. See Node.js Releases. @blueprintjs/node-build-scripts already declares a minimum Node version of 18.13.

},
"repository": {
"type": "git",
Expand Down
4 changes: 2 additions & 2 deletions packages/compiler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"@types/jest": "^27.4.1",
"@types/js-yaml": "^4.0.5",
"@types/marked": "^4.0.8",
"@types/node": "^16.11.26",
"@types/node": "^18.18.6",
"@types/yargs": "^17.0.24",
"jest": "^27.5.1",
"jest-junit": "^14.0.1",
Expand All @@ -43,7 +43,7 @@
"typescript": "~4.6.2"
},
"engines": {
"node": ">=12"
"node": ">=18"
},
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion packages/docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"typescript": "~4.6.2"
},
"engines": {
"node": ">=12"
"node": ">=18"
},
"repository": {
"type": "git",
Expand Down
4 changes: 2 additions & 2 deletions scripts/circle-build-preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ const bot = require("circle-github-bot").create();
documentation: "packages/docs/dist/index.html",
};

if (!process.env.GH_AUTH_TOKEN) {
if (!process.env.GITHUB_API_TOKEN) {
// simply log artifact URLs if auth token is missed (typical on forks)
Object.keys(ARTIFACTS).forEach(package => console.info(`${ARTIFACTS[package]}: ${getArtifactAnchorLink(package)}`));
process.exit();
}

const links = Object.keys(ARTIFACTS).map(getArtifactAnchorLink).join(" | ");
bot.comment(
process.env.GH_AUTH_TOKEN,
process.env.GITHUB_API_TOKEN,
`
<h3>${bot.commitMessage()}</h3>
Previews: <strong>${links}</strong>
Expand Down
80 changes: 80 additions & 0 deletions scripts/submit-comment-with-artifact-links.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#!/usr/bin/env node
/*
* Copyright 2017 Palantir Technologies, Inc. All rights reserved.
*/

// @ts-check
/* eslint-disable camelcase */

// Submits a comment to the change PR or commit with links to artifacts that
// show the results of the code change being applied.

import dedent from "dedent";
import { execSync } from "node:child_process";
import { basename } from "node:path";
import { Octokit } from "octokit";

/**
* @type {Array<{path: string; url: string;}>}
*/
const { default: artifacts } = await import("./artifacts.json", { assert: { type: "json" }});

if (artifacts.items === undefined) {
throw new Error(
"Unable to read artifacts.json, please make sure the CircleCI API call succeeded with the necessary personal access token.",
);
}

const ARTIFACTS = {
documentation: "packages/docs/dist/index.html",
};

function getArtifactAnchorLink(pkg) {
const artifactInfo = artifacts.items.find(a => a.path === ARTIFACTS[pkg]);
return `<a href="${artifactInfo?.url}">${pkg}</a>`;
}

if (process.env.GITHUB_API_TOKEN) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like there are some old tokens in the CI env variables right now, I'm going to try those out. it those don't work though, I will have to generate & inject a new personal token just like palantir/blueprint#5827 and the preview comments will come from my GitHub account

image

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep, as I sorta expected, the old tokens didn't work. I'll switch to a new one

image

// We can post a comment on the PR if we have the necessary Github.com personal access token with access to this
// repository and PR read/write permissions.
// See https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token#creating-a-fine-grained-personal-access-token
const octokit = new Octokit({ auth: process.env.GITHUB_API_TOKEN });

const mainDocumentationLink = getArtifactAnchorLink("documentation");
const currentGitCommitMessage = execSync('git --no-pager log --pretty=format:"%s" -1')
.toString()
.trim()
.replace(/\\"/g, '\\\\"');
const commentBody = dedent`
Build preview link for commit "${currentGitCommitMessage}": <strong>${mainDocumentationLink}</strong>

<em>This is an automated comment from the deploy-preview CircleCI job.</em>
`;

const repoParams = {
owner: "palantir",
repo: "documentalist",
};

if (process.env.CIRCLE_PULL_REQUEST) {
// attempt to comment on the PR as an "issue comment" (not a review comment)
await octokit.rest.issues.createComment({
...repoParams,
issue_number: parseInt(basename(process.env.CIRCLE_PULL_REQUEST ?? ""), 10),
body: commentBody,
});
} else if (process.env.CIRCLE_SHA1) {
// attempt to comment on the commit if there is no associated PR (this is most useful on the develop branch)
await octokit.rest.repos.createCommitComment({
...repoParams,
commit_sha: process.env.CIRCLE_SHA1,
body: commentBody,
});
}
} else {
// If the access token is missing, simply log artifact URLs (typical in builds on repository forks).
console.warn(
"No Github API token available, so we cannot post a preview comment on this build's PR. This is expected on forks which have enabled CircleCI building.",
);
Object.keys(ARTIFACTS).forEach(pkg => console.info(`${ARTIFACTS[pkg]}: ${getArtifactAnchorLink(pkg)}`));
}
33 changes: 33 additions & 0 deletions scripts/submit-preview-comment.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env bash

# This script submits a Github comment on the current PR with links to built artifacts hosted in CircleCI.
#
# To do this, it first queries the CircleCI API using a personal access token which is stored as an env variable.
# Currently, this is a token generated by @adidahiya.
# See https://support.circleci.com/hc/en-us/articles/360045457592-Access-uploaded-artifact-URL-in-job
# See https://circleci.com/docs/managing-api-tokens/#creating-a-personal-api-token
#
# After querying for artifact information, it delegates to an adjacent Node.js script to parse the links
# and post a Github comment.

set -e
set -o pipefail

if [ -z "${CIRCLE_BUILD_NUM}" ]; then
echo "Not on CircleCI, refusing to run script."
exit 1
fi

if [ -z "${CIRCLE_API_TOKEN}" ]; then
echo "No CircleCI API token available to query for artifact asset URLs from this build."
echo " --> If this is a build on a fork of the main repo, this is expected behavior. You can view artifact URLs through the CircleCI job web UI."
echo " --> If this is a build on the main repo, something is wrong: check the \$CIRCLE_API_TOKEN environment variable."
exit 0
fi

SCRIPTS_DIR=$(dirname "$(readlink -f "$0")")
# See https://circleci.com/docs/api/v2/index.html#operation/getJobArtifacts
artifacts=$(curl --request GET --url "https://circleci.com/api/v2/project/gh/$CIRCLE_PROJECT_USERNAME/$CIRCLE_PROJECT_REPONAME/$CIRCLE_BUILD_NUM/artifacts" --header "authorization: $CIRCLE_API_TOKEN")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

adjusted this based on docs: https://circleci.com/docs/api/v2/index.html#operation/getJobArtifacts

I'm thinking of migrating this to be completely implemented in JS and exporting it for re-use from @blueprintjs/node-build-scripts


echo $artifacts > ./scripts/artifacts.json
node $SCRIPTS_DIR/submit-comment-with-artifact-links.mjs
Loading