Skip to content

Commit

Permalink
Use content hash for facebook-www builds (#26331)
Browse files Browse the repository at this point in the history
Currently, any commit to React causes an internal sync since the Git
commit hash is part of the build. This creates a lot more sync commits
and noise than necessary, see:
https://github.com/facebook/react/commits/builds/facebook-www

This PR changes the version string to be a hash of the target build
files instead. This way we get a new version with any change that
actually impacts the generated files and still have a matching version
across the files.
  • Loading branch information
kassens authored Mar 6, 2023
1 parent ba353a5 commit 3cad3a5
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
12 changes: 10 additions & 2 deletions .github/workflows/commit_artifacts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,9 @@ jobs:
./compiled/babel-plugin-react-refresh/index.js
ls -R ./compiled
- name: Add REVISION files
- name: Add REVISION file
run: |
echo ${{ github.sha }} >> ./compiled/facebook-www/REVISION
cp ./compiled/facebook-www/REVISION ./compiled/facebook-www/REVISION_TRANSFORMS
- uses: actions/upload-artifact@v3
with:
name: compiled
Expand All @@ -146,7 +145,16 @@ jobs:
name: compiled
path: compiled/
- run: git status -u
- name: Check if only the REVISION file has changed
id: check_should_commit
run: |
if git status --porcelain | grep -qv '/REVISION$'; then
echo "should_commit=true" >> "$GITHUB_OUTPUT"
else
echo "should_commit=false" >> "$GITHUB_OUTPUT"
fi
- name: Commit changes to branch
if: steps.check_should_commit.outputs.should_commit == 'true'
uses: stefanzweifel/git-auto-commit-action@v4
with:
commit_message: |
Expand Down
30 changes: 22 additions & 8 deletions scripts/rollup/build-all-release-channels.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

/* eslint-disable no-for-of-loops/no-for-of-loops */

const crypto = require('node:crypto');
const fs = require('fs');
const fse = require('fs-extra');
const {spawnSync} = require('child_process');
Expand Down Expand Up @@ -40,10 +41,7 @@ if (dateString.startsWith("'")) {

// Build the artifacts using a placeholder React version. We'll then do a string
// replace to swap it with the correct version per release channel.
//
// The placeholder version is the same format that the "next" channel uses
const PLACEHOLDER_REACT_VERSION =
ReactVersion + '-' + nextChannelLabel + '-' + sha + '-' + dateString;
const PLACEHOLDER_REACT_VERSION = ReactVersion + '-PLACEHOLDER';

// TODO: We should inject the React version using a build-time parameter
// instead of overwriting the source files.
Expand Down Expand Up @@ -164,19 +162,27 @@ function processStable(buildDir) {
}

if (fs.existsSync(buildDir + '/facebook-www')) {
for (const fileName of fs.readdirSync(buildDir + '/facebook-www')) {
const hash = crypto.createHash('sha1');
for (const fileName of fs.readdirSync(buildDir + '/facebook-www').sort()) {
const filePath = buildDir + '/facebook-www/' + fileName;
const stats = fs.statSync(filePath);
if (!stats.isDirectory()) {
hash.update(fs.readFileSync(filePath));
fs.renameSync(filePath, filePath.replace('.js', '.classic.js'));
}
}
updatePlaceholderReactVersionInCompiledArtifacts(
buildDir + '/facebook-www',
ReactVersion + '-www-classic-' + sha + '-' + dateString
ReactVersion + '-www-classic-' + hash.digest('hex').substr(0, 8)
);
}

// Update remaining placeholders with next channel version
updatePlaceholderReactVersionInCompiledArtifacts(
buildDir,
ReactVersion + '-' + nextChannelLabel + '-' + sha + '-' + dateString
);

if (fs.existsSync(buildDir + '/sizes')) {
fs.renameSync(buildDir + '/sizes', buildDir + '/sizes-stable');
}
Expand Down Expand Up @@ -210,19 +216,27 @@ function processExperimental(buildDir, version) {
}

if (fs.existsSync(buildDir + '/facebook-www')) {
for (const fileName of fs.readdirSync(buildDir + '/facebook-www')) {
const hash = crypto.createHash('sha1');
for (const fileName of fs.readdirSync(buildDir + '/facebook-www').sort()) {
const filePath = buildDir + '/facebook-www/' + fileName;
const stats = fs.statSync(filePath);
if (!stats.isDirectory()) {
hash.update(fs.readFileSync(filePath));
fs.renameSync(filePath, filePath.replace('.js', '.modern.js'));
}
}
updatePlaceholderReactVersionInCompiledArtifacts(
buildDir + '/facebook-www',
ReactVersion + '-www-modern-' + sha + '-' + dateString
ReactVersion + '-www-modern-' + hash.digest('hex').substr(0, 8)
);
}

// Update remaining placeholders with next channel version
updatePlaceholderReactVersionInCompiledArtifacts(
buildDir,
ReactVersion + '-' + nextChannelLabel + '-' + sha + '-' + dateString
);

if (fs.existsSync(buildDir + '/sizes')) {
fs.renameSync(buildDir + '/sizes', buildDir + '/sizes-experimental');
}
Expand Down

0 comments on commit 3cad3a5

Please sign in to comment.