Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions .github/workflows/play-candidate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ on:

permissions:
contents: write # push the rc tag
id-token: write # keyless sign-in to Google Cloud (Workload Identity Federation)

# One Play upload at a time: two parallel runs would race for the same versionCode.
concurrency:
Expand All @@ -31,13 +32,17 @@ concurrency:

env:
PACKAGE: org.appdevforall.k2go
# Keyless Play credentials (no secret): the provider accepts only this repo in the google-play
# environment, and the robot has "Release apps to testing tracks" on this app only.
PLAY_WIF_PROVIDER: projects/540068336150/locations/global/workloadIdentityPools/github-actions/providers/knowledgetogo-repo
PLAY_SERVICE_ACCOUNT: k2go-play-publisher@knowledge-to-go.iam.gserviceaccount.com

jobs:
candidate:
name: Build, send to review and tag the candidate
runs-on: ubuntu-latest
timeout-minutes: 60
# The environment holds PLAY_SERVICE_ACCOUNT_JSON and asks a repo admin to approve each run.
# A repo admin approves each run. The Google Cloud provider trusts only this environment.
environment: google-play

steps:
Expand All @@ -61,10 +66,21 @@ jobs:
working-directory: tools/play-publish
run: python -m unittest -v test_play_publish

# A token lasts one hour, so each Play API step asks for its own (the build sits between them).
- name: Sign in to Google Play (keyless)
id: auth_plan
uses: google-github-actions/auth@v3
with:
workload_identity_provider: ${{ env.PLAY_WIF_PROVIDER }}
service_account: ${{ env.PLAY_SERVICE_ACCOUNT }}
token_format: access_token
access_token_scopes: https://www.googleapis.com/auth/androidpublisher
create_credentials_file: false

- name: Plan (version, rc tag, release notes, versionCode)
id: plan
env:
PLAY_SERVICE_ACCOUNT_JSON: ${{ secrets.PLAY_SERVICE_ACCOUNT_JSON }}
PLAY_ACCESS_TOKEN: ${{ steps.auth_plan.outputs.access_token }}
TRACK: ${{ inputs.track }}
run: |
set -euo pipefail
Expand Down Expand Up @@ -120,10 +136,20 @@ jobs:
-Pandroid.injected.signing.key.alias=$KEY_ALIAS \
-Pandroid.injected.signing.key.password=$KEY_PASSWORD

- name: Sign in to Google Play again (fresh token after the build)
id: auth_send
uses: google-github-actions/auth@v3
with:
workload_identity_provider: ${{ env.PLAY_WIF_PROVIDER }}
service_account: ${{ env.PLAY_SERVICE_ACCOUNT }}
token_format: access_token
access_token_scopes: https://www.googleapis.com/auth/androidpublisher
create_credentials_file: false

- name: Send to Google review
id: send
env:
PLAY_SERVICE_ACCOUNT_JSON: ${{ secrets.PLAY_SERVICE_ACCOUNT_JSON }}
PLAY_ACCESS_TOKEN: ${{ steps.auth_send.outputs.access_token }}
TRACK: ${{ inputs.track }}
CODE: ${{ steps.plan.outputs.code }}
LABEL: ${{ steps.plan.outputs.label }}
Expand Down
10 changes: 7 additions & 3 deletions controller/docs/ADR-K2GO-564-play-release-candidates.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,13 @@ says `vX.Y.Z-rc.N`, and only a build that says `vX.Y.Z` can become the release.
(versionName `vX.Y.Z`, sent to Play, then tagged) is not designed yet, so today button 2
refuses every rc commit. The release build runs only when `play-release.yml` calls it:
a hand-pushed `v*` tag does not start a release.
5. Play credentials: a Google Cloud service account with "Release apps to testing tracks" on
this app only. Its JSON key is the secret `PLAY_SERVICE_ACCOUNT_JSON` of the GitHub
environment `google-play`, which asks a repo admin to approve each run.
5. Play credentials, keyless: the service account
`k2go-play-publisher@knowledge-to-go.iam.gserviceaccount.com` (Google Cloud project
`knowledge-to-go`) has "Release apps to testing tracks" on this app only. It has no key: the
organization blocks key creation. The workflow signs in with Workload Identity Federation
(pool `github-actions`, provider `knowledgetogo-repo`). The provider accepts only repo
`appdevforall/KnowledgeToGo` in the GitHub environment `google-play`, which asks a repo admin
to approve each run. If the repo is renamed, update the provider condition and the grant.

## Consequences

Expand Down
41 changes: 7 additions & 34 deletions tools/play-publish/play_publish.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,27 @@
Design: controller/docs/ADR-K2GO-564-play-release-candidates.md.
API: https://developers.google.com/android-publisher/api-ref/rest

Standard library only. The JWT for the service account is signed with the openssl CLI,
so the runner needs no pip packages.
Standard library only. The Play API calls use a short-lived OAuth token from $PLAY_ACCESS_TOKEN.
The workflow gets it with keyless Workload Identity Federation (google-github-actions/auth), so
no service account key exists.

Commands (the service account JSON comes from $PLAY_SERVICE_ACCOUNT_JSON):
Commands (next-code and publish call the Play API):
plan --gradle F --notes F --tags F print version, rc tag and release notes as GitHub outputs
next-code --package P --base N print the Play versionCode for the next upload
final-tag --rc-tag T --tags F print the release tag for an approved candidate
check-release --gradle F --tag T fail unless versionName is exactly the final tag T
publish --package P --track T --aab F --code N --name S --notes-file F
"""
import argparse
import base64
import json
import os
import re
import subprocess
import sys
import tempfile
import time
import urllib.error
import urllib.parse
import urllib.request

API = "https://androidpublisher.googleapis.com/androidpublisher/v3/applications"
UPLOAD_API = "https://androidpublisher.googleapis.com/upload/androidpublisher/v3/applications"
SCOPE = "https://www.googleapis.com/auth/androidpublisher"
NOTES_MAX = 500 # Play limit for "What's new" per language.
SEMVER = re.compile(r"^v\d+\.\d+\.\d+$")
LABEL = re.compile(r"^(v\d+\.\d+\.\d+)(?:-rc\.(\d+))?$")
Expand Down Expand Up @@ -160,34 +155,12 @@ def track_release(track, name, code, notes):

# ---- Play API --------------------------------------------------------------------------

def _b64(data):
return base64.urlsafe_b64encode(data).rstrip(b"=")


def access_token(account):
"""OAuth2 service-account flow (RFC 7523): a signed JWT is exchanged for a token."""
now = int(time.time())
header = _b64(json.dumps({"alg": "RS256", "typ": "JWT"}).encode())
claims = _b64(json.dumps({"iss": account["client_email"], "scope": SCOPE,
"aud": account["token_uri"], "iat": now, "exp": now + 3600}).encode())
signing_input = header + b"." + claims
with tempfile.NamedTemporaryFile("w", suffix=".pem", delete=False) as key:
key.write(account["private_key"])
try:
signature = subprocess.run(["openssl", "dgst", "-sha256", "-sign", key.name],
input=signing_input, capture_output=True, check=True).stdout
finally:
os.unlink(key.name)
body = urllib.parse.urlencode({"grant_type": "urn:ietf:params:oauth:grant-type:jwt-bearer",
"assertion": (signing_input + b"." + _b64(signature)).decode()})
with urllib.request.urlopen(account["token_uri"], body.encode(), timeout=60) as r:
return json.load(r)["access_token"]


class Play:
def __init__(self, package):
self.package = package
self.token = access_token(json.loads(os.environ["PLAY_SERVICE_ACCOUNT_JSON"]))
self.token = os.environ.get("PLAY_ACCESS_TOKEN", "")
if not self.token:
sys.exit("PLAY_ACCESS_TOKEN is empty: run google-github-actions/auth before this step.")

def call(self, method, url, body=None, data=None, content_type="application/json"):
if body is not None:
Expand Down
Loading