Skip to content

test: use branch/alias deploys for e2e test suite to enable automatic deploy cleanup #2752

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

Merged
merged 11 commits into from
Jan 30, 2025
Merged
24 changes: 13 additions & 11 deletions tests/netlify-deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,11 @@ export class NextDeployInstance extends NextInstance {
const deployTitle = process.env.GITHUB_SHA
? `${testName} - ${process.env.GITHUB_SHA}`
: testName
const deployAlias = 'vercel-next-e2e'

const deployRes = await execa(
'npx',
['netlify', 'deploy', '--build', '--message', deployTitle ?? ''],
['netlify', 'deploy', '--build', '--message', deployTitle ?? '', '--alias', deployAlias],
{
cwd: this.testDir,
reject: false,
Expand All @@ -146,22 +147,23 @@ export class NextDeployInstance extends NextInstance {
}

try {
const [url] = new RegExp(/https:.+\.netlify\.app/gm).exec(deployRes.stdout) || []
if (!url) {
throw new Error('Could not extract the URL from the build logs')
const deployUrlRegex = new RegExp(
/https:\/\/app\.netlify\.com\/sites\/(?<siteName>.+)\/deploys\/(?<deployID>[0-9a-f]+)/gm,
).exec(deployRes.stdout)
const [buildLogsUrl] = deployUrlRegex || []
const { deployID, siteName } = deployUrlRegex?.groups || {}

if (!deployID) {
throw new Error('Could not extract DeployID from the build logs')
}
const [deployID] = new URL(url).host.split('--')
this._url = url

this._url = `https://${deployID}--${siteName}.netlify.app/`
this._parsedUrl = new URL(this._url)
this._deployId = deployID
this._shouldDeleteDeploy = !process.env.NEXT_TEST_SKIP_CLEANUP
this._cliOutput = deployRes.stdout + deployRes.stderr
require('console').log(`Deployment URL: ${this._url}`)

const [buildLogsUrl] =
new RegExp(/https:\/\/app\.netlify\.com\/sites\/.+\/deploys\/[0-9a-f]+/gm).exec(
deployRes.stdout,
) || []
require('console').log(`Deployment URL: ${this._url}`)
if (buildLogsUrl) {
require('console').log(`Logs: ${buildLogsUrl}`)
}
Expand Down
20 changes: 14 additions & 6 deletions tests/utils/create-e2e-fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { setNextVersionInFixture } from './next-version-helpers.mjs'
const DEFAULT_SITE_ID = 'ee859ce9-44a7-46be-830b-ead85e445e53'
export const SITE_ID = process.env.NETLIFY_SITE_ID ?? DEFAULT_SITE_ID
const NEXT_VERSION = process.env.NEXT_VERSION || 'latest'
const NETLIFY_DEPLOY_ALIAS = 'next-e2e-tests'

export interface DeployResult {
deployID: string
Expand Down Expand Up @@ -268,7 +269,7 @@ async function deploySite(
console.log(`🚀 Building and deploying site...`)

const outputFile = 'deploy-output.txt'
let cmd = `npx netlify deploy --build --site ${siteId}`
let cmd = `npx netlify deploy --build --site ${siteId} --alias ${NETLIFY_DEPLOY_ALIAS}`

if (packagePath) {
cmd += ` --filter ${packagePath}`
Expand All @@ -278,12 +279,19 @@ async function deploySite(
await execaCommand(cmd, { cwd: siteDir, all: true }).pipeAll?.(join(siteDir, outputFile))
const output = await readFile(join(siteDir, outputFile), 'utf-8')

const [url] = new RegExp(/https:.+\.netlify\.app/gm).exec(output) || []
if (!url) {
throw new Error('Could not extract the URL from the build logs')
const { siteName, deployID } = new RegExp(
/app\.netlify\.com\/sites\/(?<siteName>.+)\/deploys\/(?<deployID>[0-9a-f]+)/gm
).exec(output)?.groups || {}

if (!deployID) {
throw new Error('Could not extract DeployID from the build logs')
}

return {
url: `https://${deployID}--${siteName}.netlify.app/`,
deployID,
logs: output,
}
const [deployID] = new URL(url).host.split('--')
return { url, deployID, logs: output }
}

export async function deleteDeploy(deployID?: string): Promise<void> {
Expand Down
12 changes: 0 additions & 12 deletions tools/e2e/cleanup-deploys.ts

This file was deleted.

Loading