forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: set up proper publishing pipeline
- Loading branch information
Showing
16 changed files
with
1,198 additions
and
1,383 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
// @ts-check | ||
import { spawnSync } from "node:child_process"; | ||
import * as fs from "node:fs"; | ||
import * as util from "node:util"; | ||
|
||
/** | ||
* @typedef {typeof import("../../nx.json")} NxConfig | ||
* @typedef {{ tag?: string }} Options | ||
*/ | ||
|
||
/** | ||
* Exports a variable, `publish_react_native_macos`, to signal that we want to | ||
* enable publishing on Azure Pipelines. | ||
* | ||
* Note that pipelines need to read this variable separately and do the actual | ||
* work to publish bits. | ||
*/ | ||
function enablePublishingOnAzurePipelines() { | ||
console.log(`##vso[task.setvariable variable=publish_react_native_macos]1`); | ||
} | ||
|
||
/** | ||
* Loads Nx configuration. | ||
* @returns {NxConfig} | ||
*/ | ||
function loadNxConfig(configFile = "nx.json") { | ||
const nx = fs.readFileSync(configFile, { encoding: "utf-8" }); | ||
return JSON.parse(nx); | ||
} | ||
|
||
/** | ||
* Returns the currently checked out branch. Note that this function prefers | ||
* predefined CI environment variables over local clone. | ||
* @returns {string} | ||
*/ | ||
function getCurrentBranch() { | ||
// https://learn.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=azure-devops&tabs=yaml#build-variables-devops-services | ||
const adoSourceBranchName = process.env["BUILD_SOURCEBRANCHNAME"]; | ||
if (adoSourceBranchName) { | ||
return adoSourceBranchName.replace(/^refs\/heads\//, ""); | ||
} | ||
|
||
// Depending on how the repo was cloned, HEAD may not exist. We only use this | ||
// method as fallback. | ||
const { stdout } = spawnSync("git", ["rev-parse", "--abbrev-ref", "HEAD"]); | ||
return stdout.toString().trim(); | ||
} | ||
|
||
/** | ||
* @param {string} branch | ||
* @param {Options} options | ||
* @returns {{ npmTag: string; prerelease?: string; }} | ||
*/ | ||
function getStableTag(branch, { tag }) { | ||
switch (tag) { | ||
case "latest": | ||
return { npmTag: tag }; | ||
|
||
case "next": | ||
return { npmTag: tag, prerelease: "rc" }; | ||
|
||
default: | ||
return { npmTag: "v" + branch }; | ||
} | ||
} | ||
|
||
/** | ||
* Returns whether the given branch is considered main branch. | ||
* @param {string} branch | ||
*/ | ||
function isMainBranch(branch) { | ||
// There is currently no good way to consistently get the main branch. We | ||
// hardcode the value for now. | ||
return branch === "main"; | ||
} | ||
|
||
/** | ||
* Returns whether the given branch is considered a stable branch. | ||
* @param {string} branch | ||
*/ | ||
function isStableBranch(branch) { | ||
return /^\d+\.\d+-stable$/.test(branch); | ||
} | ||
|
||
/** | ||
* Verifies the configuration and enables publishing on CI. | ||
* @param {NxConfig} config | ||
* @param {string} currentBranch | ||
* @param {string} tag | ||
* @param {string} [prerelease] | ||
* @returns {asserts config is NxConfig["release"]} | ||
*/ | ||
function enablePublishing({ defaultBase, release: config }, currentBranch, tag, prerelease) { | ||
/** @type {string[]} */ | ||
const errors = []; | ||
|
||
// `defaultBase` determines what we diff against when looking for tags or | ||
// released version and must therefore be set to either the main branch or one | ||
// of the stable branches. | ||
if (currentBranch !== defaultBase) { | ||
errors.push(`'defaultBase' must be set to '${currentBranch}'`); | ||
} | ||
|
||
// Determines whether we need to add "nightly" or "rc" to the version string. | ||
const { currentVersionResolverMetadata, preid } = config.version.generatorOptions; | ||
if (preid !== prerelease) { | ||
errors.push(`'release.version.generatorOptions.preid' must be set to '${prerelease || ""}'`); | ||
} | ||
|
||
// What the published version should be tagged as e.g., "latest" or "nightly". | ||
if (currentVersionResolverMetadata.tag !== tag) { | ||
errors.push(`'release.version.generatorOptions.currentVersionResolverMetadata.tag' must be set to '${tag}'`); | ||
} | ||
|
||
if (errors.length > 0) { | ||
for (const e of errors) { | ||
console.error("❌", e); | ||
} | ||
throw new Error("Nx Release is not correctly configured for the current branch"); | ||
} | ||
|
||
enablePublishingOnAzurePipelines(); | ||
} | ||
|
||
/** | ||
* @param {Options} options | ||
*/ | ||
function main(options) { | ||
const branch = getCurrentBranch(); | ||
if (!branch) { | ||
throw new Error("Could not get current branch"); | ||
} | ||
|
||
const config = loadNxConfig(); | ||
if (isMainBranch(branch)) { | ||
enablePublishing(config, branch, "nightly", "nightly"); | ||
} else if (isStableBranch(branch)) { | ||
const { npmTag, prerelease } = getStableTag(branch, options); | ||
enablePublishing(config, branch, npmTag, prerelease); | ||
} | ||
} | ||
|
||
const { values } = util.parseArgs({ | ||
args: process.argv.slice(2), | ||
options: { | ||
tag: { | ||
type: "string", | ||
default: "next", | ||
}, | ||
}, | ||
strict: true, | ||
allowNegative: true, | ||
}); | ||
|
||
main(values); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
parameters: | ||
# If this is a new stable branch, change `publishTag` to `latest` when going stable | ||
latestStableBranch: '0.76-stable' | ||
publishTag: 'next' | ||
|
||
steps: | ||
- script: | | ||
yarn install | ||
displayName: Install npm dependencies | ||
- script: | | ||
node .ado/scripts/prepublish-check.mjs --tag nightly | ||
displayName: Verify release config (nightly) | ||
condition: ${{ eq(variables['Build.SourceBranch'], 'refs/heads/main') }} | ||
- script: | | ||
node .ado/scripts/prepublish-check.mjs --tag ${{ parameters['publishTag'] }} | ||
displayName: Verify release config (latest) | ||
condition: ${{ eq(variables['Build.SourceBranchName'], parameters['latestStableBranch']) }} | ||
- script: | | ||
node .ado/scripts/prepublish-check.mjs | ||
displayName: Verify release config (stable) | ||
condition: ${{ endsWith(variables['Build.SourceBranchName'], '-stable') }} | ||
- script: | | ||
yarn nx release --dry-run | ||
displayName: Version and publish packages (dry run) | ||
condition: ${{ ne(variables['publish_react_native_macos'], '1') }} | ||
- script: | | ||
#yarn nx release --yes | ||
yarn nx release --dry-run | ||
env: | ||
NODE_AUTH_TOKEN: $(npmAuthToken) | ||
displayName: Version and publish packages | ||
condition: ${{ eq(variables['publish_react_native_macos'], '1') }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{ | ||
"$schema": "./node_modules/nx/schemas/nx-schema.json", | ||
"defaultBase": "main", | ||
"targetDefaults": { | ||
"build": { | ||
"dependsOn": ["^build"] | ||
} | ||
}, | ||
"release": { | ||
"changelog": { | ||
"projectChangelogs": false, | ||
"workspaceChangelog": false | ||
}, | ||
"projects": ["packages/react-native", "packages/virtualized-lists"], | ||
"projectsRelationship": "independent", | ||
"versionPlans": true, | ||
"version": { | ||
"generatorOptions": { | ||
"currentVersionResolver": "registry", | ||
"currentVersionResolverMetadata": { | ||
"tag": "nightly" | ||
}, | ||
"fallbackCurrentVersionResolver": "disk", | ||
"preid": "nightly", | ||
"skipLockFileUpdate": true | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"$schema": "./node_modules/nx/schemas/nx-schema.json", | ||
"defaultBase": "main", | ||
"targetDefaults": { | ||
"build": { | ||
"dependsOn": ["^build"] | ||
} | ||
}, | ||
"release": { | ||
"changelog": { | ||
"projectChangelogs": false, | ||
"workspaceChangelog": false | ||
}, | ||
"projects": ["packages/*"], | ||
"projectsRelationship": "fixed", | ||
"versionPlans": true, | ||
"version": { | ||
"generatorOptions": { | ||
"currentVersionResolver": "disk", | ||
"currentVersionResolverMetadata": { | ||
"tag": "latest" | ||
}, | ||
"skipLockFileUpdate": true | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,8 +29,5 @@ | |
}, | ||
"devDependencies": { | ||
"@babel/core": "^7.25.2" | ||
}, | ||
"beachball": { | ||
"shouldPublish": false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,8 +45,5 @@ | |
}, | ||
"engines": { | ||
"node": ">=18" | ||
}, | ||
"beachball": { | ||
"shouldPublish": false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.