[Flutter] Automate publishing pub.dev

[Flutter] Automate publishing pub.dev

2023-08-31 hit count image

Let's see how to automate publishing the package developed for Flutter to pub.dev by using GitHub Actions.

Outline

When you develop the app with Flutter, you may need to create the package for common libraries or open source, and publish it to https://pub.dev/.

In previous blog post, I explained how to create the package for Flutter and publish it to pub.dev.

In this blog post, I will introduce how to automate publishing the package developed for Flutter to pub.dev by using GitHub Actions.

Activate Automated publishing

In order to automate publishing the package, that is already published to pub.dev, by using GitHub Actions, you need to activate the Automated publishing option in pub.dev.

Open the https://pub.dev/ page on the browser, and go to the package page that you want to automate publishing.

Automate publishing package - pub.dev package page

If you are the owner of the package, you can see the Admin tab and Activity log tab. Now, select the Admin tab to go to the admin page.

Automate publishing package - automated publishing option

When you scroll down on the admin page, you can see the Automated publishing option. Select the Enable publishing from GitHub Actions to activate the option.

Andh then, enter the owner/repository format of the GitHub repository that stores the code of the package in the Repository field.

I have a package with the following GitHub repository.

So, I entered dev-yakuza/bull in the Repository field.

And then, specify the Git Tag format to use for publishing. Usually, when you publish a package, you use the Git Tag format like v2.5.7. So, use the tag as it is unless there is a special reason.

Create GitHub Actions

Now, you are ready to publish the package to pub.dev automatically by using GitHub Actions. Next, you need to create GitHub Actions by referring to the Example workflow.

Automate publishing package - GitHub Actions example workflow

Also, you can refer to the following link to create GitHub Actions.

In my case, I created the .github/workflows/release.yml file and modify it as follows.

name: Release

on:
  push:
    tags:
      - 'v[0-9]+.[0-9]+.[0-9]+'

jobs:
  release:
    permissions:
      contents: write
      pull-requests: write
      id-token: write
    runs-on: ubuntu-latest
    steps:
      - name: Get semantic version
        id: semver
        run: echo "::set-output name=version::${GITHUB_REF#refs/tags/v}"

      - uses: actions/checkout@v3
        with:
          ref: v$

      # For Flutter package
      - uses: dart-lang/setup-dart@v1
      - uses: subosito/[email protected]
        with:
          flutter-version: '3.7.12'
      - name: Install dependencies
        run: flutter pub get

      - name: Publish
        run: flutter pub publish --force

      # For Dart package
      # - uses: dart-lang/setup-dart@v1
      # - name: Install dependencies
      #   run: dart pub get
      # - name: Publish
      #   run: dart pub publish --force

After creating the GitHub Actions like above, you can run it with the following command.

git tag v2.5.7
git push origin v2.5.7

Issue

As I explained on the previous blog post, in order to publish the package to pub.dev, the version in the CHANGELOG.md file and the pubspec.yaml file must match, and the version that is not published to pub.dev must be used.

You can modify the version in the CHANGELOG.md file and the pubspec.yaml file and commit it to GitHub, and then publish it to pub.dev with the Git Tag command. However, we can’t say this is as 100% automation.

Automate Changelog

In order to solve this issue, you need to autmate Release note in GitHub and updating the CHANGELOG.md file. You can refer to the following links to automate Release note and updating the CHANGELOG.md file by using GitHub Actions.

You can automate Release note and updating the CHANGELOG.md file by referring to the above links. In my case, I modify .github/workflows/release.yml file as follows to automate the CHANGELOG.md file.


name: Release

on:
  push:
    tags:
      - 'v[0-9]+.[0-9]+.[0-9]+'

jobs:
  release:
    permissions:
      contents: write
      pull-requests: write
      id-token: write
    runs-on: ubuntu-latest
    steps:
      - name: Get semantic version
        id: semver
        run: echo "::set-output name=version::${GITHUB_REF#refs/tags/v}"

      - uses: actions/checkout@v3
        with:
          ref: v${{ steps.semver.outputs.version }}

      - uses: release-drafter/release-drafter@v5
        id: target_release_notes
        with:
          tag: ${{ steps.semver.outputs.version }}
          name: ${{ steps.semver.outputs.version }}
          version: ${{ steps.semver.outputs.version }}
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Update Changelog
        uses: stefanzweifel/changelog-updater-action@v1
        with:
          latest-version: ${{ steps.target_release_notes.outputs.tag_name }}
          release-notes: ${{ steps.target_release_notes.outputs.body }}

      - name: Commit updated Changelog
        uses: stefanzweifel/git-auto-commit-action@v4
        with:
          commit_message: 'docs: Update changelog'
          file_pattern: CHANGELOG.md
          branch: main

      - uses: dart-lang/setup-dart@v1
      - uses: subosito/[email protected]
        with:
          flutter-version: '3.7.12'
      - name: Install dependencies
        run: flutter pub get

      - name: Update version
        run: dart run bull pub_version --version=${{ steps.semver.outputs.version }}

      - name: Commit updated pubspec
        uses: stefanzweifel/git-auto-commit-action@v4
        with:
          commit_message: 'chore: Update version for release'
          file_pattern: pubspec.yaml
          branch: main

      - name: Update Git tag
        run: |
          git tag ${{ github.ref_name }} -f
          git push origin ${{ github.ref_name }} -f

      - uses: release-drafter/release-drafter@v5
        with:
          tag: ${{ steps.semver.outputs.version }}
          name: ${{ steps.semver.outputs.version }}
          version: ${{ steps.semver.outputs.version }}
          publish: true
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Publish
        run: flutter pub publish --force

Update version of pubspec.yaml

To update the version in the pubspec.yaml file, I use the Bull package.

You can install the Bull package with the following command.

dart pub add --dev bull

And then, modify the .github/workflows/release.yml file as follows.


name: Release

on:
  push:
    tags:
      - 'v[0-9]+.[0-9]+.[0-9]+'

jobs:
  release:
    permissions:
      contents: write
      pull-requests: write
      id-token: write
    runs-on: ubuntu-latest
    steps:
      - name: Get semantic version
        id: semver
        run: echo "::set-output name=version::${GITHUB_REF#refs/tags/v}"

      - uses: actions/checkout@v3
        with:
          ref: v${{ steps.semver.outputs.version }}

      - uses: release-drafter/release-drafter@v5
        id: target_release_notes
        with:
          tag: ${{ steps.semver.outputs.version }}
          name: ${{ steps.semver.outputs.version }}
          version: ${{ steps.semver.outputs.version }}
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Update Changelog
        uses: stefanzweifel/changelog-updater-action@v1
        with:
          latest-version: ${{ steps.target_release_notes.outputs.tag_name }}
          release-notes: ${{ steps.target_release_notes.outputs.body }}

      - name: Commit updated Changelog
        uses: stefanzweifel/git-auto-commit-action@v4
        with:
          commit_message: 'docs: Update changelog'
          file_pattern: CHANGELOG.md
          branch: main

      - uses: dart-lang/setup-dart@v1
      - uses: subosito/[email protected]
        with:
          flutter-version: '3.7.12'
      - name: Install dependencies
        run: flutter pub get

      - name: Update version
        run: dart run bull pub_version --version=${{ steps.semver.outputs.version }}

      - name: Commit updated pubspec
        uses: stefanzweifel/git-auto-commit-action@v4
        with:
          commit_message: 'chore: Update version for release'
          file_pattern: pubspec.yaml
          branch: main

      - name: Update Git tag
        run: |
          git tag ${{ github.ref_name }} -f
          git push origin ${{ github.ref_name }} -f

      - uses: release-drafter/release-drafter@v5
        with:
          tag: ${{ steps.semver.outputs.version }}
          name: ${{ steps.semver.outputs.version }}
          version: ${{ steps.semver.outputs.version }}
          publish: true
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Publish
        run: flutter pub publish --force

The changes are the code to update the version of pubspec.yaml using the Bull package and the code to commit the modified the pubspec.yaml file as follows.

...
- name: Update version
  run: dart run bull pub_version --version=$

- name: Commit updated pubspec
  uses: stefanzweifel/git-auto-commit-action@v4
  with:
    commit_message: 'chore: Update version for release'
    file_pattern: pubspec.yaml
    branch: main
...

You can see how to use the Bull package in the following link.

Complated

Done! We’ve seen how to automate publishing the package developed for Flutter to pub.dev by using GitHub Actions. If you want to make and publish the opensource or common package for Flutter, please try to automate publishing to pub.dev by using the method introduced here.

Was my blog helpful? Please leave a comment at the bottom. it will be a great help to me!

App promotion

You can use the applications that are created by this blog writer Deku.
Deku created the applications with Flutter.

If you have interested, please try to download them for free.

Posts