Skip to content

Commit

Permalink
ci: implement release workflow for wasm application
Browse files Browse the repository at this point in the history
  • Loading branch information
fbozic committed Oct 16, 2024
1 parent c96531a commit ebbe3d3
Show file tree
Hide file tree
Showing 6 changed files with 169 additions and 45 deletions.
44 changes: 44 additions & 0 deletions .github/workflows/release-wasm.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: Build and release
on:
push:
branches:
- master
- ci/app-wasm-release
jobs:
metadata:
name: Build and release
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Build application
run: |
./logic/build.sh
- name: Get application metadata
id: get_app_metadata
run: |
echo "name=$(cargo read-manifest --manifest-path logic/Cargo.toml | jq -r '.name')" >> $GITHUB_OUTPUT
echo "description=$(cargo read-manifest --manifest-path logic/Cargo.toml | jq -r '.description')" >> $GITHUB_OUTPUT
echo "version=$(cargo read-manifest --manifest-path logic/Cargo.toml | jq -r '.version')" >> $GITHUB_OUTPUT
- name: Install near CLI
run: |
curl --proto '=https' --tlsv1.2 -LsSf https://github.com/near/near-cli-rs/releases/download/v0.15.1/near-cli-rs-installer.sh | sh
- name: Release application
env:
NEAR_NETWORK: testnet
NEAR_CREDENTIALS_JSON: ${{ secrets.NEAR_TESTNET_CREDENTIALS_JSON }}
NEAR_CONTRACT_ACCOUNT_ID: "calimero-package-manager.testnet"
run: |
touch "./near-credentials.json"
printf "$NEAR_CREDENTIALS_JSON" >> "./near-credentials.json"
cat "./near-credentials.json"
./logic/release.sh \
"$(realpath $NEAR_NETWORK)" \
./near-credentials.json \
"$NEAR_CONTRACT_ACCOUNT_ID"
14 changes: 7 additions & 7 deletions logic/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions logic/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "logic"
description = "Calimero WASM application example"
name = "increment"
description = "Calimero increment/decrement application"
version = "0.1.0"
edition = "2021"

Expand Down
11 changes: 7 additions & 4 deletions logic/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ cargo build --target wasm32-unknown-unknown --profile app-release

mkdir -p res

cp $TARGET/wasm32-unknown-unknown/app-release/logic.wasm ./res/
name=$(cargo read-manifest | jq -r '.name')
sanitized_name=$(echo $name | tr '-' '_')

if command -v wasm-opt > /dev/null; then
wasm-opt -Oz ./res/logic.wasm -o ./res/logic.wasm
fi
cp "$TARGET/wasm32-unknown-unknown/app-release/$sanitized_name.wasm" ./res/

if command -v wasm-opt >/dev/null; then
wasm-opt -Oz ./res/$sanitized_name.wasm -o ./res/$sanitized_name.wasm
fi
136 changes: 109 additions & 27 deletions logic/release.sh
Original file line number Diff line number Diff line change
@@ -1,46 +1,128 @@
#!/bin/bash
set -euo pipefail

cd "$(dirname $0)"

BLOBBY_IPFS="https://blobby-public.euw3.prod.gcp.calimero.network"

usage() {
echo "Usage: $0 <file_path> <name> <version> <description> <repository> <notes> <near_account_id>"
exit 1
echo "Usage: $0 <near_network> <near_credentials_file_path> <near_contract_account_id>"
}

# Validate arguments
if [[ $# -ne 7 ]]; then
if [[ $# -ne 3 ]]; then
usage
exit 1
fi

file_path="$1"
name="$2"
version="$3"
description="$4"
repository="$5"
notes="$6"
accountId="$7"
near_network="$1"
near_credentials_file_path="$2"
near_contract_account_id="$3"
near_account_id=$(cat $near_credentials_file_path | jq -r '.account_id')

if [[ ! -f "$file_path" || "$file_path" != *.wasm ]]; then
echo "Please provide a valid .wasm file."
usage
fi

# Calculate SHA-256 hash for the file
file_hash=$(sha256sum "$file_path" | awk '{ print $1 }')
echo "File Hash: $file_hash"
echo "-------------------Near network configuration-------------------"
echo "Near network: $near_network"
echo "Near credentials file path: $near_credentials_file_path"
echo "Near signer account ID: $near_account_id"
echo "Near contract account ID: $near_contract_account_id"
echo "-----------------------------------------------------------------"

# Upload application wasm file to Blobby IPFS
response=$(curl -s -X POST -H "Content-Type: application/wasm" -F "file=@${file_path}" "$BLOBBY_IPFS")
ipfs_path=$(echo "$response" | jq -r '.cid')
ipfs_path="$BLOBBY_IPFS/$ipfs_path"
name=$(cargo read-manifest | jq -r '.name')
version=$(cargo read-manifest | jq -r '.version')
description=$(cargo read-manifest | jq -r '.description')
repository=$(git remote get-url origin)
notes="-" # TODO: Add support for custom notes
sanitized_name=$(echo $name | tr '-' '_')
app_file_path="./res/$sanitized_name.wasm"

if [[ -z "$ipfs_path" ]]; then
echo "Error occurred while uploading the file."
if [[ ! -f "$app_file_path" ]]; then
echo "Failed to locate .wasm file, expected file path: $app_file_path"
exit 1
fi

echo "IPFS Path: $ipfs_path"
echo "-------------------Application metadata-------------------"
echo "Name: $name"
echo "Version: $version"
echo "Description: $description"
echo "Repository: $repository"
echo "Notes: $notes"
echo "Local file path: $app_file_path"
echo "------------------------------------------------------------"

# Update application metadata on NEAR blockchain
near call "$accountId" add_package "$(jq -n --arg name "$name" --arg description "$description" --arg repository "$repository" '{name: $name, description: $description, repository: $repository}')" --accountId "$accountId"
near call "$accountId" add_release "$(jq -n --arg name "$name" --arg version "$version" --arg notes "$notes" --arg path "$ipfs_path" --arg hash "$file_hash" '{name: $name, version: $version, notes: $notes, path: $path, hash: $hash}')" --accountId "$accountId"
package_metadata=$(
near contract \
call-function \
as-read-only \
$near_contract_account_id \
get_package_from_owner \
json-args "$(jq -n --arg name "$name" --arg owner_account "$near_account_id" --arg version "$version" '{name: $name, owner_account: $owner_account, version: $version}')" \
network-config $near_network \
now || echo ""
)
if [[ -z "$package_metadata" ]]; then
echo "Package not found, creating a new package."
near contract \
call-function \
as-transaction \
$near_contract_account_id \
add_package \
json-args "$(jq -n --arg name "$name" --arg description "$description" --arg repository "$repository" '{name: $name, description: $description, repository: $repository}')" \
prepaid-gas '100.0 Tgas' \
attached-deposit '0.0 NEAR' \
sign-as $near_account_id \
network-config $near_network \
sign-with-access-key-file $near_credentials_file_path \
send
else
echo "Package found, skipping package creation."
echo "Package metadata"
echo $package_metadata | jq -r .
echo "-----------------------------------------------------------------"
fi

release_metadata=$(
near contract \
call-function \
as-read-only \
$near_contract_account_id \
get_release_from_owner \
json-args "$(jq -n --arg name "$name" --arg owner_account "$near_account_id" --arg version "$version" '{name: $name, owner_account: $owner_account, version: $version}')" \
network-config $near_network \
now || echo ""
)
if [[ -z "$release_metadata" ]]; then
echo "Release not found, creating a new release."

file_hash=$(sha256sum "$app_file_path" | awk '{ print $1 }')
echo "File Hash: $file_hash"

response=$(curl -s -X POST -H "Content-Type: application/wasm" -F "file=@${app_file_path}" "$BLOBBY_IPFS")
ipfs_path=$(echo "$response" | jq -r '.cid')
ipfs_path="$BLOBBY_IPFS/$ipfs_path"

if [[ -z "$ipfs_path" ]]; then
echo "Error occurred while uploading the file."
exit 1
fi

echo "IPFS Path: $ipfs_path"

near contract \
call-function \
as-transaction \
$near_contract_account_id \
add_release \
json-args "$(jq -n --arg name "$name" --arg version "$version" --arg notes "$notes" --arg path "$ipfs_path" --arg hash "$file_hash" '{name: $name, version: $version, notes: $notes, path: $path, hash: $hash}')" \
prepaid-gas '100.0 Tgas' \
attached-deposit '0.0 NEAR' \
sign-as $near_account_id \
network-config $near_network \
sign-with-access-key-file $near_credentials_file_path \
send
else
echo "Release found, skipping release creation."
echo "Release metadata"
echo $release_metadata | jq -r .
echo "-----------------------------------------------------------------"
fi
5 changes: 0 additions & 5 deletions logic/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ impl AppState {
self.count
}

fn set_count(&mut self, count: u32) {
env::log(&format!("Set counter to {:?}", count));
self.count = count;
}

pub fn increase_count(&mut self, count: u32) -> u32 {
env::log(&format!("Increasing counter by {:?}", count));
self.count = self.count + count;
Expand Down

0 comments on commit ebbe3d3

Please sign in to comment.