Solidity and Hardhat Continuous Integration via GitHub Actions

Barış ATALAY
Teknasyon Engineering

--

Solidity and Hardhat Continuous Integration via GitHub Actions

The majority of articles prepared for Solidity Continuous Integration on the internet were prepared for Truffle. Greetings to all my friends who use Hardhat as a development environment like me.

Here is Solidity and Hardhat Continuous Integration via GitHub Actions in its simplest explanation.

In this example, we will execute Github Action to do the following steps for us:

  • Getting current branch codes
  • NodeJS installation and printing version information
  • Hardhat installation
  • Hardhat etc. Installing dependencies
  • Running Hardhat Tests

As a first step, let’s create our *.yml file.

After creating the .github/workflows/ folders, let’s add our “hardhat.yml” file to it.

Solidity and Hardhat Continuous Integration via GitHub Actions

You can add the following section as boilerplate code for Github Action.

Let’s explain these:

  • Label of the action to be run
  • Which brach executed is requested?
  • Which branch is requested to run when a pull request is received?
  • “runs-on” which system will be run on the temporary server to be stood up
name: Hardhat Build
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
jobs:
build:
runs-on: ubuntu-latest
steps:

STEPS

  • Getting current branch codes
- uses: actions/checkout@v2
  • Setup NodeJS (20.12.2)
- name: Setup NodeJS 20.12.2
uses: actions/setup-node@v2
with:
node-version: 20.12.2
cache: npm

- name: Show NodeJS version
run: npm --version
  • Install Hardhat
- name: Install Hardhat
run: npm install --save-dev hardhat@2.22.3
  • Install Hardhat Dependencies
- name: Install Hardhat Dependencies
run: npm ci
  • Install Solhint and Print Version
- name: Install Solhint
run: npm install -g solhint

- name: Show Solhint version
run: solhint --version

- name: Run Lint
run: solhint 'contracts/**/*.sol'
  • Run Solhint Controls
- name: Run Lint
run: solhint 'contracts/**/*.sol'
Solidity and Hardhat Continuous Integration via GitHub Actions
  • Run Tests
- name: Run Hardhat Test
env:
COINGECKO_API_KEY: ${{ secrets.COINGECKO_API_KEY }}
REPORT_CURRENCY: ${{ secrets.REPORT_CURRENCY }}
REPORT_GAS: true
REPORT_MAIN_TOKEN: ${{ secrets.REPORT_MAIN_TOKEN }}
run: npx hardhat test

Please see the GitHub documentation on how to create a Github Repository Secret.

Solidity and Hardhat Continuous Integration via GitHub Actions

That’s it. Now this Github Action will run for every new Pull Request.

Conclusion

Below you can view the full steps we mentioned in our article.

Best regards.

# Reference: https://github.com/marketplace/actions/setup-node-js-environment 
name: Hardhat Build
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Setup NodeJS 20.12.2
uses: actions/setup-node@v2
with:
node-version: 20.12.2
cache: npm
- name: Show NodeJS version
run: npm --version

- name: Install Hardhat
run: npm install --save-dev hardhat@2.22.3

- name: Install Hardhat Dependencies
run: npm ci

- name: Install Solhint
run: npm install -g solhint

- name: Show Solhint version
run: solhint --version

- name: Run Lint
run: solhint 'contracts/**/*.sol'

- name: Run Hardhat Test
env:
COINGECKO_API_KEY: ${{ secrets.COINGECKO_API_KEY }}
REPORT_CURRENCY: ${{ secrets.REPORT_CURRENCY }}
REPORT_GAS: true
REPORT_MAIN_TOKEN: ${{ secrets.REPORT_MAIN_TOKEN }}
run: npx hardhat test

--

--