Search code examples
rustgithub-actionssolanaanchor-solanaserum-project

Solana: How to setup github action (CI) for an Anchor Project


I've my anchor project build with serum's anchor framework.
I want to set up github action, So that whenever a new pull request is raised or any commit is made on the main branch, I can be sure that no code has been broken and flag any such pull request.

Here is what I've tried. But it needs around 18 to 20 minutes to run and still unsuccessful.

name: Rust

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]

env:
  CARGO_TERM_COLOR: always

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v3
    - name: Install AVM
      run: cargo install --git https://github.com/project-serum/anchor avm --force
    - name: Install Anchor
      run: avm install 0.24.2 && avm use 0.24.2
    - name: Build
      run: anchor build
    - name: Run tests
  run: anchor test

Solution

  • The initial approach was not efficient
    cons:

    • need many manual installations of solana, nvm, node and yarn
    • hence too much time for a run
    • complex
    • reinventing wheel

    When dug deep found out that found out that serum releases docker images for this purpose only. So I modified my action file to

    name: Rust
    on:
      push:
        branches: [main]
      pull_request:
        branches: [main]
    env:
      CARGO_TERM_COLOR: always
    jobs:
      build:
        runs-on: ubuntu-latest
        container: projectserum/build:v0.24.2
    
        steps:
          - uses: actions/checkout@v3
          - name: List rustup toolchains
            run: rustup toolchain list
          - name: Set default toolchain
            run: rustup default stable
          - name: List rustup toolchains
            run: rustup toolchain list
          - name: Generate new keygen
            run: solana-keygen new
          - name: Set solana target cluster to local
            run: solana config set --url http:localhost:8899
          - name: Check solana config
            run: solana config get
          - name: Install yarn dependencies
            run: yarn install
          - name: Build
            run: anchor build
          - name: Run tests
            run: anchor test
    

    It brought down the run time from 18 minutes to 3 minutes approximately.

    References: