Version Sync #21
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 | |
- 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 --wrap=0 config.json)" \ | |
-f query='mutation ($repo: String! $branch: String!, $parentCommitSha: GitObjectID!, $configChange: 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] | |
} | |
}) { | |
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" \ | |
> output.json || (<output.json jq 'if (.errors | any(.message | startswith("A pull request already exists"))) then . else halt_error end') | |
cat output.json | |
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" |