Skip to content

Commit

Permalink
fix: revert to 1.3.0 (bump to 1.6.0) (#33)
Browse files Browse the repository at this point in the history
* fix: revert to 1.3.1

* bump to 1.6.0
  • Loading branch information
healeycodes authored Aug 23, 2024
1 parent 7867de5 commit 7e0ac7d
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 120 deletions.
36 changes: 0 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ This extension contains Azure Pipelines tasks for automatically deploying your A
- [Extension Set Up](#extension-set-up)
- [Basic Pipeline Set Up](#basic-pipeline-set-up)
- [Full Featured Pipeline Set Up](#full-featured-pipeline-set-up)
- [Env Vars](#env-vars)
- [Available Env Vars](#available-env-vars)
- [Extension Reference](#extension-reference)
- [Task: `vercel-deployment-task`](#task-vercel-deployment-task)
- [Task: `vercel-azdo-pr-comment-task`](#task-vercel-azdo-pr-comment-task)
Expand Down Expand Up @@ -94,24 +92,6 @@ This guide will demonstrate how to improve the [Basic Pipeline Set Up](#basic-pi
1. Push these changes to the repository, and set a [Build Policy](#azure-build-policy-set-up) for the `main` branch.
1. Now create a new branch, push a commit, and open a PR against `main`. A new pipeline execution should trigger and it should create a preview deployment on Vercel as well as comment back on the PR with the preview URL.

## Env Vars

The extension provides a set of env vars if the option [Automatically exposing System Environment Variables](https://vercel.com/docs/projects/environment-variables/system-environment-variables) is enabled in the Project Settings. These vars are available in the build step and at run time.

Based on the selected [Framework Preset](https://vercel.com/docs/deployments/configure-a-build#framework-preset) framework-specific env vars are set for the build step like with a [regular integration](https://vercel.com/docs/projects/environment-variables/system-environment-variables#framework-environment-variables).
> Note: This option is currently only available for Next.js.

### Available Env Vars

| DevOps Variable | Generic | Next.js |
| ------------------------------------------------------ | --------------------------- | -------------------------------------- |
| Build.SourceVersion | DEVOPS_GIT_COMMIT_SHA | NEXT_PUBLIC_DEVOPS_GIT_COMMIT_SHA |
| System.PullRequest.SourceBranch/Build.SourceBranchName | DEVOPS_GIT_COMMIT_REF | NEXT_PUBLIC_DEVOPS_GIT_COMMIT_REF |
| System.PullRequest.PullRequestId | DEVOPS_GIT_PULL_REQUEST_ID | NEXT_PUBLIC_DEVOPS_GIT_PULL_REQUEST_ID |
| "devops" | DEVOPS_GIT_PROVIDER | NEXT_PUBLIC_DEVOPS_GIT_PROVIDER |
| System.TeamProjectId | DEVOPS_GIT_REPO_ID | NEXT_PUBLIC_DEVOPS_GIT_REPO_ID |
| System.TeamProject | DEVOPS_GIT_REPO_SLUG | NEXT_PUBLIC_DEVOPS_GIT_REPO_SLUG |

## Extension Reference

### Task: `vercel-deployment-task`
Expand Down Expand Up @@ -194,16 +174,6 @@ The configuration inputs `vercelProjectID`, `vercelOrgID`, and `vercelToken` can

Required: `false`

- `logs`

Enable `--logs` flag for the internal Vercel CLI operations.

Type: `boolean`

Default: `false`

Required: `false`

#### Outputs

- `deploymentURL`
Expand All @@ -212,12 +182,6 @@ The configuration inputs `vercelProjectID`, `vercelOrgID`, and `vercelToken` can

Type: `string`

- `originalDeploymentURL`

Original URL of the deployment. Can be used to create your own alias in a separate task.

Type: `string`

- `deploymentTaskMessage`

The output from the deployment. Can be passed to Vercel Azure DevOps Pull Request Comment Task.
Expand Down
96 changes: 26 additions & 70 deletions vercel-deployment-task-source/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,11 @@ async function getStagingPrefix(orgID: string, token: string): Promise<string> {
return isTeam ? result.stagingPrefix : result.user.stagingPrefix;
}

// https://vercel.com/docs/rest-api/endpoints/projects#find-a-project-by-id-or-name-response
type Framework = 'blitzjs' | 'nextjs' | 'gatsby' | 'remix' | 'astro' | 'hexo' | 'eleventy' | 'docusaurus-2' | 'docusaurus' | 'preact' | 'solidstart-1' | 'solidstart' | 'dojo' | 'ember' | 'vue' | 'scully' | 'ionic-angular' | 'angular' | 'polymer' | 'svelte' | 'sveltekit' | 'sveltekit-1' | 'ionic-react' | 'create-react-app' | 'gridsome' | 'umijs' | 'sapper' | 'saber' | 'stencil' | 'nuxtjs' | 'redwoodjs' | 'hugo' | 'jekyll' | 'brunch' | 'middleman' | 'zola' | 'hydrogen' | 'vite' | 'vitepress' | 'vuepress' | 'parcel' | 'fasthtml' | 'sanity' | 'storybook' | null;
type Project = { autoExposeSystemEnvs: boolean; framework: Framework; name: string; }

async function getProject(
async function getProjectName(
projectId: string,
orgId: string,
token: string
): Promise<Project> {
): Promise<string> {
let apiURL = `https://api.vercel.com/v9/projects/${projectId}`;
if (isTeamID(orgId)) {
apiURL += `?teamId=${orgId}`;
Expand All @@ -76,7 +72,7 @@ async function getProject(
);
}

return result;
return result.name;
}

/**
Expand Down Expand Up @@ -135,8 +131,6 @@ async function run() {

const archive = getBoolInput("archive");

const logs = getBoolInput("logs");

const vercelProjectId = reconcileConfigurationInput(
"vercelProjectId",
"VERCEL_PROJECT_ID",
Expand Down Expand Up @@ -201,59 +195,6 @@ async function run() {
if (archive) {
vercelDeployArgs.push("--archive=tgz");
}

if (logs) {
vercelDeployArgs.push("--logs");
}

const project = await getProject(vercelProjectId, vercelOrgId, vercelToken)

// Get branch name
// If triggered by a PR use `System.PullRequest.SourceBranch` (and replace the `refs/heads/`)
// If not triggered by a PR use `Build.SourceBranchName`
let branchName: string | undefined;
const buildReason = getVariable("Build.Reason");
if (buildReason === "PullRequest") {
branchName = getVariable("System.PullRequest.SourceBranch");
if (branchName) {
branchName = branchName.replace("refs/heads/", "");
}
} else {
branchName = getVariable("Build.SourceBranchName");
}

// adding predefined DevOps variables which can be useful during build as env vars in a similiar style as the regular Vercel git integration would (replacing VERCEL with DEVOPS)
if (project.autoExposeSystemEnvs) {
const addEnvVar = (envVar: string) => {
vercelDeployArgs.push('--build-env', envVar);
vercelDeployArgs.push('--env', envVar);
}

const commitSha = getVariable("Build.SourceVersion");
const pullRequestId = getVariable("System.PullRequest.PullRequestId");
const teamProject = getVariable("System.TeamProject");
const teamProjectId = getVariable("System.TeamProjectId");

addEnvVar(`DEVOPS_GIT_COMMIT_SHA=${commitSha}`);
addEnvVar(`DEVOPS_GIT_COMMIT_REF=${branchName}`);
addEnvVar(`DEVOPS_GIT_PULL_REQUEST_ID=${pullRequestId}`);
addEnvVar(`DEVOPS_GIT_PROVIDER=devops`);
addEnvVar(`DEVOPS_GIT_REPO_ID=${teamProjectId}`);
addEnvVar(`DEVOPS_GIT_REPO_SLUG=${teamProject}`);

// adding framework specific vars as with regular integration (currently only Next.js is supported) https://vercel.com/docs/projects/environment-variables/system-environment-variables#framework-environment-variables
switch (project.framework) {
case 'nextjs':
vercelDeployArgs.push('--build-env', `NEXT_PUBLIC_DEVOPS_GIT_COMMIT_SHA=${commitSha}`);
vercelDeployArgs.push('--build-env', `NEXT_PUBLIC_DEVOPS_GIT_COMMIT_REF=${branchName}`);
vercelDeployArgs.push('--build-env', `NEXT_PUBLIC_DEVOPS_GIT_PULL_REQUEST_ID=${pullRequestId}`);
vercelDeployArgs.push('--build-env', `NEXT_PUBLIC_DEVOPS_GIT_PROVIDER=devops`);
vercelDeployArgs.push('--build-env', `NEXT_PUBLIC_DEVOPS_GIT_REPO_ID=${teamProjectId}`);
vercelDeployArgs.push('--build-env', `NEXT_PUBLIC_DEVOPS_GIT_REPO_SLUG=${teamProject}`);
break;
}
}

const vercelDeploy = vercel.arg(vercelDeployArgs);
({ stdout, stderr, code } = vercelDeploy.execSync());

Expand All @@ -263,20 +204,36 @@ async function run() {
);
}

const originalDeployURL = stdout;
let deployURL = originalDeployURL;
let deployURL = stdout;

if (!deployToProduction) {
// Get branch name
// If triggered by a PR use `System.PullRequest.SourceBranch` (and replace the `refs/heads/`)
// If not triggered by a PR use `Build.SourceBranchName`
let branchName: string | undefined;
const buildReason = getVariable("Build.Reason");
if (buildReason && buildReason === "PullRequest") {
branchName = getVariable("System.PullRequest.SourceBranch");
if (branchName) {
branchName = branchName.replace("refs/heads/", "");
}
} else {
branchName = getVariable("Build.SourceBranchName");
}

if (branchName) {
const stagingPrefix = await getStagingPrefix(vercelOrgId, vercelToken);
const [projectName, stagingPrefix] = await Promise.all([
getProjectName(vercelProjectId, vercelOrgId, vercelToken),
getStagingPrefix(vercelOrgId, vercelToken),
]);
const escapedBranchName = branchName.replace(/[^a-zA-Z0-9\-]-?/g, "-");
/**
* Truncating branch name according to RFC 1035 if necessary
* Maximum length is 63 characters.
*
* Read more: https://vercel.com/guides/why-is-my-vercel-deployment-url-being-shortened
*
* project.name has a fixedLength `x`
* projectName has a fixedLength `x`
* stagingPrefix has a fixedLenght `y`
* .vercel.app has a fixedLength `11`
* two dashes
Expand All @@ -297,8 +254,8 @@ async function run() {
* longer-project-name-feature-prefix-12346-my-second-f.vercel.app
*/
const branchNameAllowedLength =
50 - project.name.length - stagingPrefix.length;
let aliasHostname = `${project.name}-${escapedBranchName}-${stagingPrefix}.vercel.app`;
50 - projectName.length - stagingPrefix.length;
let aliasHostname = `${projectName}-${escapedBranchName}-${stagingPrefix}.vercel.app`;

if (escapedBranchName.length > branchNameAllowedLength) {
// Calculate the maximum length of the branchName by removing the stagingPrefix and the dash
Expand All @@ -319,7 +276,7 @@ async function run() {
}

// Remove the stagingPrefix from the aliasHostname and use the extended aliasingBranchName
aliasHostname = `${project.name}-${aliasingBranchName}.vercel.app`;
aliasHostname = `${projectName}-${aliasingBranchName}.vercel.app`;
}

deployURL = `https://${aliasHostname}`;
Expand Down Expand Up @@ -348,7 +305,6 @@ async function run() {
}
}

setVariable("originalDeploymentURL", originalDeployURL, false, true);
setVariable("deploymentURL", deployURL, false, true);
const message = `Successfully deployed to ${deployURL}`;
setVariable("deploymentTaskMessage", message, false, true);
Expand Down
15 changes: 2 additions & 13 deletions vercel-deployment-task-source/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
"author": "Vercel",
"version": {
"Major": 1,
"Minor": 5,
"Patch": 1
"Minor": 6,
"Patch": 0
},
"instanceNameFormat": "Deploying $(vercelProject) to Vercel",
"inputs": [
Expand Down Expand Up @@ -63,24 +63,13 @@
"label": "Enable compression of the deployment code into a single file before uploading it",
"required": false,
"helpMarkDown": "Enable `--archive=tgz` flag for the internal Vercel CLI operations."
},
{
"name": "logs",
"type": "boolean",
"label": "Enable build log output in the pipeline",
"required": false,
"helpMarkDown": "Enable `--logs` flag for the internal Vercel CLI operations."
}
],
"outputVariables": [
{
"name": "deploymentURL",
"description": "The URL of the deployment."
},
{
"name": "originalDeploymentURL",
"description": "Original URL of the deployment. Can be used to create your own alias in a separate task."
},
{
"name": "deploymentTaskMessage",
"description": "The message output from the deployment. Can be passed to Vercel Azure DevOps Pull Request Comment Task."
Expand Down
2 changes: 1 addition & 1 deletion vss-extension.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"manifestVersion": 1,
"id": "vercel-deployment-extension",
"name": "Vercel Deployment Extension",
"version": "1.5.1",
"version": "1.6.0",
"publisher": "Vercel",
"public": true,
"targets": [
Expand Down

0 comments on commit 7e0ac7d

Please sign in to comment.