|
| 1 | +name: Set GHCR packages public |
| 2 | + |
| 3 | +# Experiment: GitHub's documented REST API has no "set visibility" operation |
| 4 | +# (org package routes are GET/DELETE only). This job tries the unofficial |
| 5 | +# PATCH that some projects use, with GITHUB_TOKEN + packages:write. |
| 6 | +on: |
| 7 | + # Push runs this on the PR branch (workflow_dispatch only exists after merge to main). |
| 8 | + push: |
| 9 | + paths: |
| 10 | + - ".github/workflows/set-ghcr-packages-public.yaml" |
| 11 | + workflow_dispatch: |
| 12 | + inputs: |
| 13 | + package_names: |
| 14 | + description: >- |
| 15 | + Newline-separated container package names under this org |
| 16 | + (include the repo prefix). |
| 17 | + required: false |
| 18 | + default: | |
| 19 | + rhdh-plugin-export-overlays/backstage-community-plugin-todo |
| 20 | + rhdh-plugin-export-overlays/backstage-community-plugin-todo-backend |
| 21 | +
|
| 22 | +permissions: |
| 23 | + contents: read |
| 24 | + packages: write |
| 25 | + |
| 26 | +jobs: |
| 27 | + set-public: |
| 28 | + runs-on: ubuntu-latest |
| 29 | + steps: |
| 30 | + - name: Try PATCH visibility on each package |
| 31 | + env: |
| 32 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 33 | + GH_HOST: github.com |
| 34 | + ORG: ${{ github.repository_owner }} |
| 35 | + PACKAGE_NAMES: ${{ inputs.package_names }} |
| 36 | + run: | |
| 37 | + set -u |
| 38 | + python3 - <<'PY' |
| 39 | + import json, os, subprocess, sys, urllib.parse |
| 40 | +
|
| 41 | + org = os.environ["ORG"] |
| 42 | + raw = os.environ.get("PACKAGE_NAMES", "") |
| 43 | + names = [] |
| 44 | + for line in raw.splitlines(): |
| 45 | + line = line.split("#", 1)[0].strip() |
| 46 | + if line: |
| 47 | + names.append(line) |
| 48 | +
|
| 49 | + if not names: |
| 50 | + names = [ |
| 51 | + "rhdh-plugin-export-overlays/backstage-community-plugin-todo", |
| 52 | + "rhdh-plugin-export-overlays/backstage-community-plugin-todo-backend", |
| 53 | + ] |
| 54 | +
|
| 55 | + def gh_api(method, path, *extra): |
| 56 | + cmd = ["gh", "api", "--method", method, "-i", path, *extra] |
| 57 | + print(f"\n===== {method} {path} {extra} =====", flush=True) |
| 58 | + proc = subprocess.run(cmd, capture_output=True, text=True) |
| 59 | + out = (proc.stdout or "") + (proc.stderr or "") |
| 60 | + print(out[:4000], flush=True) |
| 61 | + return proc.returncode, out |
| 62 | +
|
| 63 | + any_ok = False |
| 64 | + for name in names: |
| 65 | + encoded = urllib.parse.quote(name, safe="") |
| 66 | + base = f"orgs/{org}/packages/container/{encoded}" |
| 67 | + print(f"\n########## {name} ##########", flush=True) |
| 68 | + gh_api("GET", base) |
| 69 | + for method, path, extra in [ |
| 70 | + ("PATCH", base, ["-f", "visibility=public"]), |
| 71 | + ("PATCH", f"{base}/visibility", ["-f", "visibility=public"]), |
| 72 | + ("POST", f"{base}/visibility", ["-f", "visibility=public"]), |
| 73 | + ]: |
| 74 | + code, out = gh_api(method, path, *extra) |
| 75 | + if code == 0 and ("\"visibility\": \"public\"" in out or "204" in out[:80]): |
| 76 | + any_ok = True |
| 77 | + preview = [ |
| 78 | + "-H", |
| 79 | + "Accept: application/vnd.github.package-deletes-preview+json", |
| 80 | + "-f", |
| 81 | + "visibility=public", |
| 82 | + ] |
| 83 | + gh_api("PATCH", base, *preview) |
| 84 | +
|
| 85 | + print("\n########## GET after attempts ##########", flush=True) |
| 86 | + failed = False |
| 87 | + for name in names: |
| 88 | + encoded = urllib.parse.quote(name, safe="") |
| 89 | + path = f"orgs/{org}/packages/container/{encoded}" |
| 90 | + proc = subprocess.run( |
| 91 | + ["gh", "api", path, "--jq", ".visibility"], |
| 92 | + capture_output=True, |
| 93 | + text=True, |
| 94 | + ) |
| 95 | + vis = (proc.stdout or proc.stderr or "").strip() |
| 96 | + print(f"{name}: visibility={vis!r} exit={proc.returncode}", flush=True) |
| 97 | + if vis != "public": |
| 98 | + failed = True |
| 99 | +
|
| 100 | + if failed: |
| 101 | + print( |
| 102 | + "\nCONCLUSION: GITHUB_TOKEN could not set packages public.", |
| 103 | + file=sys.stderr, |
| 104 | + ) |
| 105 | + sys.exit(1) |
| 106 | + print("\nCONCLUSION: packages report visibility=public") |
| 107 | + PY |
0 commit comments