Have you ever experienced time-consuming difficulty manually publishing your Flutter packages? Do you wish there were a method to speed things up and eliminate human error? You’re in luck if so! You may automate your package publishing procedure and spare yourself time, hassle, and frustration by using the strength of GitHub Actions.
This Blog will guide you through the process of setting up a GitHub Workflow to automatically publish your Flutter package to pub.dev. This tutorial will give you the information and resources you need to advance your package publishing process, whether you’re an experienced developer or just learning Flutter.
Let’s Get Started✌️:
Prerequisites:
- Pub.dev Account.
- Github Repository for the Package.
- A Flutter Package to Publish.
A Little Back-Story:
A few weeks back I started working on my own Flutter Package neubrutalism_ui, which is a UI kit packed with amazing Neubrutalist styled widgets, and I was having difficulty deploying the package again and again with changes, I was trapped in an infinite loop where I was doing the same thing manually every time I had to update the package with changes.
I started looking for solutions to automate the process, after hours of trying different solutions from StackOverflow and this Blog but none of them were working, then I found https://github.com/k-paxian/dart-package-publisher this repo, which worked perfectly.
Step 1:
- Navigate to your package on pub.dev (manually deploy your package once - I found this way helpful)
- Click on the Admin Section
- Scroll down to the ‘Automated Publishing’ Section and enable the ‘Enable publishing from GitHub Actions’ button then add the repository and the version pattern.
Step 2:
Copy your pub-credentials.json:
-
For Windows
%APPDATA%/dart/pub-credentials.json
-
For Linux
$HOME/.config/dart/pub-credentials.json
-
For MacOs
~/Library/Application Support/dart/pub-credentials.json
-
NOTE: Note: if you don’t have or want to create a new pub-credentials.json. then run the following command in your project path and you will get the file in the specified path
dart pub publish --dry-run dart pub publish
Step 3:
Create a new Github Secret and store your pub-credential data there.
Step 4:
- Create a
.github/workflows
directory in your project directory. - Create a new file
publish.yaml
in the above-created directory.
Step 5:
Writing the workflow to automate the deployment🔥🔥🔥.
name: Publish Flutter Package
on:
push:
branches:
- master
jobs:
publish-package:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Set up Flutter
uses: subosito/flutter-action@v2
- name: Get dependencies
run: flutter pub get
- name: Analyze code
run: flutter analyze
- name: Format code
run: dart format --fix .
- name: Check publish warnings
run: dart pub publish --dry-run
- name: Publish package
uses: k-paxian/[email protected]
with:
credentialJson: ${{ secrets.CREDENTIAL_SECRET }}
flutter: true
skipTests: true
Explanation:
- The above action will be triggered when we push changes to the
master
branch. (I recommend creating 2 branches one for development and testing and the other for deployment, think of them asprod
anddev
environment) - Creates a job for a new build that will run on ubuntu-latest
- Set up configuration, install dependencies, analyze etc
- The
k-paxian/[email protected]
is the plugin that will take care of our package deployment in pub.dev. - Provide all the parameters like
credentialJson
which we created in GitHub Secret and so on.
[Note]: To trigger a new build, commit all your changes and head over to your root repository and there you will see create a new release build.