How to publish Dart package to pub.dev from an Azure Pipeline

97 Views Asked by At

I want to publish a Dart package to pub.dev from a yaml-based Azure Pipeline. I looked at the following documentation about automated publishing of Dart packages, but it doesn't mention Azure Pipelines. https://dart.dev/tools/pub/automated-publishing

Also, I'm guessing my Azure DevOps project would need a service connection for pub.dev (just like we currently have a service connection for NuGet.org for our NuGet packages), but I'm not seeing anything related to pub.dev in the list of available service connections.

How can I setup an automated dart package deployment pipeline from Azure DevOps to pub.dev?

1

There are 1 best solutions below

2
Alvin Zhao - MSFT On

As of now, dart SDK is not installed on Microsoft-hosted agents and there is no pipeline task out-of-the-box for us to authenticate access to Google Cloud and publish package pub.dev.

Based on your requirement and the document on Publishing from anywhere using Exported Service Account Keys, I have tested the workflow below, which works for me.

  1. Create a service account(sa-devops in my case) in Google Cloud; then create and download the key json file; (Keep the keys SAFE as they are long-lived secrets; they might be easier to use in some environments, but also pose a larger risk if accidentally leaked) enter image description here
  2. Enable publishing with Google Cloud Service account in the Admin tab of your package and type-in the service account email; enter image description here
  3. Upload the key file (GCloudKey.json in my sample) into the Secure File library of Azure Pipelines; enter image description here
  4. Use below pipeline to install dart on ubuntu-latest Microsoft-hosted agent -> Download the secure key file -> Authenticate access to Google Cloud against the key file -> Publish new version of the test package to pub.dev;
    pool:
      vmImage: ubuntu-latest
    
    stages:
    - stage: PublishToPubDev
      jobs:
      - job: Dart_Auth_Publish
        steps:
        - script: |
            sudo apt update
            sudo apt install apt-transport-https
            sudo sh -c 'wget -qO- https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -'
            sudo sh -c 'wget -qO- https://storage.googleapis.com/download.dartlang.org/linux/debian/dart_stable.list > /etc/apt/sources.list.d/dart_stable.list'
            sudo apt update
            sudo apt install dart
          displayName: 'Install Dart'
        - script: |
            dart --version
            gcloud version
          displayName: 'Verify Dart and GCloud Installation'
        - task: DownloadSecureFile@1
          inputs:
            secureFile: 'GCloudKey.json'
          name: DownloadGCloudKey
        - script: |
            echo The downloaded GCloudkey file is $(DownloadGCloudKey.secureFilePath)
            gcloud auth activate-service-account --key-file=$(DownloadGCloudKey.secureFilePath)
            gcloud auth print-identity-token --audiences=https://pub.dev | dart pub token add https://pub.dev
            dart pub publish --force
          displayName: Publish to pub.dev
          workingDirectory: XXXXtestpackage # The directory in $(System.DefaultWorkingDirectory) where my package is located
    
    
    enter image description here