Version Sync #2
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
name: Version Sync | |
on: | |
workflow_dispatch: | |
schedule: | |
# Run once a week at 00:05 UTC on Friday. | |
- cron: 5 0 * * 5 | |
permissions: | |
contents: read | |
jobs: | |
fetch-latest-versions: | |
runs-on: ubuntu-latest | |
permissions: | |
contents: write | |
pull-requests: write | |
env: | |
HEAD_BRANCH: actions/tools-update-config.json | |
steps: | |
- uses: actions/checkout@v4 | |
with: | |
persist-credentials: false | |
- name: Install Node | |
uses: actions/setup-node@v4 | |
with: | |
node-version: lts/* | |
- name: "Update the package manager versions" | |
run: | | |
LATEST_BERRY_VERSION=$(curl https://repo.yarnpkg.com/tags | jq -r '.latest.stable') | |
LATEST_NPM=$(curl https://registry.npmjs.org/npm | jq '.["dist-tags"].latest + "+sha1." + .versions[.["dist-tags"].latest].dist.shasum') | |
LATEST_PNPM=$(curl https://registry.npmjs.org/pnpm | jq '.["dist-tags"].latest + "+sha1." + .versions[.["dist-tags"].latest].dist.shasum') | |
LATEST_YARN=$(curl https://registry.npmjs.org/yarn | jq '.["dist-tags"].latest + "+sha1." + .versions[.["dist-tags"].latest].dist.shasum') | |
LATEST_BERRY=$(jq -n '$version + "+sha224." + $checksum' --arg version "$LATEST_BERRY_VERSION" --arg checksum "$(curl https://repo.yarnpkg.com/"$LATEST_BERRY_VERSION"/packages/yarnpkg-cli/bin/yarn.js | openssl dgst -sha224 | cut -d' ' -f2)") | |
git --no-pager show HEAD:config.json | jq '. * '"{ | |
definitions: { | |
npm: { | |
default: $LATEST_NPM, | |
}, | |
pnpm: { | |
default: $LATEST_PNPM, | |
}, | |
yarn: { | |
default: $LATEST_YARN, | |
transparent: { | |
default: $LATEST_BERRY, | |
}, | |
}, | |
}, | |
}" > config.json | |
- name: Check if there have been changes | |
id: check-for-changes | |
run: | | |
if git fetch origin "$HEAD_BRANCH"; then | |
git diff --exit-code --quiet FETCH_HEAD config.json || echo "CONTAINS_CHANGES=true" >> "$GITHUB_OUTPUT" | |
else | |
git diff --exit-code --quiet HEAD config.json || echo "CONTAINS_CHANGES=true" >> "$GITHUB_OUTPUT" | |
fi | |
- run: corepack yarn install --immutable | |
if: steps.check-for-changes.outputs.CONTAINS_CHANGES == 'true' | |
- run: corepack yarn build # We need the stubs to run the tests | |
if: steps.check-for-changes.outputs.CONTAINS_CHANGES == 'true' | |
- name: Remove old Nock files to avoid conflicts | |
if: steps.check-for-changes.outputs.CONTAINS_CHANGES == 'true' | |
run: rm tests/nocks.db | |
- run: corepack yarn test | |
if: steps.check-for-changes.outputs.CONTAINS_CHANGES == 'true' | |
env: | |
NOCK_ENV: record | |
- name: Push changes | |
if: steps.check-for-changes.outputs.CONTAINS_CHANGES == 'true' | |
run: | | |
HEAD_SHA= | |
if git fetch origin "$HEAD_BRANCH"; then | |
HEAD_SHA="$(git rev-parse FETCH_HEAD)" | |
else | |
# The branch does not exist yet, creating it. | |
gh api \ | |
--method POST \ | |
-H "Accept: application/vnd.github+json" \ | |
-H "X-GitHub-Api-Version: 2022-11-28" \ | |
"/repos/${GITHUB_REPOSITORY}/git/refs" \ | |
-f "ref=refs/heads/$HEAD_BRANCH" -f "sha=$GITHUB_SHA" | |
fi | |
gh api graphql \ | |
-F repo="$GITHUB_REPOSITORY" -F branch='actions/tools-update-config.json' \ | |
-F parentCommitSha="${HEAD_SHA:-$GITHUB_SHA}" \ | |
-F commit_title="$COMMIT_MESSAGE" \ | |
-F changes="$(node -p 'JSON.stringify({ | |
additions: ["config.json", "tests/nocks.db"].map(path => ({ path, contents: fs.readFileSync(path).toString('base64') })), | |
deletions: [], | |
})')" | |
-f query='mutation ($repo: String! $branch: String!, $parentCommitSha: GitObjectID!, $changes: FileChanges!, $commit_title: String!, $commit_body: String) { | |
createCommitOnBranch(input: { | |
branch: { | |
repositoryNameWithOwner: $repo, | |
branchName: $branch | |
}, | |
message: { | |
headline: $commit_title, | |
body: $commit_body | |
}, | |
expectedHeadOid: $parentCommitSha, | |
fileChanges: $changes | |
}) { | |
commit { | |
url | |
} | |
} | |
}' | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
COMMIT_MESSAGE: "feat: update package manager versions" | |
- name: Create PR if it does not exist | |
if: steps.check-for-changes.outputs.CONTAINS_CHANGES == 'true' | |
run: | | |
gh api \ | |
--method POST \ | |
-H "Accept: application/vnd.github+json" \ | |
-H "X-GitHub-Api-Version: 2022-11-28" \ | |
"/repos/${GITHUB_REPOSITORY}/pulls" \ | |
-f "title=$TITLE" -f "body=$BODY" -f "head=$HEAD_BRANCH" | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
BODY: This is an automated update of package manager versions | |
TITLE: "feat: update package manager versions" |