Pub da GitHub Actions
I job di GitHub Actions leggono e pubblicano i suoi pacchetti privati senza alcun segreto memorizzato: ogni job scambia il proprio token di identità con un token Pub valido 15 minuti. Il funzionamento con ogni provider è spiegato in Pub dalla CI.
1. Considerare attendibile l’emittente
GitHub.com è già considerato attendibile. Per GitHub Enterprise Server, un proprietario aggiunge il suo emittente, https://<your host>/_services/token, come emittente proprio di questa organizzazione.
2. Associare il repository
Nella Control Room, apra Organization, poi CI access, Bind a repository, e scelga GitHub Actions. Il modulo chiede:
- Repository: owner/name, come nell’URL del repository: acme/app.
- Owner: l’utente o l’organizzazione proprietaria: acme.
- Workflow file, optional: il workflow autorizzato a pubblicare: .github/workflows/release.yml. Un workflow riutilizzabile di un altro repository si scrive owner/repo/.github/workflows/<file>.yml.
- Environment name, optional: un ambiente GitHub in cui viene eseguito il job, come release. Le sue regole di protezione condizionano allora ogni scambio.
- Ref pattern, optional: un ref di branch, di tag o di pull request: refs/tags/v* o refs/heads/main.
Poi conceda all’associazione i suoi pacchetti con il pulsante Packages: in lettura, o in lettura e pubblicazione.
3. Eseguire lo scambio nel job
Inserisca l’ID della sua organizzazione, dalla stessa schermata « Organization », in DARTASTIC_ORG_ID. Questo job pubblica; per uno che legge soltanto, tolga dart pub publish e conceda all’associazione la sola lettura.
# .github/workflows/publish.yml: publish on a version tag
on:
push:
tags: ['v*']
permissions:
contents: read
id-token: write # lets the job request its OIDC token
jobs:
publish:
runs-on: ubuntu-latest
environment: release # optional: bind the environment too, so its reviewers gate publishing
env:
DARTASTIC_ORG_ID: <your organization id>
steps:
- uses: actions/checkout@v4
- uses: dart-lang/setup-dart@v1
- name: Exchange this job's identity for a 15-minute Pub token
run: |
set -euo pipefail
OIDC=$(curl -sS --fail-with-body \
-H "Authorization: Bearer $ACTIONS_ID_TOKEN_REQUEST_TOKEN" \
"$ACTIONS_ID_TOKEN_REQUEST_URL&audience=https://dartastic.io" | jq -er .value)
RESP=$(curl -sS --fail-with-body -X POST https://api.dartastic.io/v1/ci/exchange \
-H "Authorization: Bearer $OIDC" -H "Content-Type: application/json" \
-d "{\"orgId\":\"$DARTASTIC_ORG_ID\"}") || { echo "$RESP" >&2; exit 1; }
export PUB_TOKEN=$(echo "$RESP" | jq -er .token)
dart pub token add https://pub.dartastic.io --env-var PUB_TOKEN
echo "::add-mask::$PUB_TOKEN"
echo "PUB_TOKEN=$PUB_TOKEN" >> "$GITHUB_ENV"
- run: dart pub get
- run: dart pub publish --force- Il workflow ha bisogno di permissions: id-token: write, altrimenti GitHub non dà al job alcun token da scambiare.
- Richieda il token con l’audience https://dartastic.io: lo fa la chiamata ACTIONS_ID_TOKEN_REQUEST_URL dello snippet.
- ::add-mask:: tiene il token Pub fuori dal log del job.
Quando viene rifiutato
Il log del job mostra per primo il motivo, ad esempio TRUSTED PUBLISHER REF MISMATCH. Ogni motivo e cosa fare sono spiegati in Pub dalla CI. La guida di GitHub Actions ai propri token si trova nella sua documentazione.