diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml new file mode 100644 index 0000000..7f4ebd3 --- /dev/null +++ b/.github/workflows/deploy.yml @@ -0,0 +1,43 @@ +name: Publish ApplicationRecord to Registry + +on: + release: + types: [published] + push: + branches: + - master + - '*' + workflow_dispatch: + +env: + CERC_REGISTRY_USER_KEY: ${{ secrets.CICD_LACONIC_USER_KEY }} + CERC_REGISTRY_BOND_ID: ${{ secrets.CICD_LACONIC_BOND_ID }} + CERC_REGISTRY_RPC_ENDPOINT: "https://laconicd-mainnet-1.laconic.com" + CERC_REGISTRY_GQL_ENDPOINT: "https://laconicd-mainnet-1.laconic.com/api" + CERC_REGISTRY_CHAIN_ID: "laconic-mainnet" + CERC_REGISTRY_DEPLOYMENT_HOSTNAME: new-laconic-website + APP_NAME: "laconic-website" + DEPLOYER_LRN: "lrn://mito-systems/deployers/webapp-deployer-api.pwa.laconic.audubon.app" + AUTHORITY: "mito-systems" + CERC_APP_TYPE: "webapp" + +jobs: + cns_publish: + runs-on: ubuntu-latest + steps: + - name: "Clone project repository" + uses: actions/checkout@v3 + + - name: Use Node.js + uses: actions/setup-node@v3 + with: + node-version: 18 + + - name: "Install dependencies" + run: | + sudo apt -y update && sudo apt -y install jq + npm config set @cerc-io:registry https://git.vdb.to/api/packages/cerc-io/npm/ + npm install -g @cerc-io/laconic-registry-cli + + - name: "Publish App Record" + run: ./scripts/deploy.sh diff --git a/build-webapp.sh b/build-webapp.sh new file mode 100755 index 0000000..171f73e --- /dev/null +++ b/build-webapp.sh @@ -0,0 +1,33 @@ +#!/bin/bash + +# This script enables deployment of the Laconic website to Laconic Network Service Providers. +# These are independent operators of bare metal k8s, each running the Laconic +# automated deployment infrastructure. By publishing records onchain and sending +# payment, various services can be deployed in many jurisdictions. +# +# The linked deploy script will be triggered by new releases via Actions. +# +# https://git.vdb.to/LaconicNetwork/hosted-frontends/src/branch/main/deploy.sh + +OUTPUT_DIR="./dist" +DEST_DIR=${1:-/data} + +if [[ -d "$DEST_DIR" ]]; then + echo "${DEST_DIR} already exists." 1>&2 + exit 1 +fi + +# Install dependencies and build +echo "Installing dependencies..." +npm install || exit 1 + +echo "Building application..." +npm run build || exit 1 + +if [[ ! -d "$OUTPUT_DIR" ]]; then + echo "Missing output directory: $OUTPUT_DIR" 1>&2 + exit 1 +fi + +echo "Moving build output to ${DEST_DIR}..." +mv "$OUTPUT_DIR" "$DEST_DIR" diff --git a/scripts/deploy.sh b/scripts/deploy.sh new file mode 100644 index 0000000..7701806 --- /dev/null +++ b/scripts/deploy.sh @@ -0,0 +1,166 @@ +#!/bin/bash + +# Exit on any error +set -e + +# Check required environment variables +if [ -z "$CERC_REGISTRY_USER_KEY" ]; then + echo "Error: CERC_REGISTRY_USER_KEY environment variable is required" + exit 1 +fi + +if [ -z "$CERC_REGISTRY_BOND_ID" ]; then + echo "Error: CERC_REGISTRY_BOND_ID environment variable is required" + exit 1 +fi + +# Create temporary files +AR_RECORD_FILE=$(mktemp) +ADR_RECORD_FILE=$(mktemp) +CONFIG_FILE=$(mktemp) + +# Set defaults for env vars +CERC_APP_TYPE=${CERC_APP_TYPE:-"webapp"} +CERC_REPO_REF=${CERC_REPO_REF:-${GITHUB_SHA:-`git log -1 --format="%H"`}} +CERC_IS_LATEST_RELEASE=${CERC_IS_LATEST_RELEASE:-"true"} + +# Read package.json values +rcd_name=$(jq -r '.name' package.json | sed 's/null//') +rcd_desc=$(jq -r '.description' package.json | sed 's/null//') +rcd_repository=$(jq -r '.repository' package.json) +rcd_homepage=$(jq -r '.homepage' package.json | sed 's/null//') +rcd_license=$(jq -r '.license' package.json | sed 's/null//') +rcd_author=$(jq -r '.author' package.json | sed 's/null//') +rcd_app_version=$(jq -r '.version' package.json | sed 's/null//') + +# If repository is null or empty, get it from git +if [ -z "$rcd_repository" ] || [ "$rcd_repository" = "null" ]; then + rcd_repository=$(git remote get-url origin) +fi + +# Use APP_NAME from env if set, otherwise use package.json name +if [ -n "$APP_NAME" ]; then + rcd_name="$APP_NAME" +fi + +echo "Deploying application: $rcd_name" +echo "Version: $rcd_app_version" +echo "Commit: $CERC_REPO_REF" +echo "Repository: $rcd_repository" + +# Generate registry configuration +cat < "$CONFIG_FILE" +services: + registry: + rpcEndpoint: '${CERC_REGISTRY_RPC_ENDPOINT}' + gqlEndpoint: '${CERC_REGISTRY_GQL_ENDPOINT}' + chainId: ${CERC_REGISTRY_CHAIN_ID} + gas: 9550000 + fees: 15000000alnt +EOF + +# Auto-increment version if needed +next_ver=$(laconic -c $CONFIG_FILE registry record list --type ApplicationRecord --all --name "$rcd_name" 2>/dev/null | jq -r -s ".[] | sort_by(.createTime) | reverse | [ .[] | select(.bondId == \"$CERC_REGISTRY_BOND_ID\") ] | .[0].attributes.version" | awk -F. -v OFS=. '{$NF += 1 ; print}') + +if [ -z "$next_ver" ] || [ "1" == "$next_ver" ]; then + next_ver=0.0.1 +fi + +# Generate the application record +cat < "$AR_RECORD_FILE" +record: + type: ApplicationRecord + version: ${next_ver} + name: "$rcd_name" + description: "$rcd_desc" + homepage: "$rcd_homepage" + license: "$rcd_license" + author: "$rcd_author" + repository: + - "$rcd_repository" + repository_ref: "$CERC_REPO_REF" + app_version: "$rcd_app_version" + app_type: "$CERC_APP_TYPE" +EOF + +# Publish the application record +echo "Publishing application record..." +cat $AR_RECORD_FILE +AR_RECORD_ID=$(laconic -c $CONFIG_FILE registry record publish --filename $AR_RECORD_FILE --user-key "${CERC_REGISTRY_USER_KEY}" --bond-id ${CERC_REGISTRY_BOND_ID} | jq -r '.id') +echo "Application Record ID: $AR_RECORD_ID" + +# Set up LRN (Laconic Resource Name) +if [ -z "$CERC_REGISTRY_APP_LRN" ]; then + if [ -n "$AUTHORITY" ]; then + CERC_REGISTRY_APP_LRN="lrn://$AUTHORITY/applications/$rcd_name" + else + authority=$(echo "$rcd_name" | cut -d'/' -f1 | sed 's/@//') + app=$(echo "$rcd_name" | cut -d'/' -f2-) + CERC_REGISTRY_APP_LRN="lrn://$authority/applications/$app" + laconic -c $CONFIG_FILE registry authority reserve ${authority} --user-key "${CERC_REGISTRY_USER_KEY}" + laconic -c $CONFIG_FILE registry authority bond set ${authority} ${CERC_REGISTRY_BOND_ID} --user-key "${CERC_REGISTRY_USER_KEY}" + fi +fi + +# Set name mappings +echo "Setting name mappings..." +laconic -c $CONFIG_FILE registry name set --user-key "${CERC_REGISTRY_USER_KEY}" --bond-id ${CERC_REGISTRY_BOND_ID} "$CERC_REGISTRY_APP_LRN@${rcd_app_version}" "$AR_RECORD_ID" +laconic -c $CONFIG_FILE registry name set --user-key "${CERC_REGISTRY_USER_KEY}" --bond-id ${CERC_REGISTRY_BOND_ID} "$CERC_REGISTRY_APP_LRN@${CERC_REPO_REF}" "$AR_RECORD_ID" + +if [ "true" == "$CERC_IS_LATEST_RELEASE" ]; then + laconic -c $CONFIG_FILE registry name set --user-key "${CERC_REGISTRY_USER_KEY}" --bond-id ${CERC_REGISTRY_BOND_ID} "$CERC_REGISTRY_APP_LRN" "$AR_RECORD_ID" +fi + +# Verify the application record was published +APP_RECORD=$(laconic -c $CONFIG_FILE registry name resolve "$CERC_REGISTRY_APP_LRN" | jq '.[0]') +if [ -z "$APP_RECORD" ] || [ "null" == "$APP_RECORD" ]; then + echo "Error: No record found for $CERC_REGISTRY_APP_LRN" + exit 1 +fi + +# Handle deployer payment and deployment request if DEPLOYER_LRN is set +if [ -n "$DEPLOYER_LRN" ]; then + echo "Processing deployer payment..." + paymentAddress=$(laconic -c $CONFIG_FILE registry name resolve "$DEPLOYER_LRN" | jq -r '.[0].attributes.paymentAddress') + paymentAmount=$(laconic -c $CONFIG_FILE registry name resolve "$DEPLOYER_LRN" | jq -r '.[0].attributes.minimumPayment' | sed 's/alnt//g') + + txHash="" + if [[ -n "$paymentAmount" && "$paymentAmount" != "null" ]]; then + payment=$(laconic -c $CONFIG_FILE registry tokens send --address "$paymentAddress" --type alnt --quantity "$paymentAmount" --user-key "$CERC_REGISTRY_USER_KEY" --bond-id "$CERC_REGISTRY_BOND_ID") + txHash=$(echo "$payment" | jq -r '.tx.hash') + echo "Paid deployer with txHash: $txHash" + else + echo "No payment required - skipping payment step" + fi + + # Generate deployment request + echo "Generating deployment request..." + cat < "$ADR_RECORD_FILE" +record: + type: ApplicationDeploymentRequest + version: '1.0.0' + name: "$rcd_name@$rcd_app_version" + application: "$CERC_REGISTRY_APP_LRN@$rcd_app_version" + deployer: $DEPLOYER_LRN + dns: $CERC_REGISTRY_DEPLOYMENT_HOSTNAME + config: + env: + CERC_WEBAPP_DEBUG: "$rcd_app_version" + meta: + note: "Added by CI @ $(date)" + repository: "$rcd_repository" + repository_ref: "$CERC_REPO_REF" + payment: $txHash +EOF + + cat $ADR_RECORD_FILE + ADR_RECORD_ID=$(laconic -c $CONFIG_FILE registry record publish \ + --filename $ADR_RECORD_FILE \ + --user-key "${CERC_REGISTRY_USER_KEY}" \ + --bond-id ${CERC_REGISTRY_BOND_ID} | jq -r '.id') + echo "Deployment Record ID: $ADR_RECORD_ID" +fi + +# Cleanup +rm -f $AR_RECORD_FILE $ADR_RECORD_FILE $CONFIG_FILE +echo "Script completed successfully"