diff --git a/.github/release/publish-winget.sh b/.github/release/publish-winget.sh new file mode 100755 index 000000000000..bea0d5711107 --- /dev/null +++ b/.github/release/publish-winget.sh @@ -0,0 +1,46 @@ +#!/usr/bin/env bash +# publish-winget.sh : opens a microsoft/winget-pkgs pull request for each package render-winget.sh wrote, +# from the fork of the WINGET_GITHUB_TOKEN owner, the only way to contribute there. The workflow runs it for stable +# releases only. +. "$(dirname "$0")/lib.sh" + +if dry_run; then + echo "dry-run: skipped WinGet submission" + exit 0 +fi +test -n "${WINGET_GITHUB_TOKEN:-}" || die "Missing WINGET_GITHUB_TOKEN" +export GH_TOKEN=$WINGET_GITHUB_TOKEN + +upstream=microsoft/winget-pkgs +fork=$(gh api -X POST "repos/$upstream/forks" -F default_branch_only=true --jq .full_name) +for dir in "$1"/*/; do + id=$(basename "$dir") + folder="manifests/a/${id//.//}/$OPENCODE_VERSION" + branch="$id-$OPENCODE_VERSION" + title="New version: $id version $OPENCODE_VERSION" + if gh api "repos/$upstream/contents/$folder" --silent 2> /dev/null; then + echo "$id $OPENCODE_VERSION is already in $upstream" + continue + fi + if [ "$(gh api "repos/$upstream/pulls?state=all&head=${fork%%/*}:$branch" --jq length)" != 0 ]; then + echo "$id $OPENCODE_VERSION already has a pull request" + continue + fi + # Forks share objects with their upstream, so the branch can start from upstream master without syncing the fork. + head=$(gh api "repos/$upstream/git/ref/heads/master" --jq .object.sha) + tree=$( + for file in "$dir"*.yaml; do + jq -n --arg path "$folder/$(basename "$file")" --rawfile content "$file" \ + '{path: $path, mode: "100644", type: "blob", content: $content}' + done | jq -s --arg base "$(gh api "repos/$upstream/git/commits/$head" --jq .tree.sha)" '{base_tree: $base, tree: .}' | + gh api -X POST "repos/$fork/git/trees" --input - --jq .sha + ) + commit=$(jq -n --arg message "$title" --arg tree "$tree" --arg parent "$head" \ + '{message: $message, tree: $tree, parents: [$parent]}' | gh api -X POST "repos/$fork/git/commits" --input - --jq .sha) + # A retried release can find the branch from an attempt that failed before opening its pull request. + gh api -X POST "repos/$fork/git/refs" -f ref="refs/heads/$branch" -f sha="$commit" --silent 2> /dev/null || + gh api -X PATCH "repos/$fork/git/refs/heads/$branch" -f sha="$commit" -F force=true --silent + gh api -X POST "repos/$upstream/pulls" -f title="$title" -f head="${fork%%/*}:$branch" -f base=master \ + -f body="Published by the [OpenCode](https://github.com/anomalyco/opencode) release workflow in $GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID." \ + --jq '"Opened \(.html_url)"' +done diff --git a/.github/release/render-winget.sh b/.github/release/render-winget.sh index 92c0f18e2737..855300f4c9af 100755 --- a/.github/release/render-winget.sh +++ b/.github/release/render-winget.sh @@ -1,7 +1,6 @@ #!/usr/bin/env bash # render-winget.sh : writes the WinGet manifests for -# Anomaly.OpenCode and Anomaly.OpenCode.Desktop. Submission to microsoft/winget-pkgs is paused until the packages -# are accepted, so the workflow only uploads them. +# Anomaly.OpenCode and Anomaly.OpenCode.Desktop, which publish-winget.sh submits to microsoft/winget-pkgs. . "$(dirname "$0")/lib.sh" cli=$1 desktop=$2 out=$3 diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 924987ebb971..2578e78e880a 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -736,11 +736,16 @@ jobs: env: *aws-step run: bash .github/release/with-secrets.sh AUR_KEY=shared/AUR_SSH_KEY -- bash .github/release/publish-aur.sh "$RUNNER_TEMP/in/build-npm" - # WinGet manifests as data. Submission to microsoft/winget-pkgs stays paused until the packages are accepted. + # Renders the WinGet manifests and, for stable releases, opens their microsoft/winget-pkgs pull requests once the + # installers they point to are published. publish-winget: - needs: [plan, sign-windows, sign-desktop-windows-installer, verify] + needs: [plan, sign-windows, sign-desktop-windows-installer, publish-cli, publish-desktop, verify] if: ${{ !cancelled() && !failure() && needs.plan.outputs.release == 'true' }} runs-on: ubuntu-24.04 + environment: ${{ needs.plan.outputs.channel == 'latest' && needs.plan.outputs.dry_run != 'true' && 'release' || '' }} + permissions: + contents: read + id-token: write env: *env steps: - *checkout-release @@ -756,6 +761,17 @@ jobs: with: name: publish-winget path: ${{ runner.temp }}/out + - id: aws + name: Configure AWS credentials + if: needs.plan.outputs.channel == 'latest' && needs.plan.outputs.dry_run != 'true' + uses: aws-actions/configure-aws-credentials@e1253824e5c10ff9df46874f81ed3ec929e19cfd # v6.3.0 + with: *release-role + # WinGet moderates every submission and can reject one for reasons unrelated to the release. + - name: Submit + if: needs.plan.outputs.channel == 'latest' + continue-on-error: true + env: *aws-step + run: bash .github/release/with-secrets.sh WINGET_GITHUB_TOKEN=opencode/release/WINGET_GITHUB_TOKEN -- bash .github/release/publish-winget.sh "$RUNNER_TEMP/out" publish-github-release: needs: [plan, sign-macos, sign-windows, sign-desktop-windows-installer, publish-cli, publish-desktop, verify]