|
| 1 | +name: Build and release gem to RubyGems |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + tags: |
| 6 | + - v* |
| 7 | + |
| 8 | +jobs: |
| 9 | + release: |
| 10 | + runs-on: ubuntu-latest |
| 11 | + steps: |
| 12 | + - uses: actions/checkout@v2 |
| 13 | + with: |
| 14 | + fetch-depth: 0 # Fetch current tag as annotated. See https://github.com/actions/checkout/issues/290 |
| 15 | + - uses: ruby/setup-ruby@v1 |
| 16 | + with: |
| 17 | + ruby-version: 2.7 |
| 18 | + - name: "Extract data from tag: version, message, body" |
| 19 | + id: tag |
| 20 | + run: | |
| 21 | + git fetch --tags --force # Really fetch annotated tag. See https://github.com/actions/checkout/issues/290#issuecomment-680260080 |
| 22 | + echo ::set-output name=version::${GITHUB_REF#refs/tags/v} |
| 23 | + echo ::set-output name=subject::$(git for-each-ref $GITHUB_REF --format='%(contents:subject)') |
| 24 | + BODY="$(git for-each-ref $GITHUB_REF --format='%(contents:body)')" |
| 25 | + # Extract changelog entries between this and previous version headers |
| 26 | + escaped_version=$(echo ${GITHUB_REF#refs/tags/v} | sed -e 's/[]\/$*.^[]/\\&/g') |
| 27 | + changelog=$(awk "BEGIN{inrelease=0} /## ${escaped_version}/{inrelease=1;next} /## [0-9]+\.[0-9]+\.[0-9]+/{inrelease=0;exit} {if (inrelease) print}" CHANGELOG.md) |
| 28 | + # Multiline body for release. See https://github.community/t/set-output-truncates-multiline-strings/16852/5 |
| 29 | + BODY="${BODY}"$'\n'"${changelog}" |
| 30 | + BODY="${BODY//'%'/'%25'}" |
| 31 | + BODY="${BODY//$'\n'/'%0A'}" |
| 32 | + BODY="${BODY//$'\r'/'%0D'}" |
| 33 | + echo "::set-output name=body::$BODY" |
| 34 | + # Add pre-release option if tag name has any suffix after vMAJOR.MINOR.PATCH |
| 35 | + if [[ ${GITHUB_REF#refs/tags/} =~ ^v[0-9]+\.[0-9]+\.[0-9]+.+ ]]; then |
| 36 | + echo ::set-output name=prerelease::true |
| 37 | + fi |
| 38 | + - name: Build gem |
| 39 | + run: gem build |
| 40 | + - name: Calculate checksums |
| 41 | + run: sha256sum yabeda-sidekiq-${{ steps.tag.outputs.version }}.gem > SHA256SUM |
| 42 | + - name: Check version |
| 43 | + run: ls -l yabeda-sidekiq-${{ steps.tag.outputs.version }}.gem |
| 44 | + - name: Create Release |
| 45 | + id: create_release |
| 46 | + uses: actions/create-release@v1 |
| 47 | + env: |
| 48 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 49 | + with: |
| 50 | + tag_name: ${{ github.ref }} |
| 51 | + release_name: ${{ steps.tag.outputs.subject }} |
| 52 | + body: ${{ steps.tag.outputs.body }} |
| 53 | + draft: false |
| 54 | + prerelease: ${{ steps.tag.outputs.prerelease }} |
| 55 | + - name: Upload built gem as release asset |
| 56 | + uses: actions/upload-release-asset@v1 |
| 57 | + env: |
| 58 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 59 | + with: |
| 60 | + upload_url: ${{ steps.create_release.outputs.upload_url }} |
| 61 | + asset_path: yabeda-sidekiq-${{ steps.tag.outputs.version }}.gem |
| 62 | + asset_name: yabeda-sidekiq-${{ steps.tag.outputs.version }}.gem |
| 63 | + asset_content_type: application/x-tar |
| 64 | + - name: Upload checksums as release asset |
| 65 | + uses: actions/upload-release-asset@v1 |
| 66 | + env: |
| 67 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 68 | + with: |
| 69 | + upload_url: ${{ steps.create_release.outputs.upload_url }} |
| 70 | + asset_path: SHA256SUM |
| 71 | + asset_name: SHA256SUM |
| 72 | + asset_content_type: text/plain |
| 73 | + - name: Publish to GitHub packages |
| 74 | + env: |
| 75 | + GEM_HOST_API_KEY: Bearer ${{ secrets.GITHUB_TOKEN }} |
| 76 | + run: | |
| 77 | + gem push yabeda-sidekiq-${{ steps.tag.outputs.version }}.gem --host https://rubygems.pkg.github.com/${{ github.repository_owner }} |
| 78 | + - name: Publish to RubyGems |
| 79 | + env: |
| 80 | + GEM_HOST_API_KEY: "${{ secrets.RUBYGEMS_API_KEY }}" |
| 81 | + run: | |
| 82 | + gem push yabeda-sidekiq-${{ steps.tag.outputs.version }}.gem |
0 commit comments