From a70617124b4c0c730016a210e2b8b343c829d784 Mon Sep 17 00:00:00 2001
From: Liam Aharon <liam.aharon@hotmail.com>
Date: Sat, 28 Oct 2023 10:23:19 +1100
Subject: [PATCH] Automatically build and attach production and dev runtimes to
 GH releases (#2054)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Closes https://github.com/paritytech/release-engineering/issues/6

Adds a new Github Workflow which on a new release being created, builds
and attaches all runtimes managed in this repository in two flavours:
- `dev-debug-build`: Built with the `try-runtime` feature and has
logging enabled
- `on-chain-release`: Built with the regular old `on-chain-release`
feature

The new Github Workflow could be extended in the future by the
@paritytech/release-engineering team to fully automate the release
process if they choose to, similar to how it is fully automated in the
Fellowship repo
(https://github.com/polkadot-fellows/runtimes/blob/main/.github/workflows/release.yml).

The `on-chain-release` did not exist for parachains, so I added it.

---

Tested on my fork:
- https://github.com/liamaharon/polkadot-sdk/actions/runs/6663773523
- https://github.com/liamaharon/polkadot-sdk/releases/tag/test-6

---------

Co-authored-by: Chevdor <chevdor@users.noreply.github.com>
Co-authored-by: Dónal Murray <donal.murray@parity.io>
---
 .../build-and-attach-release-runtimes.yml     | 69 +++++++++++++++++++
 .../assets/asset-hub-rococo/Cargo.toml        |  5 ++
 .../assets/asset-hub-westend/Cargo.toml       |  5 ++
 .../bridge-hubs/bridge-hub-rococo/Cargo.toml  |  5 ++
 .../contracts/contracts-rococo/Cargo.toml     |  5 ++
 .../glutton/glutton-kusama/Cargo.toml         |  5 ++
 polkadot/runtime/rococo/Cargo.toml            |  2 +-
 polkadot/runtime/westend/Cargo.toml           |  2 +-
 8 files changed, 96 insertions(+), 2 deletions(-)
 create mode 100644 .github/workflows/build-and-attach-release-runtimes.yml

diff --git a/.github/workflows/build-and-attach-release-runtimes.yml b/.github/workflows/build-and-attach-release-runtimes.yml
new file mode 100644
index 00000000000..297f7a1665b
--- /dev/null
+++ b/.github/workflows/build-and-attach-release-runtimes.yml
@@ -0,0 +1,69 @@
+name: Build and Attach Runtimes to Releases/RC
+
+on:
+  release:
+    types:
+      - created
+
+env:
+  PROFILE: production
+
+jobs:
+  build_and_upload:
+    strategy:
+      matrix:
+        runtime:
+          - { name: westend, package: westend-runtime, path: polkadot/runtime/westend }
+          - { name: rococo, package: rococo-runtime, path: polkadot/runtime/rococo }
+          - { name: asset-hub-rococo, package: asset-hub-rococo-runtime, path: cumulus/parachains/runtimes/assets/asset-hub-rococo }
+          - { name: asset-hub-westend, package: asset-hub-westend-runtime, path: cumulus/parachains/runtimes/assets/asset-hub-westend }
+          - { name: bridge-hub-rococo, package: bridge-hub-rococo-runtime, path: cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo }
+          - { name: contracts-rococo, package: contracts-rococo-runtime, path: cumulus/parachains/runtimes/contracts/contracts-rococo }
+        build_config:
+          # Release build has logging disabled and no dev features
+          - { type: on-chain-release, opts: --features on-chain-release-build } 
+          # Debug build has logging enabled and developer features
+          - { type: dev-debug-build, opts: --features try-runtime }
+
+    runs-on: ubuntu-22.04
+
+    steps:
+    - name: Checkout code
+      uses: actions/checkout@v4
+
+    - name: Build ${{ matrix.runtime.name }} ${{ matrix.build_config.type }}
+      id: srtool_build
+      uses: chevdor/srtool-actions@v0.8.0
+      env:
+        BUILD_OPTS: ${{ matrix.build_config.opts }}
+      with:
+        chain: ${{ matrix.runtime.name }}
+        package: ${{ matrix.runtime.package }}
+        runtime_dir: ${{ matrix.runtime.path }}
+        profile: ${{ env.PROFILE }}
+
+    - name: Build Summary
+      run: |
+        echo "${{ steps.srtool_build.outputs.json }}" | jq . > ${{ matrix.runtime.name }}-srtool-digest.json
+        cat ${{ matrix.runtime.name }}-srtool-digest.json
+        echo "Runtime location: ${{ steps.srtool_build.outputs.wasm }}"
+
+    - name: Set up paths and runtime names
+      id: setup
+      run: |
+        RUNTIME_BLOB_NAME=$(echo ${{ matrix.runtime.package }} | sed 's/-/_/g').compact.compressed.wasm
+        PREFIX=${{ matrix.build_config.type == 'dev-debug-build' && 'DEV_DEBUG_BUILD__' || '' }}
+
+        echo "RUNTIME_BLOB_NAME=$RUNTIME_BLOB_NAME" >> $GITHUB_ENV
+        echo "ASSET_PATH=./${{ matrix.runtime.path }}/target/srtool/${{ env.PROFILE }}/wbuild/${{ matrix.runtime.package }}/$RUNTIME_BLOB_NAME" >> $GITHUB_ENV
+        echo "ASSET_NAME=$PREFIX$RUNTIME_BLOB_NAME" >> $GITHUB_ENV
+
+    - name: Upload Runtime to Release
+      uses: actions/upload-release-asset@v1
+      with:
+        upload_url: ${{ github.event.release.upload_url }}
+        asset_path: ${{ env.ASSET_PATH }}
+        asset_name: ${{ env.ASSET_NAME }}
+        asset_content_type: application/octet-stream
+      env:
+        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
diff --git a/cumulus/parachains/runtimes/assets/asset-hub-rococo/Cargo.toml b/cumulus/parachains/runtimes/assets/asset-hub-rococo/Cargo.toml
index ea5b5fa0554..ebf811e5463 100644
--- a/cumulus/parachains/runtimes/assets/asset-hub-rococo/Cargo.toml
+++ b/cumulus/parachains/runtimes/assets/asset-hub-rococo/Cargo.toml
@@ -240,3 +240,8 @@ std = [
 ]
 
 experimental = [ "pallet-aura/experimental" ]
+
+# A feature that should be enabled when the runtime should be built for on-chain
+# deployment. This will disable stuff that shouldn't be part of the on-chain wasm
+# to make it smaller like logging for example.
+on-chain-release-build = [ "sp-api/disable-logging" ]
diff --git a/cumulus/parachains/runtimes/assets/asset-hub-westend/Cargo.toml b/cumulus/parachains/runtimes/assets/asset-hub-westend/Cargo.toml
index 5e3807f2785..7c7a0731459 100644
--- a/cumulus/parachains/runtimes/assets/asset-hub-westend/Cargo.toml
+++ b/cumulus/parachains/runtimes/assets/asset-hub-westend/Cargo.toml
@@ -217,3 +217,8 @@ std = [
 ]
 
 experimental = [ "pallet-aura/experimental" ]
+
+# A feature that should be enabled when the runtime should be built for on-chain
+# deployment. This will disable stuff that shouldn't be part of the on-chain wasm
+# to make it smaller like logging for example.
+on-chain-release-build = [ "sp-api/disable-logging" ]
diff --git a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/Cargo.toml b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/Cargo.toml
index 5befb21c891..8c4e1612780 100644
--- a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/Cargo.toml
+++ b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/Cargo.toml
@@ -233,3 +233,8 @@ try-runtime = [
 ]
 
 experimental = [ "pallet-aura/experimental" ]
+
+# A feature that should be enabled when the runtime should be built for on-chain
+# deployment. This will disable stuff that shouldn't be part of the on-chain wasm
+# to make it smaller like logging for example.
+on-chain-release-build = [ "sp-api/disable-logging" ]
diff --git a/cumulus/parachains/runtimes/contracts/contracts-rococo/Cargo.toml b/cumulus/parachains/runtimes/contracts/contracts-rococo/Cargo.toml
index a020b66baae..0eb2428f358 100644
--- a/cumulus/parachains/runtimes/contracts/contracts-rococo/Cargo.toml
+++ b/cumulus/parachains/runtimes/contracts/contracts-rococo/Cargo.toml
@@ -192,3 +192,8 @@ try-runtime = [
 ]
 
 experimental = [ "pallet-aura/experimental" ]
+
+# A feature that should be enabled when the runtime should be built for on-chain
+# deployment. This will disable stuff that shouldn't be part of the on-chain wasm
+# to make it smaller like logging for example.
+on-chain-release-build = [ "sp-api/disable-logging" ]
diff --git a/cumulus/parachains/runtimes/glutton/glutton-kusama/Cargo.toml b/cumulus/parachains/runtimes/glutton/glutton-kusama/Cargo.toml
index e8abc61311c..f5362e4d6b2 100644
--- a/cumulus/parachains/runtimes/glutton/glutton-kusama/Cargo.toml
+++ b/cumulus/parachains/runtimes/glutton/glutton-kusama/Cargo.toml
@@ -125,3 +125,8 @@ try-runtime = [
 ]
 
 experimental = [ "pallet-aura/experimental" ]
+
+# A feature that should be enabled when the runtime should be built for on-chain
+# deployment. This will disable stuff that shouldn't be part of the on-chain wasm
+# to make it smaller like logging for example.
+on-chain-release-build = [ "sp-api/disable-logging" ]
diff --git a/polkadot/runtime/rococo/Cargo.toml b/polkadot/runtime/rococo/Cargo.toml
index 6d0dee3e434..243f4337cae 100644
--- a/polkadot/runtime/rococo/Cargo.toml
+++ b/polkadot/runtime/rococo/Cargo.toml
@@ -314,7 +314,7 @@ fast-runtime = []
 
 runtime-metrics = [ "runtime-parachains/runtime-metrics", "sp-io/with-tracing" ]
 
-# A feature that should be enabled when the runtime should be build for on-chain
+# A feature that should be enabled when the runtime should be built for on-chain
 # deployment. This will disable stuff that shouldn't be part of the on-chain wasm
 # to make it smaller like logging for example.
 on-chain-release-build = [ "sp-api/disable-logging" ]
diff --git a/polkadot/runtime/westend/Cargo.toml b/polkadot/runtime/westend/Cargo.toml
index a4f1bfb007e..cb1118cf92f 100644
--- a/polkadot/runtime/westend/Cargo.toml
+++ b/polkadot/runtime/westend/Cargo.toml
@@ -338,7 +338,7 @@ fast-runtime = []
 
 runtime-metrics = [ "runtime-parachains/runtime-metrics", "sp-io/with-tracing" ]
 
-# A feature that should be enabled when the runtime should be build for on-chain
+# A feature that should be enabled when the runtime should be built for on-chain
 # deployment. This will disable stuff that shouldn't be part of the on-chain wasm
 # to make it smaller like logging for example.
 on-chain-release-build = [ "sp-api/disable-logging" ]
-- 
GitLab