How to test publishing to pub.dev via Jenkins with Google Cloud service account?

39 Views Asked by At

For a project, I would like to setup Jenkins so that it can automatically push my Flutter package to pub.dev. So far I have followed the steps on the official Dart documentation and setup a Google Cloud service account which is allowed to publish my package (set in the package settings on pub.dev), and a key belonging to this account.

I do not want to test the setup by calling dart pub publish and having it publish to pub.dev immediately, instead I first want to verify that it is able to connect to pub.dev and its corresponding package. Is this possible by using dart pub login? When I attempt it now, it says I need to authenticate my account by clicking on a link, which defeats the purpose of it being an automatic publish job.

Is it even possible to use dart pub login with using a Google Cloud services account, or is only dart pub publish supported? What other command could I try to verify the connection works?

My Jenkins file currently has this stage:

stage('authenticate google service account') {
  steps {
    withCredentials([file(credentialsId: 'google-service-account', variable: 
       'GOOGLE_CLOUD_KEY_FILE')]) {
      script {
        sh """
          gcloud auth activate-service-account --key-file=${GOOGLE_CLOUD_KEY_FILE}
          gcloud auth print-identity-token \
                 --audiences=https://pub.dev \
          | dart pub token add https://pub.dev/packages/package_name
          dart pub login       
        """
      }
    }
  }
}
1

There are 1 best solutions below

5
lsalazar On

You can try this code:

stage('authenticate google service account') {
  steps {
    withCredentials([file(credentialsId: 'google-service-account', variable: 'GOOGLE_CLOUD_KEY_FILE')]) {
      script {
        sh """
          # activate service account & get token
          gcloud auth activate-service-account --key-file="${GOOGLE_CLOUD_KEY_FILE}"
          TOKEN=$(gcloud auth print-identity-token --audiences=https://pub.dev)

          # add token for pub.dev
          echo "Adding token for https://pub.dev/packages/package_name"
          echo "$TOKEN" | dart pub token add https://pub.dev/packages/package_name

          # verify token 
          echo "Verifying token..."
          dart pub whoami
        """
      }
    }
  }
}