|
| 1 | +name: Release and Publish to Google Artifact Registry |
| 2 | + |
| 3 | +permissions: |
| 4 | + id-token: write |
| 5 | + contents: read |
| 6 | + |
| 7 | +on: |
| 8 | + push: |
| 9 | + tags: |
| 10 | + - "v*.*.*" |
| 11 | + |
| 12 | +jobs: |
| 13 | + publish-gar: |
| 14 | + name: Build and Publish to GAR |
| 15 | + runs-on: ubuntu-latest |
| 16 | + steps: |
| 17 | + - name: Checkout |
| 18 | + uses: actions/checkout@v4 |
| 19 | + with: |
| 20 | + fetch-depth: 0 |
| 21 | + persist-credentials: false |
| 22 | + |
| 23 | + - name: Validate tag format |
| 24 | + env: |
| 25 | + TAG: ${{ github.ref_name }} |
| 26 | + run: | |
| 27 | + VERSION="${TAG#v}" |
| 28 | + if ! [[ $VERSION =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then |
| 29 | + echo "Error: Invalid version format '$VERSION'. Expected format: x.y.z" |
| 30 | + exit 1 |
| 31 | + fi |
| 32 | + echo "Releasing version: $VERSION (from tag: $TAG)" |
| 33 | +
|
| 34 | + - name: Verify GitHub Release exists |
| 35 | + env: |
| 36 | + GH_TOKEN: ${{ github.token }} |
| 37 | + TAG: ${{ github.ref_name }} |
| 38 | + run: | |
| 39 | + if ! gh release view "$TAG" --repo "$GITHUB_REPOSITORY" > /dev/null 2>&1; then |
| 40 | + echo "Error: No GitHub Release found for tag '$TAG'." |
| 41 | + echo "Create a release via GitHub UI instead of pushing tags manually." |
| 42 | + exit 1 |
| 43 | + fi |
| 44 | +
|
| 45 | + - name: Verify tag is on master |
| 46 | + env: |
| 47 | + DEFAULT_BRANCH: ${{ github.event.repository.default_branch }} |
| 48 | + run: | |
| 49 | + git fetch origin "$DEFAULT_BRANCH" --quiet |
| 50 | + if ! git merge-base --is-ancestor "$GITHUB_SHA" "origin/$DEFAULT_BRANCH"; then |
| 51 | + echo "Error: Tag commit $GITHUB_SHA is not reachable from origin/$DEFAULT_BRANCH." |
| 52 | + echo "Only tags on the $DEFAULT_BRANCH branch can be published." |
| 53 | + exit 1 |
| 54 | + fi |
| 55 | +
|
| 56 | + - name: Set up Python |
| 57 | + uses: actions/setup-python@v5 |
| 58 | + with: |
| 59 | + python-version: "3.x" |
| 60 | + |
| 61 | + - name: Install build dependencies |
| 62 | + run: pip install build twine setuptools-scm |
| 63 | + |
| 64 | + - name: Authenticate to Google Cloud |
| 65 | + id: auth |
| 66 | + uses: google-github-actions/auth@v3 |
| 67 | + with: |
| 68 | + token_format: "access_token" |
| 69 | + workload_identity_provider: "projects/668763687485/locations/global/workloadIdentityPools/github/providers/github" |
| 70 | + service_account: "github-experimentation@optimizely-iac.iam.gserviceaccount.com" |
| 71 | + |
| 72 | + - name: Build and Publish |
| 73 | + run: | |
| 74 | + python -m build |
| 75 | + python -m twine upload \ |
| 76 | + --repository-url https://us-east1-python.pkg.dev/artifact-registry-e3ca/private-python/ \ |
| 77 | + --username oauth2accesstoken \ |
| 78 | + --password ${{ steps.auth.outputs.access_token }} \ |
| 79 | + dist/* |
0 commit comments