Skip to content

Commit 0d7b006

Browse files
authored
ci: move "NPM Publish (Dry Run)" to Github Actions (#2532)
## Summary: Our NX release dry run job depends on a read-only GitHub token (I'm pretty sure...) which is hard to get with Azure Pipelines. Let's move the job to Github Actions. In the process, I also updated both the dry run and the Azure Pipelines publish job to read our publish tag directly from `nx.json` instead of having it set in YAML. Not sure why we didn't do that earlier.. I'll have to go check those PRs to see if we had a strong reason. ## Test Plan: CI should pass
1 parent 214ffac commit 0d7b006

File tree

7 files changed

+128
-104
lines changed

7 files changed

+128
-104
lines changed

.ado/apple-pr.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ stages:
2626
dependsOn: []
2727
jobs:
2828
- template: /.ado/jobs/test-javascript.yml@self
29-
- template: /.ado/jobs/npm-publish-dry-run.yml@self
3029

3130
# https://github.com/microsoft/react-native-macos/issues/2344
3231
# The Verdaccio server consistently hangs on creation, which is required for the integration tests

.ado/jobs/npm-publish-dry-run.yml

Lines changed: 0 additions & 10 deletions
This file was deleted.

.ado/jobs/npm-publish.yml

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,57 @@ jobs:
1616
targetPath: $(System.DefaultWorkingDirectory)
1717
artifactName: github-npm-js-publish
1818
steps:
19-
- template: /.ado/templates/npm-publish-steps.yml@self
19+
- checkout: self
20+
clean: true
21+
fetchFilter: blob:none
22+
persistCredentials: true
23+
24+
- template: /.ado/templates/configure-git.yml@self
25+
26+
- script: |
27+
PUBLISH_TAG=$(jq -r '.release.version.generatorOptions.currentVersionResolverMetadata.tag' nx.json)
28+
echo "##vso[task.setvariable variable=publishTag]$PUBLISH_TAG"
29+
echo "Using publish tag from nx.json: $PUBLISH_TAG"
30+
displayName: Read publish tag from nx.json
31+
32+
- script: |
33+
yarn install
34+
displayName: Install npm dependencies
35+
36+
- script: |
37+
node .ado/scripts/prepublish-check.mjs --verbose --skip-auth --tag $(publishTag)
38+
displayName: Verify release config
39+
40+
- script: |
41+
echo Target branch: $(System.PullRequest.TargetBranch)
42+
yarn nx release --dry-run --verbose
43+
displayName: Version and publish packages (dry run)
44+
condition: and(succeeded(), ne(variables['publish_react_native_macos'], '1'))
45+
46+
# Disable Nightly publishing on the main branch
47+
- ${{ if endsWith(variables['Build.SourceBranchName'], '-stable') }}:
48+
- script: |
49+
echo "//registry.npmjs.org/:_authToken=$(npmAuthToken)" > ~/.npmrc
50+
node .ado/scripts/prepublish-check.mjs --verbose --tag $(publishTag)
51+
displayName: Set and validate npm auth
52+
condition: and(succeeded(), eq(variables['publish_react_native_macos'], '1'))
53+
54+
- script: |
55+
git switch $(Build.SourceBranchName)
56+
yarn nx release --skip-publish --verbose
57+
env:
58+
GITHUB_TOKEN: $(githubAuthToken)
59+
displayName: Version Packages and Github Release
60+
condition: and(succeeded(), eq(variables['publish_react_native_macos'], '1'))
61+
62+
- script: |
63+
if [[ -f .rnm-publish ]]; then
64+
yarn nx release publish --tag ${{ parameters['publishTag'] }} --excludeTaskDependencies
65+
fi
66+
displayName: Publish packages
67+
condition: and(succeeded(), eq(variables['publish_react_native_macos'], '1'))
68+
69+
- script: |
70+
rm -f ~/.npmrc
71+
displayName: Remove npmrc if it exists
72+
condition: always()

.ado/scripts/prepublish-check.mjs

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { spawnSync } from "node:child_process";
33
import * as fs from "node:fs";
44
import * as util from "node:util";
55

6-
const ADO_PUBLISH_PIPELINE = ".ado/templates/npm-publish-steps.yml";
76
const NX_CONFIG_FILE = "nx.json";
87

98
const NPM_DEFEAULT_REGISTRY = "https://registry.npmjs.org/"
@@ -139,9 +138,20 @@ function versionToNumber(version) {
139138
* @returns {string | undefined}
140139
*/
141140
function getTargetBranch() {
142-
// https://learn.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=azure-devops&tabs=yaml#build-variables-devops-services
143-
const adoTargetBranchName = process.env["SYSTEM_PULLREQUEST_TARGETBRANCH"];
144-
return adoTargetBranchName?.replace(/^refs\/heads\//, "");
141+
// Azure Pipelines
142+
if (process.env["TF_BUILD"] === "True") {
143+
// https://learn.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=azure-devops&tabs=yaml#build-variables-devops-services
144+
const targetBranch = process.env["SYSTEM_PULLREQUEST_TARGETBRANCH"];
145+
return targetBranch?.replace(/^refs\/heads\//, "");
146+
}
147+
148+
// GitHub Actions
149+
if (process.env["GITHUB_ACTIONS"] === "true") {
150+
// https://docs.github.com/en/actions/learn-github-actions/variables#default-environment-variables
151+
return process.env["GITHUB_BASE_REF"];
152+
}
153+
154+
return undefined;
145155
}
146156

147157
/**
@@ -151,15 +161,32 @@ function getTargetBranch() {
151161
* @returns {string}
152162
*/
153163
function getCurrentBranch(options) {
154-
const adoTargetBranchName = getTargetBranch();
155-
if (adoTargetBranchName) {
156-
return adoTargetBranchName;
164+
const targetBranch = getTargetBranch();
165+
if (targetBranch) {
166+
return targetBranch;
157167
}
158168

159-
// https://learn.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=azure-devops&tabs=yaml#build-variables-devops-services
160-
const adoSourceBranchName = process.env["BUILD_SOURCEBRANCHNAME"];
161-
if (adoSourceBranchName) {
162-
return adoSourceBranchName.replace(/^refs\/heads\//, "");
169+
// Azure DevOps Pipelines
170+
if (process.env["TF_BUILD"] === "True") {
171+
// https://learn.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=azure-devops&tabs=yaml#build-variables-devops-services
172+
const sourceBranch = process.env["BUILD_SOURCEBRANCHNAME"];
173+
if (sourceBranch) {
174+
return sourceBranch.replace(/^refs\/heads\//, "");
175+
}
176+
}
177+
178+
// GitHub Actions
179+
if (process.env["GITHUB_ACTIONS"] === "true") {
180+
// https://docs.github.com/en/actions/learn-github-actions/variables#default-environment-variables
181+
const headRef = process.env["GITHUB_HEAD_REF"];
182+
if (headRef) {
183+
return headRef; // For pull requests
184+
}
185+
186+
const ref = process.env["GITHUB_REF"];
187+
if (ref) {
188+
return ref.replace(/^refs\/heads\//, ""); // For push events
189+
}
163190
}
164191

165192
const { "mock-branch": mockBranch } = options;
@@ -242,23 +269,6 @@ function getTagForStableBranch(branch, { tag }, log) {
242269
return { npmTag: NPM_TAG_NEXT, prerelease: "rc" };
243270
}
244271

245-
/**
246-
* @param {string} file
247-
* @param {string} tag
248-
* @returns {void}
249-
*/
250-
function verifyPublishPipeline(file, tag) {
251-
const data = fs.readFileSync(file, { encoding: "utf-8" });
252-
const m = data.match(/publishTag: '(latest|next|nightly|v\d+\.\d+-stable)'/);
253-
if (!m) {
254-
throw new Error(`${file}: Could not find npm publish tag`);
255-
}
256-
257-
if (m[1] !== tag) {
258-
throw new Error(`${file}: 'publishTag' must be set to '${tag}'`);
259-
}
260-
}
261-
262272
/**
263273
* Verifies the configuration and enables publishing on CI.
264274
* @param {NxConfig} config
@@ -325,8 +335,6 @@ function enablePublishing(config, currentBranch, { npmTag: tag, prerelease, isNe
325335
verifyNpmAuth();
326336
}
327337

328-
verifyPublishPipeline(ADO_PUBLISH_PIPELINE, tag);
329-
330338
// Don't enable publishing in PRs
331339
if (!getTargetBranch()) {
332340
enablePublishingOnAzurePipelines();

.ado/templates/npm-publish-steps.yml

Lines changed: 0 additions & 53 deletions
This file was deleted.

.github/workflows/microsoft-pr.yml

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ name: PR
33
on:
44
pull_request:
55
types: [opened, synchronize, edited]
6-
branches: [ "main" ]
6+
branches: [ "main", "*-stable", "release/*" ]
77

88
concurrency:
99
# Ensure single build of a pull request. `main` should not be affected.
1010
group: ${{ github.workflow }}-${{ github.ref }}-${{ github.head_ref || github.run_id }}
1111
cancel-in-progress: true
1212

1313
jobs:
14-
lint-commit:
14+
lint-title:
1515
name: "Lint PR title"
1616
permissions: {}
1717
runs-on: ubuntu-latest
@@ -34,5 +34,38 @@ jobs:
3434
build-website:
3535
name: "Build the website"
3636
permissions: {}
37+
if: github.base_ref == 'main'
3738
uses: ./.github/workflows/microsoft-build-website.yml
39+
npm-publish-dry-run:
40+
name: "NPM Publish (Dry Run)"
41+
permissions: {}
42+
runs-on: ubuntu-latest
43+
steps:
44+
- uses: actions/checkout@v4
45+
with:
46+
filter: blob:none
47+
fetch-depth: 0
48+
- uses: actions/setup-node@v4
49+
with:
50+
node-version: '22'
51+
- name: Read publish tag from nx.json
52+
id: config
53+
run: |
54+
PUBLISH_TAG=$(jq -r '.release.version.generatorOptions.currentVersionResolverMetadata.tag' nx.json)
55+
echo "publishTag=$PUBLISH_TAG" >> $GITHUB_OUTPUT
56+
echo "Using publish tag from nx.json: $PUBLISH_TAG"
57+
- name: Configure git
58+
run: |
59+
git config --global user.email "[email protected]"
60+
git config --global user.name "React-Native Bot"
61+
git remote set-url origin https://rnbot:${{ secrets.GITHUB_TOKEN }}@github.com/microsoft/react-native-macos
62+
- name: Install dependencies
63+
run: yarn
64+
- name: Verify release config
65+
run: |
66+
node .ado/scripts/prepublish-check.mjs --verbose --skip-auth --tag ${{ steps.config.outputs.publishTag }}
3867
68+
- name: Version and publish packages (dry run)
69+
run: |
70+
echo "Target branch: ${{ github.base_ref }}"
71+
yarn nx release --dry-run --verbose

docs_archive/Releases.md

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,7 @@ git push -u origin HEAD
7272

7373
## Marking a release candidate stable
7474

75-
Prepare the publish pipeline:
76-
77-
```sh
78-
sed -i '' "s/publishTag: 'next'/publishTag: 'latest'/" .ado/templates/npm-publish-steps.yml
79-
```
80-
81-
Update the release configuration:
75+
Prepare the release configuration:
8276

8377
```sh
8478
node .ado/scripts/prepublish-check.mjs --tag latest --update

0 commit comments

Comments
 (0)