From 4599a7083915af16b6e94fc2363c93d379fcbe41 Mon Sep 17 00:00:00 2001
From: Jiahao XU <Jiahao_XU@outlook.com>
Date: Sun, 12 Nov 2023 00:17:16 +1000
Subject: [PATCH] Fix CI: Dry-run `cargo-publish` on crate to be published
 (#1490)

* Fix CI: Dry-run `cargo-publish` on crate to be published

If there's no crate to publish, do not dry-run `cargo-publish`.

Only dry-run `cargo-publish` on crate to be published to avoid
`cargo-publish` error on `cargo-binstall` when one of its direct
dependencies is bumped.

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>

* Fix passing of crate to `release-cli.yml`

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>

* Fix passing of `needs.pr-info.outpus.crate`

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>

* Fix `release-dry-run`: Only run if is release or is not PR event

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>

* Fix passing of crate

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>

* Rm `just-setup` from `release-cli.yml` since it cannot be cached

release is trigged by the closing of PR, so the cache is saved under the
branch `release/*` which cannot be reused.

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>

* Fix dry-run publish

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>

---------

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
---
 .github/workflows/ci.yml          | 19 +++++++++++++++++--
 .github/workflows/release-cli.yml |  8 +++-----
 2 files changed, 20 insertions(+), 7 deletions(-)

diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 05366c3c..48012559 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -123,15 +123,30 @@ jobs:
     - run: just avoid-dev-deps
     - run: just lint
 
+  pr-info:
+    outputs:
+      is-release: ${{ steps.meta.outputs.is-release }}
+      crate: ${{ steps.meta.outputs.crates-names }}
+
+    runs-on: ubuntu-latest
+    steps:
+    - id: meta
+      if: github.event_name == 'pull_request'
+      uses: cargo-bins/release-meta@v1
+      with:
+        event-data: ${{ toJSON(github.event) }}
+        extract-notes-under: '### Release notes'
+
   release-dry-run:
+    needs: pr-info
     uses: ./.github/workflows/release-cli.yml
-    if: github.event_name != 'pull_request'
+    if: github.event_name != 'pull_request' || needs.pr-info.outputs.is-release == 'true'
     secrets: inherit
     with:
       info: |
         {
           "is-release": false,
-          "crate": "cargo-binstall",
+          "crate": "${{ needs.pr-info.outputs.crate }}",
           "version": "0.0.0",
           "notes": ""
         }
diff --git a/.github/workflows/release-cli.yml b/.github/workflows/release-cli.yml
index 80b94fda..677fb6ce 100644
--- a/.github/workflows/release-cli.yml
+++ b/.github/workflows/release-cli.yml
@@ -83,17 +83,15 @@ jobs:
       with:
         name: minisign.pub
 
-    - uses: ./.github/actions/just-setup
-
     - run: .github/scripts/ephemeral-crate.sh
 
-    - if: fromJSON(inputs.info).is-release != 'true'
+    - if: fromJSON(inputs.info).is-release != 'true' && fromJSON(inputs.info).crate != ''
       name: DRY-RUN Publish to crates.io
       env:
         crate: ${{ fromJSON(inputs.info).crate }}
         CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
-      run: cargo package -p "$crate" --allow-dirty --no-default-features
-    - if: fromJSON(inputs.info).is-release != 'true'
+      run: cargo publish --dry-run -p "$crate" --allow-dirty --no-default-features
+    - if: fromJSON(inputs.info).is-release != 'true' && fromJSON(inputs.info).crate != ''
       name: Upload crate package as artifact
       uses: actions/upload-artifact@v3
       with: