对于一个项目,我想设置 Jenkins,以便它可以自动将我的 Flutter 包推送到 pub.dev。到目前为止,我已经按照官方Dart文档上的步骤进行操作,并设置了一个允许发布我的包的Google Cloud服务帐户(在pub.dev上的包设置中设置),以及属于该帐户的密钥。
我不想通过调用
dart pub publish
并立即将其发布到 pub.dev 来测试设置,而是我首先想验证它是否能够连接到 pub.dev 及其相应的包。使用 dart pub login
可以吗?当我现在尝试时,它说我需要通过单击链接来验证我的帐户,这违背了它作为自动发布作业的目的。
是否可以通过 Google Cloud 服务帐户使用
dart pub login
,还是仅支持 dart pub publish
?我还可以尝试什么其他命令来验证连接是否有效?
我的 Jenkins 文件目前有这个阶段:
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
"""
}
}
}
}
您可以尝试以下代码:
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
"""
}
}
}
}