Dart Package

dart_package

Optimize your Dart & Flutter package development workflow with the dart_package GitHub Action.

This action helps you streamline your development process cycle by automating in-depth package analysis, running tests, tracking code-coverage with Codecov, as well as hassle-free publishing on pub.dev, saving you time and effort.

Table of contents

Setup

Setting up secrets in GitHub

In order to upload Coverage reports to Codecov or/and to publish a package on pub.dev we need to set up the corresponding tokens as secrets in our GitHub repository.

You can find here more information about secrets & environment variables.

Your secrets in your GitHub repository should look like that:

github-secrets
  • Setup Codecov

The unique repository upload token is found on the settings page of your project. You need write access to view this token.

For GitHub repositories the token will be found in https://codecov.io/github/<owner>/<repo>/settings.

codecov-token
  • Pub.dev

You can acquire your pub-credentials.json simply by running the command dart pub login and logging in to pub.dev with your Gmail account via a browser.

The pub-credentials.json will be generated in the default dart config folder in your system.

You can find it depending on your OS in the following directories:

  • On Linux (see XDG specification)

    • If $XDG_CONFIG_HOME is defined:
      • $XDG_CONFIG_HOME/dart/pub-credentials.json
    • else
      • $HOME/.config/dart/pub-credentials.json
  • On MacOS (see developer.apple.com)

    • ~/Library/Application Support/dart/pub-credentials.json
  • On Windows

    • %APPDATA%/dart/pub-credentials.json

Read more:

Please notice that the pub-credentials.json needs to be encoded into a base64 string before adding it to your repository secrets.

You can easily encode it using the following command:

base64 ./pub-credentials.json > output.txt

Inputs

NameDescriptionRequiredDefault
working_directorySpecify the working directory where the workflow will run.false“.”
sdkSpecify which SDK to use (dart, flutter).false“dart”
dart_sdkSpecify the Dart SDK version that will be used. This input will be ignored if sdk is set to flutter.false“3.4.4”
flutter_sdkSpecify the Flutter SDK version that will be used. This input will be ignored if sdk is set to dart.false“3.22.3”
analyze_directoriesSpecify the directories where dart analyze will run.false“lib test”
line_lengthThe line length to use with dart format.false“120”
concurrencyControls the number of test suites that runs concurrently, meaning that multiple tests in independent suites or platforms can run at the same time.false“4”
skip_testsFlag that defines whether to skip tests.false“false”
coverageFlag that defines whether to run tests with coverage.false“false”
codecovFlag that defines whether to upload coverage reports to Codecov.Requires the codecov_token.false“false”
codecov_tokenThe token that will be used to upload coverage reports to Codecov.Requires the codecov flag to be set to true.false“”
publishFlag that defines whether to publish the Dart package on pub.dev.false“false”
pubdev_tokenThe token that will be used to publish the Dart package to pub.dev.false“”
pana_thresholdSet a threshold in pana‘s analysis report.The exit code will indicate if (max – granted points) <= threshold.false“19”

Examples

You can try the dart_package GitHub Action using the workflows in the examples folder.

  • Pull-request workflow

# .github/workflows/pr.yml 
# This workflow runs on every pull-request to ensure Dart package quality.
name: PR Workflow

on:
  workflow_dispatch:
  pull_request:
    types: [opened, reopened, synchronize]

concurrency:
  group: ${{github.workflow}}-${{github.ref}}
  cancel-in-progress: true

jobs:
  build:
    defaults:
      run:
        working-directory: .
    runs-on: "ubuntu-latest"
    steps:
      - uses: actions/checkout@v4
      - uses: nikosportolos/dart_package@v0.2.2
  • Merge workflow

# .github/workflows/merge.yml 
# This workflow runs when merging code to the main branch.
name: Merge Workflow

on:
  workflow_dispatch:
  push:
    branches:
      - main

concurrency:
  group: ${{github.workflow}}-${{github.ref}}
  cancel-in-progress: true

jobs:
  build:
    defaults:
      run:
        working-directory: .
    runs-on: "ubuntu-latest"
    steps:
      - uses: actions/checkout@v4
      - uses: nikosportolos/dart_package@v0.2.2
        with:
          coverage: true
          codecov: true
          codecov_token: ${{ secrets.CODECOV_TOKEN }}
  • Publish workflow

# .github/workflows/publish.yml 
# This workflow runs when publishing a Dart package on pub.dev.
name: Publish Workflow

on:
  workflow_dispatch:
  release:
    types: [published]

concurrency:
  group: ${{github.workflow}}-${{github.ref}}
  cancel-in-progress: true

jobs:
  build:
    defaults:
      run:
        working-directory: .
    runs-on: "ubuntu-latest"
    steps:
      - uses: actions/checkout@v4
      - uses: nikosportolos/dart_package@v0.2.2
        with:
          coverage: true
          codecov: true
          codecov_token: ${{ secrets.CODECOV_TOKEN }}
          publish: true
          pubdev_token: ${{ secrets.PUBDEV_TOKEN }}
  • Flutter workflow

# .github/workflows/flutter.yml 
# This workflow runs on every pull-request to ensure Flutter package quality.
name: Flutter Workflow

on:
  workflow_dispatch:
  pull_request:
    types: [opened, reopened, synchronize]

concurrency:
  group: ${{github.workflow}}-${{github.ref}}
  cancel-in-progress: true

jobs:
  build:
    defaults:
      run:
        working-directory: .
    runs-on: "ubuntu-latest"
    steps:
      - uses: actions/checkout@v4
      - uses: nikosportolos/dart_package@v0.2.2
        sdK: flutter
        flutter-sdk: 3.22.3

Changelog

Check the changelog to learn what’s new in dart_package.

Contribution

Check the contribution guide if you want to help with dart_package.


You may also be interested in