Skip to content

Commit

Permalink
ci(build-info): compute git metadata using cli
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfortunato committed Apr 23, 2024
1 parent 6efb729 commit 9e3e902
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 23 deletions.
5 changes: 0 additions & 5 deletions .github/workflows/deploy-dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ name: Vercel Preview Deployment
env:
VERCEL_ORG_ID: ${{ secrets.VERCEL_ORG_ID }}
VERCEL_PROJECT_ID: ${{ secrets.VERCEL_PROJECT_ID }}
GIT_REPO_NAME: ${{ github.repository }}
GIT_COMMIT_BRANCH: ${{ github.ref_name }}
GIT_COMMIT_SHA: ${{ github.sha }}
on:
push:
branches:
Expand All @@ -25,8 +22,6 @@ jobs:
run: npm install --global vercel@latest
- name: Pull Vercel Environment Information
run: vercel pull --yes --environment=preview --token=${{ secrets.VERCEL_TOKEN }}
- name: Strip Owner Part from GIT_REPO_NAME
run: echo "GIT_REPO_NAME=${GIT_REPO_NAME##*/}" >> $GITHUB_ENV
- name: Build Project Artifacts
run: vercel build --token=${{ secrets.VERCEL_TOKEN }}
- name: Deploy Project Artifacts to Vercel
Expand Down
27 changes: 18 additions & 9 deletions lib/server-only/buildInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,23 @@ export function getCommitEntryForFile(filepath: string, head: boolean = true) {

/// Returns the commit info for this build
async function getBuildCommitInfo(): Promise<BuildCommitInfo> {
// NOTE: These environment variables are prepoluated in next.config.js
// Assert each required environment variable is a non-empty string.
assertEnvVarExists(process.env.GIT_REPO_NAME, "GIT_REPO_NAME");
assertEnvVarExists(process.env.GIT_COMMIT_SHA, "GIT_COMMIT_SHA");
assertEnvVarExists(process.env.GIT_COMMIT_BRANCH, "GIT_COMMIT_BRANCH");
// NOTE: We assume our build environment has our git repo
// and git history configured, which getCommitEntryForFile already assumes.
// So lets just use the git cli to get the info.
const branch = launchShellCmd("git branch --show-current")?.toString().trim();
if (!branch)
throw "Could not compute build branch using git cli. Are you sure the build environment has git set up?";
const hash = launchShellCmd(`git log -1 --pretty=format:"%H"`)
?.toString()
.trim();
if (!hash)
throw "Could not compute build commit hash using git cli. Are you sure the build environment has git set up?";
const repo = "personal-website"; // This should never change.

return {
repo: process.env.GIT_REPO_NAME,
hash: process.env.GIT_COMMIT_SHA,
branch: process.env.GIT_COMMIT_BRANCH,
repo,
hash,
branch,
};
}

Expand All @@ -56,7 +63,7 @@ function launchShellCmd(cmd: string) {
}
} catch (error) {
console.log(error);
return buffer;
return undefined;
}
return buffer;
}
Expand All @@ -74,3 +81,5 @@ function assertEnvVarExists(
throw new Error(`Environment variable "${variableName}" is not a string.`);
}
}

launchShellCmd("git branch --show-current")?.toString().trim();
9 changes: 0 additions & 9 deletions next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,6 @@ module.exports = (phase, { defaultConfig }) => {
},
output: "standalone",
};
if (phase != PHASE_DEVELOPMENT_SERVER) {
// NOTE: Alternatively, we can make this less imperative by
// returning a different config object and the env field
// On production builds, we should be are vars from
// whatever platform we are using right now its Vercel.
// process.env.GIT_REPO_NAME = process.env.VERCEL_GIT_REPO_SLUG;
// process.env.GIT_COMMIT_SHA = process.env.VERCEL_GIT_COMMIT_SHA;
// process.env.GIT_COMMIT_BRANCH = process.env.VERCEL_GIT_COMMIT_REF;
}

return nextConfig;
};

0 comments on commit 9e3e902

Please sign in to comment.