Building and Deploying Solana Programs Using Github Actions & Blockpulsar

In this tutorial, we are going to show how to create a Solana Program in Rust, with building and deployment using GitHub Actions and the Blockpulsar platform

Building and Deploying Solana Programs Using Github Actions & Blockpulsar

In Solana, smart contracts are called programs, and they are Rust or C/C++ applications. When it comes to automated building and deployment you need to set up the Solana Cluster and make sure you have enough SOL in your Solana Program Account.

In this tutorial, we are going to show how to create a Solana Program in Rust, with building and deployment using GitHub Actions and the Blockpulsar platform.

In practice, Github Actions with complete Rust Solana configuration is quite heavy with container images of more than 4GB 😒 Luckily, Blockpulsar.com abstracts this complexity by giving a single command to the start Solana Program building and deploying processes. The incredible part of all this is that Blockpulsar’s deployment process automatically checks the account balance and calculates the amount of Airdrop within theSOL to ensure that Solana CLI can successfully deploy the program.

Configuring Github Action Secrets

To get started with Github Actions, you need Github Action Secrets. Blockpulsar itself requires an API Key and Secret Key. This allows you to enable strict Solana key-pairs in Github Actions that help to keep it secure/safe and flexible to use.

In your Blockpulsar.com account (you will need to create an account if you don’t have one), you can create a new App on the https://app.blockpulsar.com/apps page and copy all the necessary keys.

https://app.lockpulsar.com/apps

An API Key and API Secret are required for constructing Github Actions, and an App ID for the blockpulsar.json configuration file in your repository root. Now you can create your Solana program on your machine or you can download a minimal Hello World example from here: https://github.com/blockpulsar/solana-hello-world.

If you want to build and deploy your own Solana Program without GitHub Actions see our previous post: “How to Deploy Solana Program with Blockpulsar”.

The goal of the current tutorial is to show you how you can integrate Blockpulsar Solana Program CI/CD with Github actions.

Once you have an API Key and API Secret from Blockpulsar, you will need to create Github Action Secrets in your project repository: Settings → Secrets → Actions. How you name your secret keys is totally up to you,but for this tutorial, we will name BLOCKPULSAR_KEY and BLOCKPULSAR_SECRET.

Github Actions Secrets

That’s it!

Now you need to make a Github Action Workflow configuration (.yml file) to run Blockpulsar CI/CD whenever you have Pull Request or Master push.

Github Action Workflow blockpulsar.yml

Github Actions are sourced from configuration files under the directory .github, which should be in your repository root. You should have a workflow subdirectory with actual Github Actions (.yml configuration files).

.github /
  workflows /

    blockpulsar.yml
...
...
cargo.toml
blockpulsar.json # this is where we have an App ID
...

Note: it doesn’t have to be precisely blockpulsar.yml as Github Actions pick up any configuration file inside .github/workflows. We use blockpulsar.yml to distinguish Github Actions specifically defined for Solana Program deployments over Blockpulsar.

Your Github Action with Blockpulsar has to be executed during the Push Action and Pull Request Action, though it is subjective. It is possible that you might have a different use case. The critical thing is that Blockpulsar doesn’t care when you do the execution. There are no specific actions related to overload or configuration that have to be there to execute the Solana Rust Project building and deployment.

# .github/workflows/blockpulsar.yml

name: blockpulsar-deploy
on: [push, pull_request]
jobs:
deploy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Configuring Blockpulsar
        env:
          API_KEY: ${{ secrets.BLOCKPULSAR_KEY }}
          API_SECRET: ${{ secrets.BLOCKPULSAR_SECRET }}
        run: |
          git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com"
          git config --local user.name "github-actions[bot]"
          git config --local --add safe.directory .
          git fetch --unshallow
          git remote add bp "https://$API_KEY:$API_SECRET@ci.blockpulsar.com/git"
      - name: Push to Blockpulsar Git
        run: git push bp master

That's where all the magic happens 🪄 Most of it is just a standard Github Actions configurations except the part where you have git configurations inside the Github Actions Run command. The thing is that Github Actions are using your repository copy with Git Shallow copy, which prevents you from making git push to another remote origin. You have to configure Git and run git fetch --unshallow to unhallow all commits.

Running the following two commands will execute the entire deployment. The rest of the configurations you see in the YAML file are just a preparation to get your Git repository in proper shape to make git push bp master and start Solana Program building and deployment.

Commands we are aiming to execute inside Github Action

git remote add bp "https://$API_KEY:$API_SECRET@ci.blockpulsar.com/git"
git push bp master

After committing and pushing the .githiub/workflows/blockpulsar.yml file, you will see on the Github Actions page there is a new build process starting up 🥳

Github Deployment Action

If you have everything configured correctly and your Solana Rust code compiles correctly, you will see the following logs inside the “Push To Blockpulsar Git” action step.

remote: Getting Solana account keys
remote:
remote: Solana Keys have been loaded successfully!
remote: Blockpulsar CI starts the build process. Please wait a bit...
remote: Cloning into 'program'...
remote:    Compiling proc-macro2 v1.0.36
remote:    Compiling unicode-xid v0.2.2
...
...
...
remote:    Compiling serde_bytes v0.11.5
remote:    Compiling blockpulsar-solana-example v0.1.0 (/root/program)
remote:     Finished release [optimized] target(s) in 1m 08s
remote: Blockpulsar is checking Solana account balance for dev stage
remote: Solana Account Balance - 1.79041236 SOL
remote: Account balance is less than 2 SOL, Blockpulsar will make 2 SOL Airdrop
remote: Requesting airdrop of 2 SOL
remote:
remote: Signature: ku8aBsodj3N5PGKZ8usbismvhU3q2VMh8V15VWof9D8zQ8c8mZsn5JbqnUV5FUf2U12iBE742GeQxoCxe5nKupv
remote:
remote: 3.79041236 SOL
remote: Blockpulsar is deploying program helloworld.so
remote: Program Id: HxkietqQDU74w1Xa5HK4QLiw8SVA6QYbQQqCKwvwQy5F
remote:
To https://ci.blockpulsar.com/git
* [new branch]      master -> master

You can see that Blockpulsar automatically does the SOL Airdrop and deploys the program like magic 🪄

Conclusion

Your Github Action will run every time you make a Pull Request or Push something to the repository. Ideally, you probably need a configuration based on the master or main branch. Considering that deploying to Solana Mainnet costs real money and mistakes could be detrimental for your Solana Blockchain project, you will want to make sure that it works properly before deploying the program to Mainnet. That’s why automated deployment to Mainnet without testing is not a good idea, at least if you don’t have a proper way of automated code analysis in your Solana Program smart-contract logic.

We, at Blockpulsar.com, would like to hear how you did CI/CD automation on your Solana projects. Please consider saying “Hi” any time on our Discord Server. We are also ready to assist you with configurations and Solana Program development.

Deploy Your First Solana Smart Contract Now!

Blockpulsar provides seamless abstraction layers for building and deploying Solana Programs (smart-contracts) without having to have local Solana Validator setup or complicated key management. You can focus on building an actual smart-contract logic, while we handle the rest.

Blockpulsar.com

Blockpulsar makes smart contract development simple, flexible and scalable by providing CI/CD for smart contracts.

© 2022 Copyright Blockpulsar Inc.