Skip to content

Commit

Permalink
ci(editor): Write custom github action for generating changelog from …
Browse files Browse the repository at this point in the history
…PR names and throw out the changeset package
  • Loading branch information
Mikey Stengel committed Oct 9, 2024
1 parent 46fb167 commit bc1fd0f
Show file tree
Hide file tree
Showing 53 changed files with 112 additions and 577 deletions.
8 changes: 0 additions & 8 deletions .changeset/README.md

This file was deleted.

12 changes: 0 additions & 12 deletions .changeset/config.json

This file was deleted.

5 changes: 0 additions & 5 deletions .changeset/gentle-cherries-flow.md

This file was deleted.

5 changes: 0 additions & 5 deletions .changeset/hip-moose-relate.md

This file was deleted.

5 changes: 0 additions & 5 deletions .changeset/yellow-beans-kiss.md

This file was deleted.

84 changes: 84 additions & 0 deletions .github/workflows/editor-changelog.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
name: Generate Editor Changelog

on:
push:
branches:
- '**' # Runs on all branches

jobs:
generate-changelog:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: 20

- name: Install dependencies
run: yarn

- name: Get current version of editor package
id: get_local_version
run: echo "CURRENT_VERSION=$(node -p \"require('./packages/editor/package.json').version\")" >> $GITHUB_ENV

- name: Get latest published version from npm
id: get_npm_version
run: |
NPM_VERSION=$(npm view @serlo/editor version)
echo "NPM_VERSION=$NPM_VERSION" >> $GITHUB_ENV
- name: Check if version has changed
id: check_version
run: |
if [ "$CURRENT_VERSION" != "$NPM_VERSION" ]; then
echo "version_changed=true" >> $GITHUB_ENV
else
echo "version_changed=false" >> $GITHUB_ENV
- name: Stop if version has not changed
if: env.version_changed == 'false'
run: exit 0

- name: Get last tag for editor package
id: get_last_tag
run: |
git fetch --all --tags
LAST_TAG=$(git describe --tags --match "v*" --abbrev=0 -- packages/editor)
echo "LAST_TAG=$LAST_TAG" >> $GITHUB_ENV
- name: Get merged PRs affecting editor package
id: get_prs
run: |
PRS=$(curl -s "https://api.github.com/repos/${{ github.repository }}/pulls?state=closed&per_page=100" | jq -r '.[] | select(.merged_at != null and .merged_at > $GITHUB_ENV.LAST_TAG and .head.repo.full_name == "${{ github.repository }}") | .number')
echo "PR_NUMBERS=$PRS" >> $GITHUB_ENV
- name: Get PR titles that touched editor folder
id: get_pr_titles
run: |
for pr in $PRS; do
FILES=$(curl -s "https://api.github.com/repos/${{ github.repository }}/pulls/$pr/files" | jq -r '.[].filename')
if echo "$FILES" | grep -q "^packages/editor/"; then
PR_TITLE=$(curl -s "https://api.github.com/repos/${{ github.repository }}/pulls/$pr" | jq -r '.title')
echo "$PR_TITLE" >> pr_titles.txt
fi
done
shell: bash

- name: Generate Changelog
run: |
echo "## Changelog for version $CURRENT_VERSION" > packages/editor/CHANGELOG.md
if [ -f pr_titles.txt ]; then
while IFS= read -r title; do
echo "- $title" >> packages/editor/CHANGELOG.md
done < pr_titles.txt
fi
- name: Commit and Push Changelog
run: |
git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com"
git config --local user.name "GitHub Action Bot"
git add packages/editor/CHANGELOG.md
git commit -m "chore: update changelog for version $CURRENT_VERSION"
git push origin ${{ github.ref_name }}
92 changes: 20 additions & 72 deletions .github/workflows/editor.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,98 +11,46 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- uses: actions/setup-node@v4
with:
node-version: 20
registry-url: https://registry.npmjs.org

- run: yarn

- name: Build editor package
run: yarn editor:build

- name: Apply Changesets and Version Bump
run: yarn changeset version
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Check if the next version is already published
- name: Check if current version is already published
id: check_published
run: |
NEXT_VERSION=$(node -p "require('./packages/editor/package.json').version")
if npm view @serlo/editor@$NEXT_VERSION > /dev/null 2>&1; then
CURRENT_VERSION=$(node -p "require('./packages/editor/package.json').version")
if npm view @serlo/editor@$CURRENT_VERSION > /dev/null 2>&1; then
echo "already_published=true" >> $GITHUB_OUTPUT
else
echo "already_published=false" >> $GITHUB_OUTPUT
fi
- name: Create a new branch for the version bump
- name: Publish to npm
if: steps.check_published.outputs.already_published == 'false'
run: |
NEXT_VERSION=$(node -p "require('./packages/editor/package.json').version")
git config --global user.name 'GitHub Actions'
git config --global user.email '[email protected]'
git checkout -b "release/@serlo/editor-$NEXT_VERSION"
- name: Check if changes occurred after version bump
id: check_changes
run: |
git add .
if git diff --exit-code --quiet; then
echo "no_changes=true" >> $GITHUB_OUTPUT
else
echo "no_changes=false" >> $GITHUB_OUTPUT
fi
- name: Commit changes to the new branch
if: steps.check_published.outputs.already_published == 'false' && steps.check_changes.outputs.no_changes == 'false'
run: |
NEXT_VERSION=$(node -p "require('./packages/editor/package.json').version")
git add .
git commit -m "chore(release): bump @serlo/editor to version $NEXT_VERSION"
git push origin "release/@serlo/editor-$NEXT_VERSION"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Create Pull Request
if: steps.check_published.outputs.already_published == 'false' && steps.check_changes.outputs.no_changes == 'false'
id: create_pr
run: |
NEXT_VERSION=$(node -p "require('./packages/editor/package.json').version")
PR_URL=$(gh pr create --title "Release: Bump @serlo/editor to version $NEXT_VERSION" --body "Automated PR to bump @serlo/editor to $NEXT_VERSION" --base staging --head "release/@serlo/editor-$NEXT_VERSION" --json url -q .url)
echo "pr_url=$PR_URL" >> $GITHUB_OUTPUT
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Wait for PR to be merged
if: steps.create_pr.outputs.pr_url
run: |
PR_URL="${{ steps.create_pr.outputs.pr_url }}"
while true; do
PR_STATE=$(gh pr view $PR_URL --json state -q .state)
if [ "$PR_STATE" = "MERGED" ]; then
echo "PR has been merged"
break
elif [ "$PR_STATE" = "CLOSED" ]; then
echo "PR was closed without merging"
exit 1
fi
echo "Waiting for PR to be merged..."
sleep 60
done
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Publish to npm (after PR merge)
if: steps.check_published.outputs.already_published == 'false' && steps.create_pr.outputs.pr_url
run: yarn editor:publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

- name: Create GitHub Release (after PR merge)
if: steps.check_published.outputs.already_published == 'false' && steps.create_pr.outputs.pr_url
- name: Create and push Git tag
if: steps.check_published.outputs.already_published == 'false'
run: |
NEXT_VERSION=$(node -p "require('./packages/editor/package.json').version")
body=$(git log -1 --pretty=format:%B)
gh release create "v$NEXT_VERSION" --notes "$body"
VERSION=$(node -p "require('./packages/editor/package.json').version")
git tag v$VERSION
git push origin v$VERSION
- name: Create GitHub Release
if: steps.check_published.outputs.already_published == 'false'
uses: actions/create-release@v1
with:
tag_name: v${{ steps.check_published.outputs.CURRENT_VERSION }}
release_name: v${{ steps.check_published.outputs.CURRENT_VERSION }}
draft: false
prerelease: false
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file removed .yarn/cache/mri-npm-1.2.0-8ecee0357d-83f515abbc.zip
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file removed .yarn/cache/pify-npm-4.0.1-062756097b-9c4e34278c.zip
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
"test": "yarn workspace @serlo/frontend test"
},
"devDependencies": {
"@changesets/cli": "^2.27.9",
"@serlo/eslint-config": "workspace:*",
"@serlo/typescript-config": "workspace:*",
"cross-env": "^7.0.3",
Expand Down
Loading

0 comments on commit bc1fd0f

Please sign in to comment.