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

chore: custom wait-for-netlify action #7083

Merged
merged 1 commit into from
Jun 4, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions .github/actions/wait-for-netlify/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: Wait for the Netlify deployment for the current commit to be ready
description: Wait for the Netlify deployment for the current commit to be ready

inputs:
netlify_token:
description: The value of secrets.NETLIFY_TOKEN
required: true
max_timeout:
description: The maximum length of time to keep retrying the Netlify api
retry_interval:
description: How long to wait between retries of the Netlify api

runs:
using: node16
main: index.js
108 changes: 108 additions & 0 deletions .github/actions/wait-for-netlify/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
const core = require('@actions/core');
const github = require('@actions/github');

const NETLIFY_SITE_ID = '128d21c7-b2fe-45ad-b141-9878fcf5de3a'; // https://app.netlify.com/sites/typescript-eslint/overview
const NETLIFY_TOKEN = core.getInput('netlify_token', { required: true });
const MAX_TIMEOUT = core.getInput('max_timeout') || 300000; // 5 minutes
const RETRY_INTERVAL = core.getInput('retry_interval') || 5000; // 5 seconds

const COMMIT_SHA =
github.context.eventName === 'pull_request'
? github.context.payload.pull_request.head.sha
: github.context.sha;
const BRANCH =
github.context.eventName === 'pull_request'
? github.context.payload.pull_request.head.ref
: github.context.ref_name;

if (!COMMIT_SHA || !BRANCH) {
core.setFailed(
`Could not determine the full commit SHA and branch from the GitHub context: ${JSON.stringify(
github.context,
null,
2,
)}`,
);
}

async function run() {
const { NetlifyAPI } = await import('netlify'); // ESM only, cannot be used with `require`
const client = new NetlifyAPI(NETLIFY_TOKEN);

async function getReadyDeploymentForCommitRef() {
console.log(
`Checking if deployment for commit "${COMMIT_SHA}" on branch "${BRANCH}" has state "ready"...`,
);
const deployments = await client.listSiteDeploys({
site_id: NETLIFY_SITE_ID,
branch: BRANCH,
});
console.log(
`Found ${deployments.length} deployments for this branch "${BRANCH}"`,
);
const deploymentForCommit = deployments.find(
deployment => deployment.commit_ref === COMMIT_SHA,
);
if (!deploymentForCommit) {
console.log(
`No deployment found yet for commit "${COMMIT_SHA}" on branch "${BRANCH}"`,
);
return null;
}
if (deploymentForCommit.state !== 'ready') {
console.log(
`Resolve deployment for commit "${COMMIT_SHA}" on branch "${BRANCH}", but it is not ready yet. State: ${deploymentForCommit.state}`,
);
return null;
}
return deploymentForCommit;
}

async function waitUntilReadyDeployment() {
const maxTimeout = new Promise((_, reject) => {
const id = setTimeout(() => {
clearTimeout(id);
reject(
new Error(
`Error: Timed out in ${MAX_TIMEOUT}ms, based on the configured MAX_TIMEOUT.`,
),
);
}, MAX_TIMEOUT);
});

const isReady = new Promise(async (resolve, reject) => {
const checkReady = async () => {
try {
const readyDeployment = await getReadyDeploymentForCommitRef();
if (readyDeployment) {
return resolve({ readyDeployment });
}
console.log(
`Deployment is not ready yet. Retrying in ${RETRY_INTERVAL}ms based on the configured RETRY_INTERVAL...`,
);
setTimeout(checkReady, RETRY_INTERVAL);
} catch (err) {
return reject(err);
}
};
checkReady();
});

return Promise.race([isReady, maxTimeout]);
}

waitUntilReadyDeployment()
.then(({ readyDeployment }) => {
console.log(
`Resolved "ready" deployment with ID: ${readyDeployment.id}, URL: ${readyDeployment.deploy_ssl_url}`,
);
core.setOutput('deploy_id', readyDeployment.id);
core.setOutput('url', readyDeployment.deploy_ssl_url);
process.exit(0);
})
.catch(error => {
core.setFailed(error.message);
});
}

run();
87 changes: 41 additions & 46 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -176,52 +176,47 @@ jobs:
# Sadly 1 day is the minimum
retention-days: 1

## TODO - re-enable once we fix them
# https://github.com/typescript-eslint/typescript-eslint/issues/6508
# website_tests:
# permissions:
# contents: read # to fetch code (actions/checkout)

# name: Website tests
# # We technically do not need to wait for build within the pipeline any more because the build we care about is happening within Netlify, however,
# # it is highly likely that if the CI one fails, the Netlify one will as well, so in order to not waste unncessary Github Actions minutes/resources,
# # we do still keep this requirement here.
# needs: [build]
# runs-on: ubuntu-latest
# steps:
# - name: Checkout
# uses: actions/checkout@v3
# with:
# fetch-depth: 2

# - name: Install
# uses: ./.github/actions/prepare-install
# with:
# node-version: ${{ env.PRIMARY_NODE_VERSION }}

# - name: Install Playwright Browsers
# run: npx playwright install --with-deps

# - name: Wait for Netlify deployment
# uses: probablyup/wait-for-netlify-action@v3.4.0
# id: waitForDeployment
# with:
# site_id: '128d21c7-b2fe-45ad-b141-9878fcf5de3a'
# max_timeout: 300 # 5 minutes
# env:
# NETLIFY_TOKEN: ${{ secrets.NETLIFY_TOKEN }}

# - name: Run Playwright tests against the Netlify deployment
# run: yarn playwright test --reporter=list
# working-directory: packages/website
# env:
# PLAYWRIGHT_TEST_BASE_URL: ${{ steps.waitForDeployment.outputs.url }}

# - if: always()
# uses: actions/upload-artifact@v3
# with:
# name: playwright-report
# path: packages/website/playwright-report
website_tests:
permissions:
contents: read # to fetch code (actions/checkout)

name: Website tests
# We technically do not need to wait for build within the pipeline any more because the build we care about is happening within Netlify, however,
# it is highly likely that if the CI one fails, the Netlify one will as well, so in order to not waste unncessary Github Actions minutes/resources,
# we do still keep this requirement here.
needs: [build]
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 2

- name: Install
uses: ./.github/actions/prepare-install
with:
node-version: ${{ env.PRIMARY_NODE_VERSION }}

- name: Install Playwright Browsers
run: npx playwright install --with-deps

- name: Wait for Netlify deployment
uses: ./.github/actions/wait-for-netlify
id: waitForDeployment
with:
netlify_token: ${{ secrets.NETLIFY_TOKEN }}

- name: Run Playwright tests against the Netlify deployment
run: yarn playwright test --reporter=list
working-directory: packages/website
env:
PLAYWRIGHT_TEST_BASE_URL: ${{ steps.waitForDeployment.outputs.url }}

- if: always()
uses: actions/upload-artifact@v3
with:
name: playwright-report
path: packages/website/playwright-report

upload_coverage:
name: Upload Codecov Coverage
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
"node": "^12.22.0 || ^14.17.0 || >=16.0.0"
},
"devDependencies": {
"@actions/core": "^1.10.0",
"@actions/github": "^5.1.1",
"@babel/code-frame": "^7.18.6",
"@babel/core": "^7.20.2",
"@babel/eslint-parser": "^7.19.1",
Expand Down Expand Up @@ -99,6 +101,7 @@
"make-dir": "^3.1.0",
"markdownlint-cli": "^0.33.0",
"ncp": "^2.0.0",
"netlify": "^13.1.7",
"nx": "16.2.2",
"nx-cloud": "16.0.5",
"patch-package": "^6.4.7",
Expand Down
6 changes: 3 additions & 3 deletions packages/website/tests/playground.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import AxeBuilder from '@axe-core/playwright';
import type { Page } from '@playwright/test';
import { expect, test } from '@playwright/test';

test.describe('Playground', () => {
// TODO: fix these tests and reenable them
test.describe.skip('Playground', () => {
test.beforeEach(async ({ page }) => {
await page.goto('/play');
});
Expand All @@ -11,8 +12,7 @@ test.describe('Playground', () => {
await new AxeBuilder({ page }).analyze();
});

// TODO: fix this test and reenable it
test.skip('Usage', async ({ page }) => {
test('Usage', async ({ page }) => {
// 1. Type some valid code in the playground
await writeInEditor(page, 'let value: string[];');

Expand Down