Skip to content

Version Sync

Version Sync #9

Workflow file for this run

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 tests/Up.test.ts || true
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=$HEAD_BRANCH" \
-F parentCommitSha="${HEAD_SHA:-$GITHUB_SHA}" \
-F commit_title="$COMMIT_MESSAGE" \
-F configChange[path]="config.json" \
-F configChange[contents]="$(base64 -i config.json -o-)" \
-F nockChange[path]="tests/nocks.db" \
-F nockChange[contents]="$(base64 -i tests/nocks.db -o-)" \
-f query='mutation ($repo: String! $branch: String!, $parentCommitSha: GitObjectID!, $configChange: FileAddition!, $nockChange: FileAddition!, $commit_title: String!, $commit_body: String) {
createCommitOnBranch(input: {
branch: {
repositoryNameWithOwner: $repo,
branchName: $branch
},
message: {
headline: $commit_title,
body: $commit_body
},
expectedHeadOid: $parentCommitSha,
fileChanges: {
additions: [$configChange, $nockChange]
}
}) {
commit {
url
}
}
}'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
COMMIT_MESSAGE: "feat: update package manager versions"
- name: Create PR if it does not exist
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" -f "base=$BASE_BRANCH"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
BASE_BRANCH: ${{ github.event.repository.default_branch }}
BODY: This is an automated update of package manager versions
TITLE: "feat: update package manager versions"