As I embarked on learning more about Web3 I was introduced to Ganache the “one click blockchain”. It is great for local development. I wanted to figure out a way to use it as part of the PR workflow with Azure Static Web Apps (SWA). Azure Static Web Apps can spin up a temporary environment for reviewing changes during a PR. If the application is a Distributed Application (dApp) to review the changes you must have the smart contracts deployed. That is where I want to use Azure Container Instances (ACI) to host a temporary instance of Ganache to deploy the smart contracts during the PR.
First I needed to figure out how to deploy an ACI to host the Ganache docker image from trufflesuite/ganache. I will be using the cross-platform Azure CLI and cross-platform PowerShell. To create an ACI you need a resource group. You can create a new one or store it in the same resource group as the SWA. If you want to create a resource group for the ACI you can use the following command:
az group create --resource-group test-ganache --location centralus
With the resource group created we can now deploy the ACI using the following command:
$aci = az container create --resource-group test-ganache --name ganache --image trufflesuite/ganache:latest --ports 8545 --ip-address Public --output json | ConvertFrom-Json
The command above is using PowerShell to convert the JSON output of the az container create command into a PowerShell object and storing it in $aci. We need this so that we can gain access to the IP address of the ACI. The create command exposes port 8545 which is the default port used by Ganache and requests that the container gets a public IP.
If you are running these commands in an interactive PowerShell session with geth installed you could connect to this block chain using the following command:
geth attach http://$($aci.ipAddress.ip):8545
If you need access to an account address or private key you can review the logs of the container. To get the logs of the container that output the address and keys when it started use the following command:
$logs = az container logs --container ganache --resource-group test-ganache --name ganache
With the output stored in $logs we can use the Select-String CmdLet to grab the address and private key using the commands below:
# Get first address
($logs | Select-String '\(0\) ([^\(]+) \(').Matches.Groups[1].Value
# Get first private key
($logs | Select-String '\(0\) ([^\(]{64,})').Matches.Groups[1].Value
The commands above get the first address. If you want a different account just adjust the index in the regular expression from 0 to the desired index. For example, to get the second account change \(0\) to \(1\) in the commands above.
To clean up your ACI use the following command:
az container delete --resource-group test-ganache --name ganache --yes
Or if you want to delete the entire resource group you can use the following command:
az group delete --resource-group test-ganache --yes