diff --git a/.github/workflows/publish-to-registry.yml b/.github/workflows/publish-to-registry.yml new file mode 100644 index 0000000..8c598af --- /dev/null +++ b/.github/workflows/publish-to-registry.yml @@ -0,0 +1,34 @@ +name: Publish ApplicationRecord to Registry +on: + release: + types: [published] + push: + branches: + - publish-to-registry + +env: + CERC_REGISTRY_USER_KEY: ${{ secrets.CICD_LACONIC_USER_KEY }} + CERC_REGISTRY_BOND_ID: ${{ secrets.CICD_LACONIC_BOND_ID }} + +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 Yarn" + run: npm install -g yarn + - name: "Install registry CLI" + run: | + npm config set @cerc-io:registry https://git.vdb.to/api/packages/cerc-io/npm/ + yarn global add @cerc-io/laconic-registry-cli + - name: "Install jq" + run: apt -y update && apt -y install jq + - name: "Publish Application Record" + run: scripts/publish-app-record.sh + - name: "Request Deployment" + run: scripts/request-app-deployment.sh diff --git a/scripts/publish-app-record.sh b/scripts/publish-app-record.sh new file mode 100755 index 0000000..47b993c --- /dev/null +++ b/scripts/publish-app-record.sh @@ -0,0 +1,69 @@ +#!/bin/bash + +set -e + +RECORD_FILE=tmp.rf.$$ +CONFIG_FILE=`mktemp` + +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"} + +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 | sed 's/null//') +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//') + +cat < "$CONFIG_FILE" +services: + cns: + restEndpoint: '${CERC_REGISTRY_REST_ENDPOINT:-http://console.laconic.com:1317}' + gqlEndpoint: '${CERC_REGISTRY_GQL_ENDPOINT:-http://console.laconic.com:9473/api}' + chainId: ${CERC_REGISTRY_CHAIN_ID:-laconic_9000-1} + gas: 1750000 + fees: 20000aphoton +EOF + +next_ver=$(laconic -c $CONFIG_FILE cns 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 + +cat < "$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 + + +cat $RECORD_FILE +RECORD_ID=$(laconic -c $CONFIG_FILE cns record publish --filename $RECORD_FILE --user-key "${CERC_REGISTRY_USER_KEY}" --bond-id ${CERC_REGISTRY_BOND_ID} | jq -r '.id') +echo $RECORD_ID + +if [ -z "$CERC_REGISTRY_APP_CRN" ]; then + authority=$(echo "$rcd_name" | cut -d'/' -f1 | sed 's/@//') + app=$(echo "$rcd_name" | cut -d'/' -f2-) + CERC_REGISTRY_APP_CRN="crn://$authority/applications/$app" +fi + +laconic -c $CONFIG_FILE cns name set --user-key "${CERC_REGISTRY_USER_KEY}" --bond-id ${CERC_REGISTRY_BOND_ID} "$CERC_REGISTRY_APP_CRN@${rcd_app_version}" "$RECORD_ID" +laconic -c $CONFIG_FILE cns name set --user-key "${CERC_REGISTRY_USER_KEY}" --bond-id ${CERC_REGISTRY_BOND_ID} "$CERC_REGISTRY_APP_CRN@${CERC_REPO_REF}" "$RECORD_ID" +if [ "true" == "$CERC_IS_LATEST_RELEASE" ]; then + laconic -c $CONFIG_FILE cns name set --user-key "${CERC_REGISTRY_USER_KEY}" --bond-id ${CERC_REGISTRY_BOND_ID} "$CERC_REGISTRY_APP_CRN" "$RECORD_ID" +fi + +rm -f $RECORD_FILE $CONFIG_FILE diff --git a/scripts/request-app-deployment.sh b/scripts/request-app-deployment.sh new file mode 100755 index 0000000..f3feebd --- /dev/null +++ b/scripts/request-app-deployment.sh @@ -0,0 +1,56 @@ +#!/bin/bash + +set -e + +RECORD_FILE=tmp.rf.$$ +CONFIG_FILE=`mktemp` + +rcd_name=$(jq -r '.name' package.json | sed 's/null//' | sed 's/^@//') +rcd_app_version=$(jq -r '.version' package.json | sed 's/null//') + +cat < "$CONFIG_FILE" +services: + cns: + restEndpoint: '${CERC_REGISTRY_REST_ENDPOINT:-http://console.laconic.com:1317}' + gqlEndpoint: '${CERC_REGISTRY_GQL_ENDPOINT:-http://console.laconic.com:9473/api}' + chainId: ${CERC_REGISTRY_CHAIN_ID:-laconic_9000-1} + gas: 1550000 + fees: 20000aphoton +EOF + +if [ -z "$CERC_REGISTRY_APP_CRN" ]; then + authority=$(echo "$rcd_name" | cut -d'/' -f1 | sed 's/@//') + app=$(echo "$rcd_name" | cut -d'/' -f2-) + CERC_REGISTRY_APP_CRN="crn://$authority/applications/$app" +fi + +APP_RECORD=$(laconic -c $CONFIG_FILE cns name resolve "$CERC_REGISTRY_APP_CRN" | jq '.[0]') +if [ -z "$APP_RECORD" ] || [ "null" == "$APP_RECORD" ]; then + echo "No record found for $CERC_REGISTRY_APP_CRN." + exit 1 +fi + +cat < "$RECORD_FILE" +record: + type: ApplicationDeploymentRequest + version: 1.0.0 + name: "$rcd_name@$rcd_app_version" + application: "$CERC_REGISTRY_APP_CRN@$rcd_app_version" + dns: "$CERC_REGISTRY_DEPLOYMENT_SHORT_HOSTNAME" + deployment: "$CERC_REGISTRY_DEPLOYMENT_CRN" + config: + env: + CERC_WEBAPP_DEBUG: "$rcd_app_version" + CERC_MAX_GENERATE_TIME: 180 + CERC_NEXT_VERSION: 13.4.2 + meta: + note: "Added by CI @ `date`" + repository: "`git remote get-url origin`" + repository_ref: "${GITHUB_SHA:-`git log -1 --format="%H"`}" +EOF + +cat $RECORD_FILE +RECORD_ID=$(laconic -c $CONFIG_FILE cns record publish --filename $RECORD_FILE --user-key "${CERC_REGISTRY_USER_KEY}" --bond-id ${CERC_REGISTRY_BOND_ID} | jq -r '.id') +echo $RECORD_ID + +rm -f $RECORD_FILE $CONFIG_FILE