diff --git a/.env b/.env new file mode 100644 index 000000000000..f67ef17208dc --- /dev/null +++ b/.env @@ -0,0 +1,17 @@ +# These variables are overridden in CI with GitHub Action variables if triggered from pull-request or push; +# Locally, fallback to the current local git status +BRANCH="$(git branch --show-current)" +COMMIT_SHA="$(git rev-parse HEAD)" + +# Read the CesiumJS version from package.json +CESIUM_VERSION=$(npm pkg get version | tr -d '"') + +# Build artifact configuration +BUILD_ARTIFACT_PATH="cesium/${BRANCH}" +BUILD_ARTIFACT_BUCKET="s3://cesium-public-builds/${BUILD_ARTIFACT_PATH}" +BUILD_ARTIFACT_URL="https://ci-builds.cesium.com/${BUILD_ARTIFACT_PATH}" + +INDEX_URL="${BUILD_ARTIFACT_URL}/index.html" +ZIP_URL="${BUILD_ARTIFACT_URL}/Cesium-${CESIUM_VERSION}.zip" +NPM_URL="${BUILD_ARTIFACT_URL}/cesium-${CESIUM_VERSION}.tgz" +COVERAGE_URL="${BUILD_ARTIFACT_URL}/Build/Coverage/index.html" \ No newline at end of file diff --git a/.github/workflows/.env.pull_request b/.github/workflows/.env.pull_request new file mode 100644 index 000000000000..4e83e6a570eb --- /dev/null +++ b/.github/workflows/.env.pull_request @@ -0,0 +1,2 @@ +BRANCH="$(jq -r .pull_request.head.ref ${GITHUB_EVENT_PATH})" +COMMIT_SHA="$(jq -r .pull_request.head.sha ${GITHUB_EVENT_PATH})" \ No newline at end of file diff --git a/.github/workflows/.env.push b/.github/workflows/.env.push new file mode 100644 index 000000000000..ea6d9a0ff956 --- /dev/null +++ b/.github/workflows/.env.push @@ -0,0 +1,2 @@ +BRANCH=${GITHUB_REF_NAME} +COMMIT_SHA=${GITHUB_SHA} \ No newline at end of file diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml new file mode 100644 index 000000000000..6e4848cb6613 --- /dev/null +++ b/.github/workflows/coverage.yml @@ -0,0 +1,45 @@ +name: coverage +on: + pull_request: +concurrency: + group: coverage-${{ github.ref }} + cancel-in-progress: true +jobs: + coverage: + runs-on: ubuntu-latest + env: + AWS_ACCESS_KEY_ID: ${{ secrets.DEV_AWS_ACCESS_KEY_ID }} + AWS_SECRET_ACCESS_KEY: ${{ secrets.DEV_AWS_SECRET_ACCESS_KEY }} + AWS_REGION: us-east-1 + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + permissions: + statuses: write + contents: read + steps: + - uses: actions/checkout@v5 + - name: install node 22 + uses: actions/setup-node@v5 + with: + node-version: "22" + - name: npm install + run: npm install + - name: set status pending + if: ${{ env.AWS_ACCESS_KEY_ID != '' }} + run: | + npx @dotenvx/dotenvx run -f ./.github/workflows/.env.pull_request -- \ + node ./scripts/setCommitStatus.js coverage pending + - name: build + run: npm run build + - name: coverage (firefox) + run: npm run coverage -- --browsers FirefoxHeadless --webgl-stub --failTaskOnError --suppressPassed + - name: upload coverage artifacts + if: ${{ env.AWS_ACCESS_KEY_ID != '' }} + run: | + npx @dotenvx/dotenvx run -f ./.github/workflows/.env.pull_request -f .env \ + -- sh -c \ + 'aws s3 sync ./Build/Coverage $BUILD_ARTIFACT_BUCKET/Build/Coverage --delete' + - name: set status success + if: ${{ env.AWS_ACCESS_KEY_ID != '' }} + run: | + npx @dotenvx/dotenvx run -f ./.github/workflows/.env.pull_request -- \ + node ./scripts/setCommitStatus.js coverage ${{ job.status }} \ No newline at end of file diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 8f4e156b4e0b..03cf4c9dd8ab 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -2,7 +2,7 @@ name: deploy on: push: branches-ignore: - - 'cesium.com' + - "cesium.com" - production concurrency: group: deploy-${{ github.ref }} @@ -14,26 +14,31 @@ jobs: statuses: write contents: read env: - BUILD_VERSION: ${{ github.ref_name }}.${{ github.run_number }} AWS_ACCESS_KEY_ID: ${{ secrets.DEV_AWS_ACCESS_KEY_ID }} AWS_SECRET_ACCESS_KEY: ${{ secrets.DEV_AWS_SECRET_ACCESS_KEY }} AWS_REGION: us-east-1 - BRANCH: ${{ github.ref_name }} GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - GITHUB_REPO: ${{ github.repository }} - GITHUB_SHA: ${{ github.sha }} BASE_URL: /cesium/${{ github.ref_name }}/ - DEPLOYED_URL: https://ci-builds.cesium.com/cesium/${{ github.ref_name }}/ steps: - uses: actions/checkout@v5 - name: install node 22 uses: actions/setup-node@v5 with: - node-version: '22' + node-version: "22" - name: npm install run: npm install - - name: set the version in package.json - run: npm run deploy-set-version -- --buildVersion $BUILD_VERSION + - name: set version in package.json + run: | + npx @dotenvx/dotenvx run -f ./.github/workflows/.env.push \ + -- sh -c \ + 'npm version prerelease --preid $BRANCH --ws --include-workspace-root --no-git-tag-version' + - name: set status pending + if: ${{ env.AWS_ACCESS_KEY_ID != '' }} + run: | + npx @dotenvx/dotenvx run -f ./.github/workflows/.env.push -- \ + node ./scripts/setCommitStatus.js zip pending && \ + node ./scripts/setCommitStatus.js npm pending && \ + node ./scripts/setCommitStatus.js index pending - name: create release zip run: npm run make-zip - name: package cesium module @@ -48,7 +53,9 @@ jobs: - name: deploy to s3 if: ${{ env.AWS_ACCESS_KEY_ID != '' }} run: | - aws s3 sync . s3://cesium-public-builds/cesium/$BRANCH/ \ + npx @dotenvx/dotenvx run -f ./.github/workflows/.env.push -f .env \ + -- sh -c \ + 'aws s3 sync . $BUILD_ARTIFACT_BUCKET \ --cache-control "no-cache" \ --exclude ".git/*" \ --exclude ".github/*" \ @@ -61,7 +68,11 @@ jobs: --exclude "node_modules/*" \ --exclude "scripts/*" \ --exclude "Tools/*" \ - --delete - - name: set status + --delete' + - name: set status success if: ${{ env.AWS_ACCESS_KEY_ID != '' }} - run: npm run deploy-status -- --status success --message Deployed + run: | + npx @dotenvx/dotenvx run -f ./.github/workflows/.env.push -- \ + node ./scripts/setCommitStatus.js zip ${{ job.status }} && \ + node ./scripts/setCommitStatus.js npm ${{ job.status }} && \ + node ./scripts/setCommitStatus.js index ${{ job.status }} diff --git a/.github/workflows/dev.yml b/.github/workflows/dev.yml index 3355278ad295..109320579a1c 100644 --- a/.github/workflows/dev.yml +++ b/.github/workflows/dev.yml @@ -15,7 +15,7 @@ jobs: - name: install node 22 uses: actions/setup-node@v5 with: - node-version: '22' + node-version: "22" - name: npm install run: npm install - name: lint *.js @@ -24,28 +24,6 @@ jobs: run: npm run markdownlint - name: format code run: npm run prettier-check - coverage: - runs-on: ubuntu-latest - env: - AWS_ACCESS_KEY_ID: ${{ secrets.DEV_AWS_ACCESS_KEY_ID }} - AWS_SECRET_ACCESS_KEY: ${{ secrets.DEV_AWS_SECRET_ACCESS_KEY }} - AWS_REGION: us-east-1 - BRANCH: ${{ github.ref_name }} - steps: - - uses: actions/checkout@v5 - - name: install node 22 - uses: actions/setup-node@v5 - with: - node-version: '22' - - name: npm install - run: npm install - - name: build - run: npm run build - - name: coverage (firefox) - run: npm run coverage -- --browsers FirefoxHeadless --webgl-stub --failTaskOnError --suppressPassed - - name: upload coverage artifacts - if: ${{ env.AWS_ACCESS_KEY_ID != '' }} - run: aws s3 sync ./Build/Coverage s3://cesium-public-builds/cesium/$BRANCH/Build/Coverage --delete --color on release-tests: runs-on: ubuntu-latest steps: @@ -53,7 +31,7 @@ jobs: - name: install node 22 uses: actions/setup-node@v5 with: - node-version: '22' + node-version: "22" - name: npm install run: npm install - name: release build @@ -69,7 +47,7 @@ jobs: - name: install node 20 uses: actions/setup-node@v5 with: - node-version: '20' + node-version: "20" - name: npm install run: npm install - name: release build diff --git a/.github/workflows/prod.yml b/.github/workflows/prod.yml index 628d8890ac5e..113ff4d1c1a2 100644 --- a/.github/workflows/prod.yml +++ b/.github/workflows/prod.yml @@ -29,7 +29,6 @@ jobs: AWS_REGION: us-east-1 BRANCH: ${{ github.ref_name }} GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - GITHUB_REPO: ${{ github.repository }} GITHUB_SHA: ${{ github.sha }} steps: - uses: actions/checkout@v5 diff --git a/.github/workflows/sandcastle-dev.yml b/.github/workflows/sandcastle-dev.yml index 1aec62462108..c4fd3415e216 100644 --- a/.github/workflows/sandcastle-dev.yml +++ b/.github/workflows/sandcastle-dev.yml @@ -13,7 +13,6 @@ jobs: AWS_REGION: us-east-1 BRANCH: ${{ github.ref_name }} GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - GITHUB_REPO: ${{ github.repository }} GITHUB_SHA: ${{ github.sha }} steps: - uses: actions/checkout@v5 diff --git a/Documentation/Contributors/BuildGuide/README.md b/Documentation/Contributors/BuildGuide/README.md index 3471d2d628d2..d2137ff4635f 100644 --- a/Documentation/Contributors/BuildGuide/README.md +++ b/Documentation/Contributors/BuildGuide/README.md @@ -153,6 +153,3 @@ Here's the full set of scripts and what they do. - `test-webgl-stub` - Runs all tests using the WebGL stub, which WebGL calls a noop and ignores related test expectations - `test-webgl-validation` - Runs all tests with Karma and enables low-level WebGL validation - `test-release` - Runs all tests on the minified release version of built Cesium -- **Deployment scripts** - - `deploy-status` - Sets the deployment statuses in GitHub, for use in CI - - `deploy-set-version` - Sets the version of `package.json`, for use in CI diff --git a/Documentation/Contributors/ContinuousIntegration/README.md b/Documentation/Contributors/ContinuousIntegration/README.md index 0a9b714951ed..d589a566f5da 100644 --- a/Documentation/Contributors/ContinuousIntegration/README.md +++ b/Documentation/Contributors/ContinuousIntegration/README.md @@ -2,8 +2,9 @@ - [Background](#background) - [Actions and workflows](#actions-and-workflows) + - [Environment variables](#environment-variables) - [Continuous deployment](#continuous-deployment) -- [Configuration](#configuration) +- [Configuration guide](#configuration-guide) - [Configure a different S3 bucket](#configure-a-different-s3-bucket) - [Configure S3 credentials](#configure-s3-credentials) @@ -18,51 +19,52 @@ CesiumJS uses [GitHub Actions](https://docs.github.com/en/actions) for continuou Reusable actions are defined in `/.github/actions/` and workflows in `.github/workflows/`. -A workflow is triggered whenever someone pushes code to the CesiumJS repository, or an external contributor opens a pull request. After the build has completed, at the bottom of the pull request page the status of the build is shown. In the dropdown menu, individual checks are displayed. Logs and deployed artifacts can be accessed by clicking the "Details" link. +Workflows are triggered when a commit is pushed to the `cesium` repository and when a contributor opens or updates a pull request. After the build has completed, the overall status of the build is shown at the bottom of the pull request page. + +In the dropdown menu, individual status checks are displayed. Logs and deployed build artifacts can be accessed by clicking the link associated with the individual check. ![GitHub Action Checks](github_action_checks.png) -The workflow checks for any CesiumJS branch are accessible under the [Branches](https://github.com/CesiumGS/cesium/branches/all) page by clicking the icon next to the branch name. +The status checks for any branch are also accessible under the [Branches](https://github.com/CesiumGS/cesium/branches/all) page by clicking the icon next to the branch name. ![GitHub Branches](github_branches.png) -## Continuous deployment +### Environment variables -Automated deployments make recent code changes available for testing and review without needing to fetch and build locally. We deploy each of the following on a per-branch basis. +Any non-secret environment variables for CI and CD are managed using `.env` files and [dotenvx](https://github.com/dotenvx/dotenvx). -| Artifact | Link (`main` branch) | -| ---------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| Sandcastle | [`https://ci-builds.cesium.com/cesium/main/Apps/Sandcastle/index.html`](https://ci-builds.cesium.com/cesium/main/Apps/Sandcastle/index.html) | -| Documentation | [`https://ci-builds.cesium.com/cesium/main/Build/Documentation/index.html`](https://ci-builds.cesium.com/cesium/main/Build/Documentation/index.html) | -| Coverage results | [`https://ci-builds.cesium.com/cesium/main/Build/Coverage/index.html`](https://ci-builds.cesium.com/cesium/main/Build/Coverage/index.html) | -| Release zip | [`https://ci-builds.cesium.com/cesium/main/..zip`](https://ci-builds.cesium.com/cesium/main/..zip) | -| npm package | [`https://ci-builds.cesium.com/cesium/main/..tgz`](https://ci-builds.cesium.com/cesium/main/..tgz) | +- **Local dev**: `.env` is checked in at the repository root and provides defaults constant values and local fallbacks. + - To run a script or command with these variables configured locally, use `npx @dotenvx/dotenvx run -- `, e.g., `npx @dotenvx/dotenvx run -- node ./scripts/setCommitStatus.js`. + - To expand a variable directly in a command, use a subshell command, e.g., `npx @dotenvx/dotenvx run -- sh -c 'echo "${BRANCH}"'`. +- **GitHub Actions workflow - `push` trigger**: `.github/workflows/.env.push` +- **GitHub Actions workflow - `pull_request` trigger**: `.github/workflows/.env.pull_request` -## Configuration +## Continuous deployment -Additional set up is required for deployment if you do not have commit access to CesiumJS. +Automated deployments make recent code changes available for convenient testing and review—No need to fetch or build locally. In the `cesium` repository, all continuous deployment artifacts are uploaded for commits authored by users with commit access. -### Configure a different S3 bucket +Each of the following are deployed on a per-branch basis. -It is possible to configure your development branch of CesiumJS to deploy build artifacts to a different [AWS S3 Bucket](http://docs.aws.amazon.com/AmazonS3/latest/dev/UsingBucket.html). If you are using the cesium-public-builds bucket and have valid credentials, skip to [Configure S3 Credentials](#configure-s3-credentials) +| Artifact | Link pattern (`main`) | +| ---------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | +| Sandcastle | `https://ci-builds.cesium.com/cesium//Apps/Sandcastle/index.html` (i.e., [`https://ci-builds.cesium.com/cesium/main/Apps/Sandcastle/index.html`](https://ci-builds.cesium.com/cesium/main/Apps/Sandcastle/index.html)) | +| Documentation | `https://ci-builds.cesium.com/cesium//Build/Documentation/index.html` (i.e., [`https://ci-builds.cesium.com/cesium/main/Build/Documentation/index.html`](https://ci-builds.cesium.com/cesium/main/Build/Documentation/index.html)) | +| Coverage results | `https://ci-builds.cesium.com/cesium//Build/Coverage/index.html` (i.e., [`https://ci-builds.cesium.com/cesium/main/Build/Coverage/index.html`](https://ci-builds.cesium.com/cesium/main/Build/Coverage/index.html)) | +| Release zip | `https://ci-builds.cesium.com/cesium//Cesium--.0.zip` (i.e., [`https://ci-builds.cesium.com/cesium/main/Cesium-1.X.X-main.0.zip`](https://ci-builds.cesium.com/cesium/main/Cesium-1.X.X-main.0.zip)) | +| npm package | `https://ci-builds.cesium.com/cesium//cesium--.0.tgz` (i.e., [`https://ci-builds.cesium.com/cesium/main/cesium-1.X.X-main.0.tgz`](https://ci-builds.cesium.com/cesium/main/cesium-1.X.X-main.0.tgz)) | -- In `.gtihub/workflows/dev.yml`, in the following lines, replace "cesium-public-builds" with the name of your S3 bucket. +## Configuration guide -```sh -aws s3 sync ./Build/Coverage s3://cesium-public-builds/cesium/$BRANCH/Build/Coverage --delete --color on -``` +Additional set up is required for deployment _only_ if you do not have commit access to CesiumJS. -```sh -aws s3 sync Build/unzipped/ s3://cesium-public-builds/cesium/$BRANCH/Build/ --cache-control "no-cache" --delete -``` +### Configure a different S3 bucket -- In `gulpfile.js`, edit the following line: +It is possible to configure your development branch of CesiumJS to deploy build artifacts to a different [AWS S3 Bucket](http://docs.aws.amazon.com/AmazonS3/latest/dev/UsingBucket.html). If you are using the default "cesium-public-builds" bucket and have valid credentials, skip to [Configure S3 Credentials](#configure-s3-credentials) -```javascript -const devDeployUrl = "https://ci-builds.cesium.com/cesium/"; -``` +In the environment file `.env`, update the following values: -- Edit the URL to match the URL hosting the S3 bucket specified in the previous step. +- Replace the value of `BUILD_ARTIFACT_BUCKET` with the name of the target S3 bucket. +- Replace the value of `BUILD_ARTIFACT_URL` with the public URL correspondeding to the target S3 bucket. ### Configure S3 credentials diff --git a/gulpfile.js b/gulpfile.js index 28297313b758..4b0c3eca3209 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -2,7 +2,6 @@ import { writeFileSync, copyFileSync, readFileSync, existsSync } from "fs"; import { readFile, writeFile } from "fs/promises"; import { join, basename, resolve, dirname } from "path"; import { exec, execSync } from "child_process"; -import fetch from "node-fetch"; import { createRequire } from "module"; import { finished } from "stream/promises"; @@ -39,7 +38,8 @@ const scope = "cesium"; const require = createRequire(import.meta.url); const packageJson = require("./package.json"); let version = packageJson.version; -if (/\.0$/.test(version)) { +if (/\.\d+\.0$/.test(version)) { + // Removes the patch release ".0" at the end of any non-prelease version version = version.substring(0, version.length - 2); } const karmaConfigFile = resolve("./Specs/karma.conf.cjs"); @@ -54,7 +54,6 @@ function getWorkspaces(onlyDependencies = false) { : packageJson.workspaces; } -const devDeployUrl = process.env.DEPLOYED_URL; const isProduction = process.env.PROD; //Gulp doesn't seem to have a way to get the currently running tasks for setting @@ -554,8 +553,6 @@ async function pruneScriptsForZip(packageJsonPath) { delete scripts.prettier; // Remove deploy tasks - delete scripts["deploy-status"]; - delete scripts["deploy-set-version"]; delete scripts["website-release"]; // Set server tasks to use production flag @@ -698,66 +695,6 @@ export const makeZip = gulp.series(release, async function createZipFile() { return src; }); -export async function deploySetVersion() { - const buildVersion = argv.buildVersion; - if (buildVersion) { - // NPM versions can only contain alphanumeric and hyphen characters - packageJson.version += `-${buildVersion.replace(/[^[0-9A-Za-z-]/g, "")}`; - return writeFile("package.json", JSON.stringify(packageJson, undefined, 2)); - } -} - -export async function deployStatus() { - const status = argv.status; - const message = argv.message; - const deployUrl = `${devDeployUrl}`; - const zipUrl = `${deployUrl}Cesium-${version}.zip`; - const npmUrl = `${deployUrl}cesium-${version}.tgz`; - const coverageUrl = `${deployUrl}Build/Coverage/index.html`; - - return Promise.all([ - setStatus(status, deployUrl, message, "deploy / artifact: deployment"), - setStatus(status, zipUrl, message, "deploy / artifact: zip file"), - setStatus(status, npmUrl, message, "deploy / artifact: npm package"), - setStatus( - status, - coverageUrl, - message, - "deploy / artifact: coverage results", - ), - ]); -} - -async function setStatus(state, targetUrl, description, context) { - // skip if the environment does not have the token - if (!process.env.GITHUB_TOKEN) { - return; - } - - const body = { - state: state, - target_url: targetUrl, - description: description, - context: context, - }; - - const response = await fetch( - `https://api.github.com/repos/${process.env.GITHUB_REPO}/statuses/${process.env.GITHUB_SHA}`, - { - method: "post", - body: JSON.stringify(body), - headers: { - "Content-Type": "application/json", - Authorization: `token ${process.env.GITHUB_TOKEN}`, - "User-Agent": "Cesium", - }, - }, - ); - - const result = await response.json(); - return result; -} - /** * Generates coverage report. * diff --git a/package.json b/package.json index 99ce5e90b601..b0263f53254e 100644 --- a/package.json +++ b/package.json @@ -23,7 +23,7 @@ ], "repository": { "type": "git", - "url": "https://github.com/CesiumGS/cesium.git" + "url": "git+https://github.com/CesiumGS/cesium.git" }, "bugs": { "url": "https://github.com/CesiumGS/cesium/issues" @@ -56,6 +56,7 @@ }, "devDependencies": { "@cesium/eslint-config": "^12.0.0", + "@dotenvx/dotenvx": "^1.51.0", "@playwright/test": "^1.41.1", "chokidar": "^4.0.1", "cloc": "^2.6.0-cloc", @@ -95,7 +96,6 @@ "markdownlint-cli": "^0.45.0", "merge-stream": "^2.0.0", "mkdirp": "^3.0.1", - "node-fetch": "^3.2.10", "open": "^10.0.2", "prettier": "3.6.2", "prismjs": "^1.28.0", @@ -139,8 +139,6 @@ "test-e2e-release-all": "release=true playwright test -c Specs/e2e/playwright.config.js", "test-e2e-report": "playwright show-report Build/Specs/e2e/report", "test-e2e-update": "playwright test -c Specs/e2e/playwright.config.js --project=chromium -u", - "deploy-status": "gulp deployStatus", - "deploy-set-version": "gulp deploySetVersion", "prettier": "prettier --write \"**/*\"", "prettier-check": "prettier --check \"**/*\"", "cspell": "npx cspell lint -c .vscode/cspell.json --unique --no-progress" diff --git a/scripts/setCommitStatus.js b/scripts/setCommitStatus.js new file mode 100644 index 000000000000..1c0b1b4c3aaf --- /dev/null +++ b/scripts/setCommitStatus.js @@ -0,0 +1,132 @@ +import yargs from "yargs"; +import { hideBin } from "yargs/helpers"; +import "@dotenvx/dotenvx/config"; + +const GITHUB_TOKEN = process.env.GITHUB_TOKEN; +const GITHUB_REPO = process.env.GITHUB_REPOSITORY; +const GITHUB_WORKFLOW = process.env.GITHUB_WORKFLOW; +const COMMIT_SHA = process.env.COMMIT_SHA; +const CESIUM_VERSION = process.env.CESIUM_VERSION; + +export async function setCommitStatus({ status, url, context, message }) { + if (!GITHUB_TOKEN || GITHUB_TOKEN === "") { + throw new Error(`Environment variable is not defined: "GITHUB_TOKEN"`); + } + + if (!GITHUB_REPO || GITHUB_REPO === "") { + throw new Error(`Environment variable is not defined: "GITHUB_REPO"`); + } + + if (!COMMIT_SHA || COMMIT_SHA === "") { + throw new Error(`Environment variable is not defined: "COMMIT_SHA"`); + } + + const body = { + state: status, + context: context, + target_url: url, + description: message, + }; + + console.log(`Posting ${GITHUB_REPO} commit status of ${COMMIT_SHA}: + ${JSON.stringify(body, null, 2)}`); + + const response = await fetch( + `https://api.github.com/repos/${GITHUB_REPO}/statuses/${COMMIT_SHA}`, + { + method: "post", + body: JSON.stringify(body), + headers: { + "Content-Type": "application/json", + Authorization: `token ${GITHUB_TOKEN}`, + "User-Agent": "Cesium", + }, + }, + ); + + if (!response.ok) { + throw new Error(`Responded with ${response.status}`); + } + + const result = await response.json(); + return result; +} + +const getArtifactContext = (artifact) => { + if (!GITHUB_WORKFLOW || GITHUB_WORKFLOW === "") { + return `artifact: ${artifact}`; + } + + return `${GITHUB_WORKFLOW} / artifact: ${artifact}`; +}; + +await yargs() + .command( + "* [context] [url] [message]", + "set commit status, for example, to link to a build artifact", + (yargs) => + yargs + .positional("status", { + type: "string", + choices: ["error", "failure", "pending", "success"], + describe: "The state that the commit will be marked as.", + }) + .positional("context", { + type: "string", + describe: "A label to differentiate this status check.", + }) + .positional("url", { + type: "string", + describe: "The linked URL to associate with this status.", + }) + .positional("message", { + type: "string", + describe: "A short description of this status.", + }), + setCommitStatus, + ) + .command( + "coverage ", + "set deployment status of coverage results", + () => {}, + async ({ status }) => + setCommitStatus({ + status, + url: process.env.COVERAGE_URL, + context: getArtifactContext("coverage report"), + }), + ) + .command( + "zip ", + "set deployment status of the release zip file", + () => {}, + async ({ status }) => + setCommitStatus({ + status, + url: process.env.ZIP_URL, + context: getArtifactContext(`Cesium-${CESIUM_VERSION}.zip`), + }), + ) + .command( + "npm ", + "set deployment status of the npm package", + () => {}, + async ({ status }) => + setCommitStatus({ + status, + url: process.env.NPM_URL, + context: getArtifactContext(`cesium-${CESIUM_VERSION}.tgz`), + }), + ) + .command( + "index ", + "set deployment status of the static build", + () => {}, + async ({ status }) => + setCommitStatus({ + status, + url: process.env.INDEX_URL, + context: getArtifactContext("index.html"), + }), + ) + .parse(hideBin(process.argv));