Blax
Blax

Reputation: 588

What is the easiest way to publish a pub.dev package in a CI?

I'm trying to automatically publish a package in Github Actions using dart pub publish but for it I need to login. The login workflow needs user-interation (logging in via Google) and thus can't be used on a CI.

As per https://github.com/dart-lang/pub/issues/2227 there doesn't seem to be an easy way, but someone mentionned :

Having to put the path like run: echo ${{secrets.PUB_CREDENTIALS}} > /opt/hostedtoolcache/flutter/1.9.1-hotfix.2-stable/x64/.pub-cache/credentials.json is not really great to maintain.

Is the format of this credentials.json known ?

Edit: On Windows, the credentials.json file is not in C:\Users\<USER>\AppData\Roaming\Pub\Cache\credentials.json as indicated in the blog post, it is now at C:\Users\<USER>\AppData\Roaming\dart\pub-credentials.json.

Upvotes: 1

Views: 1414

Answers (2)

Ced
Ced

Reputation: 17357

This is now supported

https://dart.dev/tools/pub/automated-publishing

I cannot post an explanation here as stackoverflow rules would require because this is too long and might change in the future.

Upvotes: 1

MαπμQμαπkγVπ.0
MαπμQμαπkγVπ.0

Reputation: 6729

This medium post has a clear guide that could help you on publishing Flutter or Dart packages to pub.dev via CI-CD servers.

Assuming you have the following requirements:

  1. A Flutter package ready to be published.
  2. An active account on pub.dev.
  3. A repository on GitHub with admin access.
  4. A working laptop, internet connection, and some patience!

These are the steps to follow:

  1. Retrieve Login Credentials for pub.dev
  2. Create Secrets on GitHub
  3. Write a shell script to set credentials
  4. Create a workflow for GitHub Action to publish a package.
  5. Triggering Builds.

Take note of this when running pub publish:

When we run this command in our terminal for the first time, pub would ask us to log in to our pub.dev account by opening the URL printed on the command line. Once we log in using that URL, pub store some login credentials (which happens to be some tokens) into a file called credentials.json.

Next time when you run pub publish command, it checks for this file and proceeds further without asking for login again. This gave me a hint that to make it work on CI-CD servers, we need to create this file somehow on the build machine.

You can visit the blog "Publish Your Flutter/Dart Package using GitHub Actions" for more details on each steps mentioned above.

Upvotes: 6

Related Questions