|
| 1 | +name: Release on Changelog Update |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - master |
| 7 | + paths: |
| 8 | + - 'CHANGELOG.md' |
| 9 | + |
| 10 | +env: |
| 11 | + MAKEOPTS: -j |
| 12 | + |
| 13 | +permissions: |
| 14 | + contents: write |
| 15 | + |
| 16 | +jobs: |
| 17 | + release: |
| 18 | + name: Build and Release |
| 19 | + runs-on: ubuntu-24.04 |
| 20 | + steps: |
| 21 | + - name: Checkout code |
| 22 | + uses: actions/checkout@v4 |
| 23 | + with: |
| 24 | + submodules: true |
| 25 | + fetch-depth: 0 |
| 26 | + |
| 27 | + - name: Extract latest version and release notes from CHANGELOG.md |
| 28 | + id: changelog |
| 29 | + run: | |
| 30 | + # Find the first versioned section (skip [Unreleased]) |
| 31 | + # Pattern: ## [x.y.z...] - date |
| 32 | + VERSION=$(grep -oP '(?<=^## \[)[^\]]+(?=\])' CHANGELOG.md | grep -v -i '^Unreleased$' | head -1) |
| 33 | +
|
| 34 | + if [ -z "$VERSION" ]; then |
| 35 | + echo "No versioned release found in CHANGELOG.md" |
| 36 | + exit 1 |
| 37 | + fi |
| 38 | +
|
| 39 | + echo "version=$VERSION" >> "$GITHUB_OUTPUT" |
| 40 | + echo "tag=v$VERSION" >> "$GITHUB_OUTPUT" |
| 41 | +
|
| 42 | + # Extract release notes between this version's heading and the next ## heading |
| 43 | + # Use awk to capture lines between the target section and the next section |
| 44 | + ESCAPED_VERSION=$(printf '%s' "$VERSION" | sed 's/[.[\*^$()+?{|]/\\&/g') |
| 45 | + NOTES=$(awk "/^## \\[$ESCAPED_VERSION\\]/{found=1; next} found && /^## \\[/{exit} found{print}" CHANGELOG.md) |
| 46 | +
|
| 47 | + # Write notes to a file for the release body |
| 48 | + echo "$NOTES" > /tmp/release_notes.md |
| 49 | +
|
| 50 | + echo "Detected version: $VERSION" |
| 51 | + echo "Release notes:" |
| 52 | + cat /tmp/release_notes.md |
| 53 | +
|
| 54 | + - name: Check if release already exists |
| 55 | + id: check_release |
| 56 | + env: |
| 57 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 58 | + run: | |
| 59 | + TAG="v${{ steps.changelog.outputs.version }}" |
| 60 | + if gh release view "$TAG" --repo "$GITHUB_REPOSITORY" > /dev/null 2>&1; then |
| 61 | + echo "Release $TAG already exists. Skipping." |
| 62 | + echo "exists=true" >> "$GITHUB_OUTPUT" |
| 63 | + else |
| 64 | + echo "exists=false" >> "$GITHUB_OUTPUT" |
| 65 | + fi |
| 66 | +
|
| 67 | + - name: Install cross-compiler |
| 68 | + if: steps.check_release.outputs.exists == 'false' |
| 69 | + run: sudo apt-get update && sudo apt-get install --yes gcc-arm-none-eabi |
| 70 | + |
| 71 | + - run: pipx install poetry |
| 72 | + if: steps.check_release.outputs.exists == 'false' |
| 73 | + |
| 74 | + - run: poetry install --only=build |
| 75 | + if: steps.check_release.outputs.exists == 'false' |
| 76 | + |
| 77 | + - name: Create credits file |
| 78 | + if: steps.check_release.outputs.exists == 'false' |
| 79 | + run: | |
| 80 | + echo "$PYBRICKS_EV3_CREDITS" > bricks/ev3/ci_credits.txt |
| 81 | + env: |
| 82 | + PYBRICKS_EV3_CREDITS: ${{ secrets.PYBRICKS_EV3_CREDITS }} |
| 83 | + |
| 84 | + - name: Build firmware |
| 85 | + if: steps.check_release.outputs.exists == 'false' |
| 86 | + run: | |
| 87 | + TAG="${{ steps.changelog.outputs.tag }}" |
| 88 | + export MICROPY_GIT_TAG=ci-release-${{ github.run_number }}-${TAG} |
| 89 | + export MICROPY_GIT_HASH=$(echo ${{ github.sha }} | cut -c1-8) |
| 90 | + poetry run make $MAKEOPTS -C micropython/mpy-cross |
| 91 | + poetry run make $MAKEOPTS -C bricks/movehub |
| 92 | + poetry run make $MAKEOPTS -C bricks/cityhub |
| 93 | + poetry run make $MAKEOPTS -C bricks/technichub |
| 94 | + poetry run make $MAKEOPTS -C bricks/primehub |
| 95 | + poetry run make $MAKEOPTS -C bricks/essentialhub |
| 96 | + poetry run make $MAKEOPTS -C bricks/nxt |
| 97 | + poetry run make $MAKEOPTS -C bricks/ev3 |
| 98 | +
|
| 99 | + - name: Create Release and Upload Assets |
| 100 | + if: steps.check_release.outputs.exists == 'false' |
| 101 | + env: |
| 102 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 103 | + run: | |
| 104 | + TAG="${{ steps.changelog.outputs.tag }}" |
| 105 | + VERSION="${{ steps.changelog.outputs.version }}" |
| 106 | +
|
| 107 | + # Determine pre-release flag (alpha, beta, release candidate) |
| 108 | + PRERELEASE_FLAG="" |
| 109 | + if [[ "$VERSION" == *a* ]] || [[ "$VERSION" == *b* ]] || [[ "$VERSION" == *rc* ]]; then |
| 110 | + PRERELEASE_FLAG="--prerelease" |
| 111 | + fi |
| 112 | +
|
| 113 | + # Rename and collect firmware assets |
| 114 | + HUBS="movehub cityhub technichub primehub essentialhub nxt ev3" |
| 115 | + ASSETS="" |
| 116 | + for HUB in $HUBS; do |
| 117 | + NEW_FILENAME="./bricks/$HUB/build/pybricks-$HUB-$TAG.zip" |
| 118 | + mv "./bricks/$HUB/build/firmware.zip" "$NEW_FILENAME" |
| 119 | + ASSETS="$ASSETS $NEW_FILENAME" |
| 120 | + done |
| 121 | +
|
| 122 | + # Create the git tag and GitHub release |
| 123 | + git tag "$TAG" |
| 124 | + git push origin "$TAG" |
| 125 | +
|
| 126 | + gh release create "$TAG" \ |
| 127 | + --repo="$GITHUB_REPOSITORY" \ |
| 128 | + --title="${VERSION}" \ |
| 129 | + -F /tmp/release_notes.md \ |
| 130 | + $PRERELEASE_FLAG \ |
| 131 | + $ASSETS |
0 commit comments