<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Olich's Blog]]></title><description><![CDATA[💻 Full Stack Engineer with a fascination for distributed systems, blockchains, and cloud development.
🔭 I’m able to write code in Rust, TypeScript, Java, C# a]]></description><link>https://hashnode.olich.me</link><image><url>https://cdn.hashnode.com/res/hashnode/image/upload/v1647528076624/OEEtq-Ggi.png</url><title>Olich&apos;s Blog</title><link>https://hashnode.olich.me</link></image><generator>RSS for Node</generator><lastBuildDate>Thu, 12 Nov 2026 16:55:19 GMT</lastBuildDate><atom:link href="https://hashnode.olich.me/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Smart contract solidity development with foundry]]></title><description><![CDATA[Developing smart contracts on EVM blockchains requires a robust development environment. While Hardhat is a popular choice, Foundry offers a streamlined…]]></description><link>https://hashnode.olich.me/smart-contract-solidity-development-with-foundry</link><guid isPermaLink="true">https://hashnode.olich.me/smart-contract-solidity-development-with-foundry</guid><category><![CDATA[Solidity]]></category><category><![CDATA[slither]]></category><category><![CDATA[foundry]]></category><category><![CDATA[GitHub]]></category><category><![CDATA[Ethereum]]></category><category><![CDATA[Rust]]></category><category><![CDATA[Blockchain]]></category><category><![CDATA[ci-cd]]></category><dc:creator><![CDATA[Oleh Andrushko]]></dc:creator><pubDate>Thu, 07 Dec 2023 12:09:23 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1701950468593/c392c9bb-058b-42bc-a8cc-9d6b22608419.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Developing smart contracts on EVM blockchains requires a robust development environment. While Hardhat is a popular choice, Foundry offers a streamlined, efficient alternative. This post guides you through setting up a Solidity development repository using Foundry.</p>
<h2 id="heading-prerequisites">Prerequisites</h2>
<ul>
<li><p><a target="_blank" href="https://github.com/">GitHub</a></p>
</li>
<li><p>Latest <a target="_blank" href="https://www.rust-lang.org/">Rust</a> version</p>
</li>
<li><p><a target="_blank" href="https://marketplace.visualstudio.com/items?itemName=JuanBlanco.solidity">Solidity for Visual Studio Code</a></p>
</li>
<li><p>latest <a target="_blank" href="https://docs.soliditylang.org/en/v0.8.23/installing-solidity.html">Solidity</a> version</p>
</li>
</ul>
<h2 id="heading-why-foundry">Why Foundry?</h2>
<blockquote>
<p><a target="_blank" href="https://book.getfoundry.sh/">Foundry</a> is a smart contract development toolchain. Foundry manages your dependencies, compiles your project, runs tests, deploys, and lets you interact with the chain from the command-line and via Solidity scripts. ..<a target="_blank" href="https://www.paradigm.xyz/2021/12/introducing-the-foundry-ethereum-development-toolbox">read foundry annoncement article</a>..</p>
</blockquote>
<p>Foundry was created by the team at <a target="_blank" href="https://www.paradigm.xyz/">Paradigm</a>, a cryptocurrency investment firm, to improve and streamline the experience of building on Ethereum. <a target="_blank" href="https://www.paradigm.xyz/">Paradigm</a> is known for its contributions to the Ethereum ecosystem, with a team that includes researchers, engineers, and security experts.</p>
<p>Foundry is made up of four components:</p>
<ul>
<li><p><a target="_blank" href="https://book.getfoundry.sh/forge/"><strong>Forge</strong></a>: a testing framework (similar to Truffle, Hardhat, and DappTools).</p>
</li>
<li><p><a target="_blank" href="https://book.getfoundry.sh/cast/"><strong>Cast</strong></a>: CLI for interacting (performing RPC calls) with EVM smart contracts, sending transactions, and getting chain data.</p>
</li>
<li><p><a target="_blank" href="https://book.getfoundry.sh/anvil/"><strong>Anvil</strong></a>: local Ethereum node, similar to Ganache or Hardhat Network.</p>
</li>
<li><p><a target="_blank" href="https://book.getfoundry.sh/chisel/"><strong>Chisel</strong></a>: advanced Solidity REPL. It can be used to quickly test the behavior of Solidity snippets on a local or forked network.</p>
</li>
</ul>
<p>When weighing Foundry against other smart contract development frameworks (like Hardhat), it stands out for several key reasons:</p>
<ul>
<li><p><strong>Speed and Efficiency</strong>: Foundry is written in Rust to be blazing fast, easy to install, and friendly to a wider set of contributors. It compiles Solidity code faster than Hardhat, which can be a significant advantage when iterating over smart contract development cycles.</p>
</li>
<li><p><strong>Advanced Testing</strong>: Foundry uses <a target="_blank" href="https://book.getfoundry.sh/forge/advanced-testing">Forge for testing</a>, which allows for <a target="_blank" href="https://www.paradigm.xyz/2021/12/introducing-the-foundry-ethereum-development-toolbox#you-should-be-writing-your-tests-in-solidity">writing tests</a> in Solidity itself, unlike Hardhat which typically uses JavaScript. This can simplify the process as developers can write tests in the same language they're developing the smart contracts.</p>
</li>
<li><p><strong>Integrated Debugging</strong>: The Forge testing framework includes an <a target="_blank" href="https://book.getfoundry.sh/forge/debugger">in-built debugger</a>, which can be used without additional plugins or configurations, offering a streamlined debugging process.</p>
</li>
<li><p><strong>Flexibility and Minimalism:</strong> Foundry is minimalistic and flexible, it comes with fewer out-of-the-box features, which can be seen as a plus for developers who prefer a less opinionated framework that they can tailor to their specific needs.</p>
</li>
<li><p><strong>Built-In Fuzz Testing</strong>: Foundry supports <a target="_blank" href="https://www.paradigm.xyz/2021/12/introducing-the-foundry-ethereum-development-toolbox#you-should-be-fuzzing-your-functions">fuzz testing</a>, which tests contracts with huge amounts of random inputs to ensure robustness against unforeseen inputs.</p>
</li>
<li><p><strong>First-Class Support for Solidity</strong>: Being a solidity-first tool, Foundry provides first-class support for the language, with updates closely following Solidity's updates.</p>
</li>
<li><p><strong>Gas Usage Reports:</strong>: Forge generates <a target="_blank" href="https://book.getfoundry.sh/forge/gas-reports">gas reports</a>, a crucial feature for blockchain development, to help developers optimize smart contract efficiency.</p>
</li>
<li><p><strong>Security Focus</strong>: With tools like <a target="_blank" href="https://book.getfoundry.sh/config/static-analyzers">Slither integrated</a>, Foundry has a strong focus on security, making it easier to write safer smart contracts.</p>
</li>
</ul>
<h2 id="heading-step-1-install-foundry">Step 1: Install Foundry</h2>
<p>Open your terminal and run the following command:</p>
<pre><code class="lang-bash">curl -L https://foundry.paradigm.xyz | bash
</code></pre>
<p>and then:</p>
<pre><code class="lang-bash"><span class="hljs-comment"># if need to refresh the configs</span>
<span class="hljs-built_in">source</span> /path/to/.zshenv
<span class="hljs-comment"># to actually install foundry</span>
foundryup
</code></pre>
<h2 id="heading-step-2-initiate-the-project">Step 2: Initiate the project</h2>
<p>Create a new directory and run the following command inside:</p>
<pre><code class="lang-bash">forge init
</code></pre>
<p>The command will generate a complete project with the following structure:</p>
<ul>
<li><p><code>.github/workflows</code>: a folder that contains a <code>test.yml</code> file with foundry-related GitHub actions</p>
</li>
<li><p><code>lib</code>: a directory for storing actual libraries/dependencies as git-submodules (similar to <code>node_modules</code> in hardhat)</p>
</li>
<li><p><code>script</code>: a directory for solidity scripts (similar to <code>tasks</code> in hardhat)</p>
</li>
<li><p><code>src</code>: a default directory for contracts code (similar to <code>contracts</code> in hardhat)</p>
</li>
<li><p><code>test</code>: a default directory for tests, where any contract with a function that starts with test is considered to be a test.</p>
</li>
<li><p><code>.gitmodules</code>: a file for mapping installed libraries/dependencies as <a target="_blank" href="https://git-scm.com/book/en/v2/Git-Tools-Submodules">git-submodules</a></p>
</li>
<li><p><code>foundry.toml</code>: a configuration file for Foundry behavior: e.g. contract folder location, output etc...</p>
</li>
</ul>
<p>Now you can run the following <a target="_blank" href="https://book.getfoundry.sh/reference/forge/">commands</a> to check if everything is working correctly inside the directory:</p>
<pre><code class="lang-bash"><span class="hljs-comment"># 1. building the project</span>
forge build
<span class="hljs-comment"># 2. testing example contract</span>
forge <span class="hljs-built_in">test</span>
</code></pre>
<h2 id="heading-step-3-smart-contract-developing">Step 3: Smart Contract developing</h2>
<h3 id="heading-install-dependencies">Install dependencies</h3>
<p>Forge manages dependencies using <a target="_blank" href="https://git-scm.com/book/en/v2/Git-Tools-Submodules">git submodules</a> by default, which means that it works with any GitHub repository that contains smart contracts.</p>
<p>In this case, we will use OpenZeppelin library and install it as a dependency in our project by simply running the command:</p>
<pre><code class="lang-bash">forge install OpenZeppelin/openzeppelin-contracts
</code></pre>
<p>It should add the <code>openzeppelin-contracts</code> library in <code>/lib</code> directory. If you are using VS Code, you might see some errors like: "Source "forge-std/Test.sol" not found: File import callback not supported". In order to fix it simply run the <a target="_blank" href="https://book.getfoundry.sh/config/vscode#1-remappings">remappings</a> command:</p>
<pre><code class="lang-bash">forge remappings &gt; remappings.txt
</code></pre>
<h3 id="heading-erc20-token">ERC20 Token</h3>
<p>In this example, we will work with OpenZeppelin ERC20 smart contract:</p>
<ol>
<li>Rename (or create new) the <code>src/Counter.sol</code> to <code>src/MyToken.sol</code> and replace it with the following code:</li>
</ol>
<pre><code class="lang-Solidity"> <span class="hljs-comment">// SPDX-License-Identifier: MIT</span>
<span class="hljs-meta"><span class="hljs-keyword">pragma</span> <span class="hljs-keyword">solidity</span> ^0.8.20;</span>

<span class="hljs-keyword">import</span> <span class="hljs-string">"@openzeppelin/contracts/token/ERC20/ERC20.sol"</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">"@openzeppelin/contracts/access/Ownable.sol"</span>;

<span class="hljs-class"><span class="hljs-keyword">contract</span> <span class="hljs-title">MyToken</span> <span class="hljs-keyword">is</span> <span class="hljs-title">ERC20</span>, <span class="hljs-title">Ownable</span> </span>{
    <span class="hljs-function"><span class="hljs-keyword">constructor</span>(<span class="hljs-params"><span class="hljs-keyword">address</span> initialOwner</span>)
        <span class="hljs-title">ERC20</span>(<span class="hljs-params"><span class="hljs-string">"MyToken"</span>, <span class="hljs-string">"MTK"</span></span>)
        <span class="hljs-title">Ownable</span>(<span class="hljs-params">initialOwner</span>)
    </span>{}

    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">mint</span>(<span class="hljs-params"><span class="hljs-keyword">address</span> to, <span class="hljs-keyword">uint256</span> amount</span>) <span class="hljs-title"><span class="hljs-keyword">public</span></span> <span class="hljs-title">onlyOwner</span> </span>{
        _mint(to, amount);
    }
}
</code></pre>
<ol>
<li>Rename (or create new) the <code>test/Counter.t.sol</code> to <code>test/MyToken.t.sol</code> and replace it with the following code:</li>
</ol>
<pre><code class="lang-Solidity"> <span class="hljs-comment">// SPDX-License-Identifier: MIT</span>
<span class="hljs-meta"><span class="hljs-keyword">pragma</span> <span class="hljs-keyword">solidity</span> ^0.8.20;</span>

<span class="hljs-keyword">import</span> {<span class="hljs-title">Test</span>, <span class="hljs-title">console2</span>} <span class="hljs-title"><span class="hljs-keyword">from</span></span> <span class="hljs-string">"forge-std/Test.sol"</span>;
<span class="hljs-keyword">import</span> {<span class="hljs-title">MyToken</span>} <span class="hljs-title"><span class="hljs-keyword">from</span></span> <span class="hljs-string">"../src/MyToken.sol"</span>;

<span class="hljs-class"><span class="hljs-keyword">contract</span> <span class="hljs-title">MyTokenTest</span> <span class="hljs-keyword">is</span> <span class="hljs-title">Test</span> </span>{
    MyToken <span class="hljs-keyword">public</span> token;

    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">setUp</span>(<span class="hljs-params"></span>) <span class="hljs-title"><span class="hljs-keyword">public</span></span> </span>{
        token <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> MyToken(<span class="hljs-number">0x636C16881D405cdE477f56546825c88862be5189</span>);
    }
}
</code></pre>
<ol>
<li>Rename (or create new) the <code>script/Counter.s.sol</code> to <code>script/MyToken.s.sol</code> and replace it with the following code:</li>
</ol>
<pre><code class="lang-Solidity"><span class="hljs-comment">// SPDX-License-Identifier: MIT</span>
<span class="hljs-meta"><span class="hljs-keyword">pragma</span> <span class="hljs-keyword">solidity</span> ^0.8.20;</span>

<span class="hljs-keyword">import</span> {<span class="hljs-title">Script</span>, <span class="hljs-title">console2</span>} <span class="hljs-title"><span class="hljs-keyword">from</span></span> <span class="hljs-string">"forge-std/Script.sol"</span>;

<span class="hljs-class"><span class="hljs-keyword">contract</span> <span class="hljs-title">MyTokenScript</span> <span class="hljs-keyword">is</span> <span class="hljs-title">Script</span> </span>{
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">setUp</span>(<span class="hljs-params"></span>) <span class="hljs-title"><span class="hljs-keyword">public</span></span> </span>{}

    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">run</span>(<span class="hljs-params"></span>) <span class="hljs-title"><span class="hljs-keyword">public</span></span> </span>{
        vm.broadcast();
    }
}
</code></pre>
<blockquote>
<p><a target="_blank" href="https://book.getfoundry.sh/tutorials/solidity-scripting">Read more on solidity scripting...</a></p>
</blockquote>
<ol>
<li>Run <code>forge build</code> to make sure everything is working as expected:</li>
</ol>
<pre><code class="lang-bash">$ forge build
[⠒] Compiling...
[⠔] Compiling 30 files with 0.8.23
[⠑] Solc 0.8.23 finished <span class="hljs-keyword">in</span> 3.40s
Compiler run successful!
</code></pre>
<h3 id="heading-write-some-tests">Write some tests</h3>
<p>Let's add some more tests for our ERC20 token inside <code>MyToken.t.sol</code>.</p>
<p>Forge uses the following keywords in tests:</p>
<ul>
<li><p><strong>setUp</strong>: an optional function invoked before each test case is run</p>
</li>
<li><p><strong>test</strong>: functions prefixed with test are run as a test case</p>
</li>
<li><p><strong>testFailt</strong>: the inverse of the test prefix - if the function does not revert, the test fails</p>
</li>
</ul>
<blockquote>
<p><a target="_blank" href="https://book.getfoundry.sh/forge/writing-tests">Read more on writing tests in foundry...</a></p>
</blockquote>
<p>For now, let's just add a simple test for minting functionality, here's the complete content of <code>MyToken.t.sol</code>:</p>
<pre><code class="lang-Solidity"><span class="hljs-comment">// SPDX-License-Identifier: MIT</span>
<span class="hljs-meta"><span class="hljs-keyword">pragma</span> <span class="hljs-keyword">solidity</span> ^0.8.20;</span>

<span class="hljs-keyword">import</span> {<span class="hljs-title">Test</span>, <span class="hljs-title">console2</span>} <span class="hljs-title"><span class="hljs-keyword">from</span></span> <span class="hljs-string">"forge-std/Test.sol"</span>;
<span class="hljs-keyword">import</span> {<span class="hljs-title">MyToken</span>} <span class="hljs-title"><span class="hljs-keyword">from</span></span> <span class="hljs-string">"../src/MyToken.sol"</span>;

<span class="hljs-class"><span class="hljs-keyword">contract</span> <span class="hljs-title">MyTokenTest</span> <span class="hljs-keyword">is</span> <span class="hljs-title">Test</span> </span>{
    MyToken <span class="hljs-keyword">public</span> token;
    <span class="hljs-keyword">address</span> <span class="hljs-keyword">constant</span> contractOwner <span class="hljs-operator">=</span> <span class="hljs-number">0x636C16881D405cdE477f56546825c88862be5189</span>;
     <span class="hljs-keyword">address</span> <span class="hljs-keyword">constant</span> alice <span class="hljs-operator">=</span> <span class="hljs-number">0x001C7CA24BC0B0b81b2F9Ee69dEDD76995861C39</span>;

    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">setUp</span>(<span class="hljs-params"></span>) <span class="hljs-title"><span class="hljs-keyword">public</span></span> </span>{
        token <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> MyToken(contractOwner);
    }

    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">testMint</span>(<span class="hljs-params"></span>) <span class="hljs-title"><span class="hljs-keyword">public</span></span> </span>{ 
        <span class="hljs-comment">// Impersonate the owner to call mint function</span>
        vm.prank(contractOwner);
        <span class="hljs-comment">// mint tokens to Alice</span>
        token.mint(alice, <span class="hljs-number">10000000</span>);
        <span class="hljs-comment">// assert the balance</span>
        assertEq(<span class="hljs-number">10000000</span>, token.balanceOf(alice));
    }
}
</code></pre>
<p>Run the tests:</p>
<pre><code class="lang-bash">forge <span class="hljs-built_in">test</span>
[⠢] Compiling...
No files changed, compilation skipped

Running 1 <span class="hljs-built_in">test</span> <span class="hljs-keyword">for</span> <span class="hljs-built_in">test</span>/MyToken.t.sol:MyTokenTest
[PASS] testMint() (gas: 58176)
Test result: ok. 1 passed; 0 failed; 0 skipped; finished <span class="hljs-keyword">in</span> 528.25µs

Ran 1 <span class="hljs-built_in">test</span> suites: 1 tests passed, 0 failed, 0 skipped (1 total tests)
</code></pre>
<p>Forge can produce <a target="_blank" href="https://book.getfoundry.sh/forge/traces">traces</a> either for failing tests (-vvv) or all tests (-vvvv), what could be very useful for debugging the tests:</p>
<pre><code class="lang-bash">forge <span class="hljs-built_in">test</span> -vvvv
[⠢] Compiling...
No files changed, compilation skipped

Running 1 <span class="hljs-built_in">test</span> <span class="hljs-keyword">for</span> <span class="hljs-built_in">test</span>/MyToken.t.sol:MyTokenTest
[PASS] testMint() (gas: 58176)
Traces:
  [58176] MyTokenTest::testMint()
    ├─ [0] VM::prank(0x636C16881D405cdE477f56546825c88862be5189)
    │   └─ ← ()
    ├─ [49064] MyToken::mint(0x001C7CA24BC0B0b81b2F9Ee69dEDD76995861C39, 10000000 [1e7])
    │   ├─ emit Transfer(from: 0x0000000000000000000000000000000000000000, to: 0x001C7CA24BC0B0b81b2F9Ee69dEDD76995861C39, value: 10000000 [1e7])
    │   └─ ← ()
    ├─ [563] MyToken::balanceOf(0x001C7CA24BC0B0b81b2F9Ee69dEDD76995861C39) [staticcall]
    │   └─ ← 10000000 [1e7]
    └─ ← ()

Test result: ok. 1 passed; 0 failed; 0 skipped; finished <span class="hljs-keyword">in</span> 1.09ms

Ran 1 <span class="hljs-built_in">test</span> suites: 1 tests passed, 0 failed, 0 skipped (1 total tests)
</code></pre>
<p>You can also generate a pretty cool gas usage report with <code>--gas-report</code> suffix:</p>
<pre><code class="lang-bash">forge <span class="hljs-built_in">test</span> --gas-report
[⠢] Compiling...
No files changed, compilation skipped

Running 1 <span class="hljs-built_in">test</span> <span class="hljs-keyword">for</span> <span class="hljs-built_in">test</span>/MyToken.t.sol:MyTokenTest
[PASS] testMint() (gas: 58176)
Test result: ok. 1 passed; 0 failed; 0 skipped; finished <span class="hljs-keyword">in</span> 956.46µs
| src/MyToken.sol:MyToken contract |                 |       |        |       |         |
|----------------------------------|-----------------|-------|--------|-------|---------|
| Deployment Cost                  | Deployment Size |       |        |       |         |
| 518572                           | 3010            |       |        |       |         |
| Function Name                    | min             | avg   | median | max   | <span class="hljs-comment"># calls |</span>
| balanceOf                        | 563             | 563   | 563    | 563   | 1       |
| mint                             | 49064           | 49064 | 49064  | 49064 | 1       |

Ran 1 <span class="hljs-built_in">test</span> suites: 1 tests passed, 0 failed, 0 skipped (1 total tests)
</code></pre>
<blockquote>
<p><a target="_blank" href="https://book.getfoundry.sh/tutorials/best-practices#tests">Read more about test best practices...</a></p>
</blockquote>
<h2 id="heading-step-4-configuring-ci-with-github-actions">Step 4: Configuring CI with GitHub actions</h2>
<p>Foudry already created some basic ci steps inside <code>.github/workflows/test.yml</code>, but we will boost our pipeline with some more steps like:</p>
<ul>
<li><p>Solidity code <strong>format check</strong>: since to keep it simple, we do not have a linter in this setup, we will introduce this simple step just to be sure we are not commiting an ugly code :)</p>
</li>
<li><p><strong>Coverage</strong> report: committing a robust contract code is quite important so we will automatically check our test coverage regression in each PR.</p>
</li>
<li><p><strong>Static analysis</strong>: committing a secure contract code is also important, so we will use <a target="_blank" href="https://github.com/crytic/slither">slither</a> to automatically analyze the code.</p>
</li>
<li><p>Publish <strong>documentation</strong>: foundry has a nice way to automatically <a target="_blank" href="https://book.getfoundry.sh/reference/forge/forge-doc">generate smart contract documentation</a> so we will use that to publish a new version of documentation on [GitHub pages].</p>
</li>
</ul>
<p>Let's start with renaming <code>.github/workflows/test.yml</code> file to <code>.github/workflows/ci.yml</code> and refactor the jobs.</p>
<h3 id="heading-build-and-test">Build and test</h3>
<p>This one is for building and testing our solidity code:</p>
<pre><code class="lang-yml"><span class="hljs-attr">build-n-test:</span>
  <span class="hljs-attr">runs-on:</span> <span class="hljs-string">ubuntu-latest</span>
  <span class="hljs-attr">steps:</span>
    <span class="hljs-bullet">-</span> <span class="hljs-attr">uses:</span> <span class="hljs-string">actions/checkout@v3</span>

    <span class="hljs-bullet">-</span> <span class="hljs-attr">name:</span> <span class="hljs-string">Install</span> <span class="hljs-string">Foundry</span>
      <span class="hljs-attr">uses:</span> <span class="hljs-string">foundry-rs/foundry-toolchain@v1</span>

    <span class="hljs-bullet">-</span> <span class="hljs-attr">name:</span> <span class="hljs-string">Build</span> <span class="hljs-string">contracts</span>
      <span class="hljs-attr">run:</span> <span class="hljs-string">|
        forge --version
        forge build --sizes
</span>
    <span class="hljs-bullet">-</span> <span class="hljs-attr">name:</span> <span class="hljs-string">Run</span> <span class="hljs-string">tests</span>
      <span class="hljs-attr">run:</span> <span class="hljs-string">|</span>
        <span class="hljs-string">forge</span> <span class="hljs-string">test</span> <span class="hljs-string">-vvv</span>
</code></pre>
<h3 id="heading-format-checks">Format checks</h3>
<p>This one is pretty simple, we just need to know if the code is formatted in the right way. A job to add to the ci should be the following:</p>
<pre><code class="lang-yml"><span class="hljs-attr">format:</span>
  <span class="hljs-attr">runs-on:</span> <span class="hljs-string">ubuntu-latest</span>
  <span class="hljs-attr">steps:</span>
    <span class="hljs-bullet">-</span> <span class="hljs-attr">uses:</span> <span class="hljs-string">actions/checkout@v3</span>

    <span class="hljs-bullet">-</span> <span class="hljs-attr">name:</span> <span class="hljs-string">Install</span> <span class="hljs-string">Foundry</span>
      <span class="hljs-attr">uses:</span> <span class="hljs-string">foundry-rs/foundry-toolchain@v1</span>

    <span class="hljs-bullet">-</span> <span class="hljs-attr">name:</span> <span class="hljs-string">Check</span> <span class="hljs-string">formatting</span>
      <span class="hljs-attr">run:</span> <span class="hljs-string">|
        forge --version
        forge fmt --check</span>
</code></pre>
<h3 id="heading-coverage">Coverage</h3>
<p>In this job we:</p>
<ul>
<li><p>use <a target="_blank" href="https://book.getfoundry.sh/reference/forge/forge-coverage">forge coverage</a> command to generate the report.</p>
</li>
<li><p>publish a comprehensive coverage report as a comment on each push request using <a target="_blank" href="https://github.com/romeovs/lcov-reporter-action">romeovs/lcov-reporter-action</a>.</p>
</li>
<li><p>check coverage threshold using <a target="_blank" href="https://github.com/zgosalvez/github-actions-report-lcov">zgosalvez/github-actions-report-lcov</a>.</p>
</li>
</ul>
<p>The complete code for the job looks like this:</p>
<pre><code class="lang-yml"><span class="hljs-attr">coverage:</span>
  <span class="hljs-attr">runs-on:</span> <span class="hljs-string">ubuntu-latest</span>
  <span class="hljs-attr">steps:</span>
    <span class="hljs-bullet">-</span> <span class="hljs-attr">uses:</span> <span class="hljs-string">actions/checkout@v3</span>

    <span class="hljs-bullet">-</span> <span class="hljs-attr">name:</span> <span class="hljs-string">Install</span> <span class="hljs-string">Foundry</span>
      <span class="hljs-attr">uses:</span> <span class="hljs-string">foundry-rs/foundry-toolchain@v1</span>
    <span class="hljs-comment"># Generate coverage report</span>
    <span class="hljs-bullet">-</span> <span class="hljs-attr">name:</span> <span class="hljs-string">Run</span> <span class="hljs-string">coverage</span>
      <span class="hljs-attr">run:</span> <span class="hljs-string">forge</span> <span class="hljs-string">coverage</span> <span class="hljs-string">--report</span> <span class="hljs-string">summary</span> <span class="hljs-string">--report</span> <span class="hljs-string">lcov</span>

    <span class="hljs-comment"># Adjust the paths in this step to exclude specific directories from coverage analysis.</span>
    <span class="hljs-bullet">-</span> <span class="hljs-attr">name:</span> <span class="hljs-string">Filter</span> <span class="hljs-string">directories</span>
      <span class="hljs-attr">run:</span> <span class="hljs-string">|
        sudo apt update &amp;&amp; sudo apt install -y lcov
        lcov --remove lcov.info 'test/*' 'script/*' --output-file lcov.info --rc lcov_branch_coverage=1
</span>
    <span class="hljs-comment"># This step automatically publishes a comprehensive coverage report as a comment on each push and</span>
    <span class="hljs-comment"># removes any previous comments. </span>
    <span class="hljs-bullet">-</span> <span class="hljs-attr">name:</span> <span class="hljs-string">Post</span> <span class="hljs-string">coverage</span> <span class="hljs-string">report</span>
      <span class="hljs-attr">if:</span> <span class="hljs-string">github.event_name</span> <span class="hljs-string">==</span> <span class="hljs-string">'pull_request'</span>
      <span class="hljs-attr">uses:</span> <span class="hljs-string">romeovs/lcov-reporter-action@v0.3.1</span>
      <span class="hljs-attr">with:</span>
        <span class="hljs-attr">delete-old-comments:</span> <span class="hljs-literal">true</span>
        <span class="hljs-attr">lcov-file:</span> <span class="hljs-string">./lcov.info</span>
        <span class="hljs-attr">github-token:</span> <span class="hljs-string">${{</span> <span class="hljs-string">secrets.GITHUB_TOKEN</span> <span class="hljs-string">}}</span> <span class="hljs-comment"># Adds a coverage summary comment to the PR.</span>

    <span class="hljs-comment"># This step verifies that the minimum coverage threshold is met and fails if it is not.</span>
    <span class="hljs-bullet">-</span> <span class="hljs-attr">name:</span> <span class="hljs-string">Verify</span> <span class="hljs-string">minimum</span> <span class="hljs-string">coverage</span>
      <span class="hljs-attr">uses:</span> <span class="hljs-string">zgosalvez/github-actions-report-lcov@v4</span>
      <span class="hljs-attr">with:</span>
        <span class="hljs-attr">coverage-files:</span> <span class="hljs-string">./lcov.info</span>
        <span class="hljs-attr">minimum-coverage:</span> <span class="hljs-number">100</span> <span class="hljs-comment"># Set coverage threshold.</span>
</code></pre>
<p>You can see the example comment <a target="_blank" href="https://github.com/olich97/solidity-foundry-template/pull/2#issuecomment-1845147545">here</a>.</p>
<blockquote>
<p>Remember not to optimize for coverage, but to optimize for <a target="_blank" href="https://book.getfoundry.sh/tutorials/best-practices?highlight=coverage#best-practices-1">well thought-out tests</a>.</p>
</blockquote>
<h3 id="heading-static-analysis">Static analysis</h3>
<p>For static analysis, we use <a target="_blank" href="https://github.com/crytic/slither-action">slither actions</a> and the complete code for the job looks like this:</p>
<pre><code class="lang-yml"><span class="hljs-attr">code-analysis:</span>
  <span class="hljs-attr">runs-on:</span> <span class="hljs-string">ubuntu-latest</span>
  <span class="hljs-attr">permissions:</span>
    <span class="hljs-comment"># required for uploading slither sarif results</span>
    <span class="hljs-attr">security-events:</span> <span class="hljs-string">write</span>
  <span class="hljs-attr">steps:</span>
    <span class="hljs-bullet">-</span> <span class="hljs-attr">uses:</span> <span class="hljs-string">actions/checkout@v2</span>

    <span class="hljs-bullet">-</span> <span class="hljs-attr">name:</span> <span class="hljs-string">Run</span> <span class="hljs-string">static</span> <span class="hljs-string">analysis</span> <span class="hljs-string">with</span> <span class="hljs-string">slither</span>
      <span class="hljs-attr">uses:</span> <span class="hljs-string">crytic/slither-action@v0.3.0</span>
      <span class="hljs-attr">id:</span> <span class="hljs-string">slither</span>
      <span class="hljs-attr">continue-on-error:</span> <span class="hljs-literal">true</span>
      <span class="hljs-attr">with:</span>
        <span class="hljs-attr">sarif:</span> <span class="hljs-string">results.sarif</span>
        <span class="hljs-attr">slither-args:</span> <span class="hljs-string">--filter-paths</span> <span class="hljs-string">"./lib|./test"</span> <span class="hljs-comment"># exclude other contracts (open zeppelin ones) and test </span>

    <span class="hljs-bullet">-</span> <span class="hljs-attr">name:</span> <span class="hljs-string">Upload</span> <span class="hljs-string">static</span> <span class="hljs-string">anaylisis</span> <span class="hljs-string">SARIF</span> <span class="hljs-string">output</span> <span class="hljs-string">file</span> <span class="hljs-comment"># it should also add relevant comments on PR</span>
      <span class="hljs-attr">uses:</span> <span class="hljs-string">github/codeql-action/upload-sarif@v2</span>
      <span class="hljs-attr">with:</span>
        <span class="hljs-attr">sarif_file:</span> <span class="hljs-string">${{</span> <span class="hljs-string">steps.slither.outputs.sarif</span> <span class="hljs-string">}}</span>
</code></pre>
<h3 id="heading-documentation">Documentation</h3>
<p>This will be a separate workflow and it will be executed only then we push to <code>main</code> branch. The flow will build the documentation and publish the generated static website to GitHub pages of the repository.</p>
<p>To do so just create a new file <code>.github/workflows/docs.yml</code> with the following content:</p>
<pre><code class="lang-yml"><span class="hljs-comment"># Simple workflow for deploying static content to GitHub Pages</span>
<span class="hljs-attr">name:</span> <span class="hljs-string">Documentation</span> <span class="hljs-string">Deploy</span>

<span class="hljs-attr">on:</span>
  <span class="hljs-comment"># Runs on pushes targeting the default branch</span>
  <span class="hljs-attr">push:</span>
    <span class="hljs-attr">branches:</span> [<span class="hljs-string">"main"</span>]
  <span class="hljs-comment"># Allows you to run this workflow manually from the Actions tab</span>
  <span class="hljs-attr">workflow_dispatch:</span>

<span class="hljs-comment"># Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages</span>
<span class="hljs-attr">permissions:</span>
  <span class="hljs-attr">contents:</span> <span class="hljs-string">read</span>
  <span class="hljs-attr">pages:</span> <span class="hljs-string">write</span>
  <span class="hljs-attr">id-token:</span> <span class="hljs-string">write</span>

<span class="hljs-comment"># Allow only one concurrent deployment, skipping runs queued between the run in-progress and latest queued.</span>
<span class="hljs-comment"># However, do NOT cancel in-progress runs as we want to allow these production deployments to complete.</span>
<span class="hljs-attr">concurrency:</span>
  <span class="hljs-attr">group:</span> <span class="hljs-string">"pages"</span>
  <span class="hljs-attr">cancel-in-progress:</span> <span class="hljs-literal">false</span>

<span class="hljs-attr">jobs:</span>
  <span class="hljs-comment"># Single deploy job since we're just deploying</span>
  <span class="hljs-attr">build-n-deploy:</span>
    <span class="hljs-attr">environment:</span>
      <span class="hljs-attr">name:</span> <span class="hljs-string">github-pages</span>
      <span class="hljs-attr">url:</span> <span class="hljs-string">${{</span> <span class="hljs-string">steps.deployment.outputs.page_url</span> <span class="hljs-string">}}</span>
    <span class="hljs-attr">runs-on:</span> <span class="hljs-string">ubuntu-latest</span>
    <span class="hljs-attr">steps:</span>
      <span class="hljs-bullet">-</span> <span class="hljs-attr">name:</span> <span class="hljs-string">Checkout</span>
        <span class="hljs-attr">uses:</span> <span class="hljs-string">actions/checkout@v4</span>

      <span class="hljs-bullet">-</span> <span class="hljs-attr">name:</span> <span class="hljs-string">Install</span> <span class="hljs-string">Foundry</span>
        <span class="hljs-attr">uses:</span> <span class="hljs-string">foundry-rs/foundry-toolchain@v1</span> 

      <span class="hljs-bullet">-</span> <span class="hljs-attr">name:</span> <span class="hljs-string">Build</span> <span class="hljs-string">documentation</span>
        <span class="hljs-attr">run:</span> <span class="hljs-string">|
          forge --version
          forge doc --build
</span>
      <span class="hljs-bullet">-</span> <span class="hljs-attr">name:</span> <span class="hljs-string">Setup</span> <span class="hljs-string">Pages</span>
        <span class="hljs-attr">uses:</span> <span class="hljs-string">actions/configure-pages@v4</span>

      <span class="hljs-bullet">-</span> <span class="hljs-attr">name:</span> <span class="hljs-string">Upload</span> <span class="hljs-string">artifact</span>
        <span class="hljs-attr">uses:</span> <span class="hljs-string">actions/upload-pages-artifact@v2</span>
        <span class="hljs-attr">with:</span>
          <span class="hljs-comment"># Upload final docs folder</span>
          <span class="hljs-attr">path:</span> <span class="hljs-string">'docs/book'</span>
      <span class="hljs-bullet">-</span> <span class="hljs-attr">name:</span> <span class="hljs-string">Deploy</span> <span class="hljs-string">to</span> <span class="hljs-string">GitHub</span> <span class="hljs-string">Pages</span>
        <span class="hljs-attr">id:</span> <span class="hljs-string">deployment</span>
        <span class="hljs-attr">uses:</span> <span class="hljs-string">actions/deploy-pages@v3</span>
</code></pre>
<p>This will automatically generate documentation for your smart contract and publish it to GitHub pages like this one: https://olich97.github.io/solidity-foundry-template.</p>
<p>We are done with the CI configuration and you can view the complete code for this GitHub Actions setup under <a target="_blank" href="https://github.com/olich97/solidity-foundry-template/tree/main/.github/workflows">olich97/solidity-foundry-template/.github/workflows</a>.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1701950644039/0c1850d9-325b-495f-94c7-2f040c108714.gif" alt class="image--center mx-auto" /></p>
<h2 id="heading-step-5-smart-contract-deployment-and-verification">Step 5: Smart Contract deployment and verification</h2>
<p>There are <a target="_blank" href="https://book.getfoundry.sh/forge/deploying">different ways</a> to deploy and verify a smart contract with foundry.</p>
<p>In this case, I choose to write my own deployment script and use a <code>.env</code> file for configuration.</p>
<blockquote>
<p><strong>Note</strong>: you must be careful when exposing private keys in a <code>.env</code> file and loading them into programs. This is only recommended for use with non-privileged deployers or for local / test setups. For production setups please review the various <a target="_blank" href="https://book.getfoundry.sh/reference/forge/forge-script#wallet-options---raw">wallet options</a> that Foundry supports.</p>
</blockquote>
<ol>
<li>Rename (or create new) previously updated <code>script/MyToken.s.sol</code> to <code>script/DeployMyToken.sol</code> and replace the content with the following code:</li>
</ol>
<pre><code class="lang-Solidity"><span class="hljs-comment">// SPDX-License-Identifier: MIT</span>
<span class="hljs-meta"><span class="hljs-keyword">pragma</span> <span class="hljs-keyword">solidity</span> ^0.8.20;</span>

<span class="hljs-keyword">import</span> {<span class="hljs-title">Script</span>, <span class="hljs-title">console2</span>} <span class="hljs-title"><span class="hljs-keyword">from</span></span> <span class="hljs-string">"forge-std/Script.sol"</span>;
<span class="hljs-keyword">import</span> {<span class="hljs-title">MyToken</span>} <span class="hljs-title"><span class="hljs-keyword">from</span></span> <span class="hljs-string">"../src/MyToken.sol"</span>;

<span class="hljs-class"><span class="hljs-keyword">contract</span> <span class="hljs-title">DeployMyTokenScript</span> <span class="hljs-keyword">is</span> <span class="hljs-title">Script</span> </span>{
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">run</span>(<span class="hljs-params"></span>) <span class="hljs-title"><span class="hljs-keyword">external</span></span> </span>{
        <span class="hljs-comment">// load variables from envinronment</span>
        <span class="hljs-keyword">uint256</span> deployerPrivateKey <span class="hljs-operator">=</span> vm.envUint(<span class="hljs-string">"DEPLOYER_PRIVATE_KEY"</span>);
        <span class="hljs-keyword">address</span> contractOwner <span class="hljs-operator">=</span> vm.envAddress(<span class="hljs-string">"CONTRACT_OWNER_ADDRESS"</span>);
        <span class="hljs-comment">// deploying the contract</span>
        vm.startBroadcast(deployerPrivateKey);
        MyToken token <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> MyToken(contractOwner);
        vm.stopBroadcast();
    }
}
</code></pre>
<ol>
<li>Create the <code>.env</code> file with following content:</li>
</ol>
<pre><code class="lang-typescript"># Get your api key <span class="hljs-keyword">from</span>: https:<span class="hljs-comment">//etherscan.io/</span>
ETHERSCAN_API_KEY=&lt;your-etherscan-api-key&gt;
# Get your api key <span class="hljs-keyword">from</span>: https:<span class="hljs-comment">//www.alchemy.com/pricing</span>
SEPOLIA_URL=https:<span class="hljs-comment">//eth-sepolia.alchemyapi.io/v2/&lt;alchemy key&gt;</span>
# <span class="hljs-keyword">export</span> <span class="hljs-keyword">from</span> wallet
DEPLOYER_PRIVATE_KEY=&lt;your-account-<span class="hljs-keyword">private</span>-key&gt;
# <span class="hljs-keyword">in</span> order to transfer ownership after deploy
CONTRACT_OWNER_ADDRESS= &lt;target address&gt;
</code></pre>
<ol>
<li>Run the following commands to deploy and verify the contract on Sepolia:</li>
</ol>
<pre><code class="lang-bash"><span class="hljs-comment"># To load the variables in the .env file</span>
<span class="hljs-built_in">source</span> .env
<span class="hljs-comment"># To deploy and verify our contract</span>
forge script script/DeployMyToken.sol:DeployMyTokenScript --rpc-url <span class="hljs-variable">$SEPOLIA_URL</span> --broadcast --verify -vvvv
</code></pre>
<p>This command will deploy the contract on sepolia testnet and verify it on etherscan, the output should be similar to the following (love the log details!):</p>
<pre><code class="lang-bash">forge script script/DeployMyToken.sol:DeployMyTokenScript --rpc-url <span class="hljs-variable">$SEPOLIA_URL</span> --broadcast --verify -vvvv
[⠢] Compiling...
No files changed, compilation skipped
Traces:
  [576533] DeployMyTokenScript::run()
    ├─ [0] VM::envUint(<span class="hljs-string">"DEPLOYER_PRIVATE_KEY"</span>) [staticcall]
    │   └─ ← &lt;env var value&gt;
    ├─ [0] VM::envAddress(<span class="hljs-string">"CONTRACT_OWNER_ADDRESS"</span>) [staticcall]
    │   └─ ← &lt;env var value&gt;
    ├─ [0] VM::startBroadcast(104482394105896740396624689231901044842727466658525734315239796502028651856635 [1.044e77])
    │   └─ ← ()
    ├─ [518572] → new MyToken@0x72ea9eB9019e52cF53fadA75eC32eC7ccf2d4996
    │   ├─ emit OwnershipTransferred(previousOwner: 0x0000000000000000000000000000000000000000, newOwner: 0xca4938B679aB6e6c9B7a19610C306A2256B7dF2a)
    │   └─ ← 2245 bytes of code
    ├─ [0] VM::stopBroadcast()
    │   └─ ← ()
    └─ ← ()
Script ran successfully.
<span class="hljs-comment">## Setting up 1 EVM.</span>
==========================
Simulated On-chain Traces:
  [618088] → new MyToken@0x72ea9eB9019e52cF53fadA75eC32eC7ccf2d4996
    ├─ emit OwnershipTransferred(previousOwner: 0x0000000000000000000000000000000000000000, newOwner: 0xca4938B679aB6e6c9B7a19610C306A2256B7dF2a)
    └─ ← 2245 bytes of code
==========================
Chain 11155111
Estimated gas price: 14.833583938 gwei
Estimated total gas used <span class="hljs-keyword">for</span> script: 803514
Estimated amount required: 0.011918992364358132 ETH
==========================
<span class="hljs-comment">###</span>
Finding wallets <span class="hljs-keyword">for</span> all the necessary addresses...
<span class="hljs-comment">##</span>
Sending transactions [0 - 0].
⠁ [00:00:00] [<span class="hljs-comment">##################################################################################################################################] 1/1 txes (0.0s)</span>
Transactions saved to: /Users/olich/tmp/solidity-foundry-template/broadcast/DeployMyToken.sol/11155111/run-latest.json
Sensitive values saved to: /Users/olich/tmp/solidity-foundry-template/cache/DeployMyToken.sol/11155111/run-latest.json
<span class="hljs-comment">##</span>
Waiting <span class="hljs-keyword">for</span> receipts.
⠉ [00:00:06] [<span class="hljs-comment">##############################################################################################################################] 1/1 receipts (0.0s)</span>
<span class="hljs-comment">##### sepolia</span>
✅  [Success]Hash: 0xc693b84d7652efb6b2e5844dd7b0cc097d3d6002871d907798b60f9d32d2e7fd
Contract Address: 0x72ea9eB9019e52cF53fadA75eC32eC7ccf2d4996
Block: 4834242
Paid: 0.00645380857284286 ETH (618278 gas * 10.43836037 gwei)
<span class="hljs-comment">##</span>
Transactions saved to: /Users/olich/tmp/solidity-foundry-template/broadcast/DeployMyToken.sol/11155111/run-latest.json
Sensitive values saved to: /Users/olich/tmp/solidity-foundry-template/cache/DeployMyToken.sol/11155111/run-latest.json
==========================
ONCHAIN EXECUTION COMPLETE &amp; SUCCESSFUL.
Total Paid: 0.00645380857284286 ETH (618278 gas * avg 10.43836037 gwei)
<span class="hljs-comment">##</span>
Start verification <span class="hljs-keyword">for</span> (1) contracts
Start verifying contract `0x72ea9eB9019e52cF53fadA75eC32eC7ccf2d4996` deployed on sepolia
<span class="hljs-comment">##</span>
Submitting verification <span class="hljs-keyword">for</span> [src/MyToken.sol:MyToken] 0x72ea9eB9019e52cF53fadA75eC32eC7ccf2d4996.
<span class="hljs-comment">##</span>
Submitted contract <span class="hljs-keyword">for</span> verification:
        Response: `OK`
        GUID: `dnbux2hiruxytzwdylwuedk7dst7esbbwzdxggttqmwsa1rizr`
        URL:
        https://sepolia.etherscan.io/address/0x72ea9eb9019e52cf53fada75ec32ec7ccf2d4996
Contract verification status:
Response: `NOTOK`
Details: `Pending <span class="hljs-keyword">in</span> queue`
Contract verification status:
Response: `OK`
Details: `Pass - Verified`
Contract successfully verified
All (1) contracts were verified!
<span class="hljs-comment">##</span>
Transactions saved to: /Users/olich/tmp/solidity-foundry-template/broadcast/DeployMyToken.sol/11155111/run-latest.json
Sensitive values saved to: /Users/olich/tmp/solidity-foundry-template/cache/DeployMyToken.sol/11155111/run-latest.json
</code></pre>
<h2 id="heading-conclusion">Conclusion</h2>
<p>As the blockchain landscape continues to evolve, tools like Foundry are crucial for developers seeking to build secure, efficient, and reliable smart contracts. This guide serves as an invaluable resource, whether you're a seasoned developer or just starting in the world of blockchain and smart contract development.</p>
<p>Complete code: <a target="_blank" href="https://github.com/olich97/solidity-foundry-template">olich97/solidity-foundry-template</a>.</p>
]]></content:encoded></item><item><title><![CDATA[Setting up smart contract solidity development repository with hardhat]]></title><description><![CDATA[In this blog post, we will discuss the steps to set up a complete and fully operational repo for solidity smart contract development using hardhat]]></description><link>https://hashnode.olich.me/setting-up-smart-contract-solidity-development-repository-with-hardhat</link><guid isPermaLink="true">https://hashnode.olich.me/setting-up-smart-contract-solidity-development-repository-with-hardhat</guid><category><![CDATA[hardhat]]></category><category><![CDATA[Solidity]]></category><category><![CDATA[GitHub]]></category><category><![CDATA[Ethereum]]></category><category><![CDATA[ci-cd]]></category><dc:creator><![CDATA[Oleh Andrushko]]></dc:creator><pubDate>Thu, 12 Oct 2023 13:54:26 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1697118119510/d91ebac6-a88c-4ab2-8ca6-040a8d369183.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>When it comes to developing <a target="_blank" href="https://ethereum.org/en/smart-contracts/">smart contracts</a> on the EVM blockchains, having a reliable and efficient development environment is crucial. One popular option among developers is Hardhat, a powerful development framework that simplifies the process of building, testing, and deploying smart contracts.</p>
<p>In this blog post, we will discuss the steps to set up a complete and fully operational repository for solidity smart contract development using hardhat, biome, solhint, husky and configuring a CI on GitHub with slither.</p>
<h2 id="heading-prerequisites"><strong>Prerequisites</strong></h2>
<ul>
<li><p><a target="_blank" href="https://nodejs.org/en">Node</a></p>
</li>
<li><p><a target="_blank" href="https://yarnpkg.com/getting-started">yarn</a> or <a target="_blank" href="https://docs.npmjs.com/cli/v8">npm</a>: in this article, I’ll use yarn</p>
</li>
<li><p>GitHub</p>
</li>
<li><p><a target="_blank" href="https://hardhat.org/hardhat-vscode/docs/overview#hardhat-for-visual-studio-code">Hardhat for Visual Studio Code</a></p>
</li>
</ul>
<h2 id="heading-step-1-setting-up-the-initial-project">Step 1: Setting up the initial project</h2>
<ol>
<li>First of all, we need to initialise the project inside your chosen location by running:</li>
</ol>
<pre><code class="lang-bash">yarn init -y
</code></pre>
<p>it will create a package.json file.</p>
<ol>
<li>Install hardhat by running the command:</li>
</ol>
<pre><code class="lang-bash">yarn add --dev hardhat
</code></pre>
<p>it will install hardhat package as dev dependency.</p>
<ol>
<li>Create the project by running command:</li>
</ol>
<pre><code class="lang-bash">npx hardhat init
</code></pre>
<ol>
<li>Select the project type to create:</li>
</ol>
<pre><code class="lang-bash">888    888                      888 888               888
888    888                      888 888               888
888    888                      888 888               888
8888888888  8888b.  888d888 .d88888 88888b.   8888b.  888888
888    888     <span class="hljs-string">"88b 888P"</span>  d88<span class="hljs-string">" 888 888 "</span>88b     <span class="hljs-string">"88b 888
888    888 .d888888 888    888  888 888  888 .d888888 888
888    888 888  888 888    Y88b 888 888  888 888  888 Y88b.
888    888 "</span>Y888888 888     <span class="hljs-string">"Y88888 888  888 "</span>Y888888  <span class="hljs-string">"Y888

👷 Welcome to Hardhat v2.18.0 👷‍

? What do you want to do? … 
  Create a JavaScript project
❯ Create a TypeScript project
  Create an empty hardhat.config.js
  Quit</span>
</code></pre>
<p>I will be using <em>TypeScript</em> so select <em>Create a TypeScript project</em> and follow the instructions.</p>
<p>It should create the following structure:</p>
<ul>
<li><p><code>contracts/</code> is where the source files for your contracts should be.</p>
</li>
<li><p><code>test/</code> is where your tests should go.</p>
</li>
<li><p><code>scripts/</code> is where simple automation scripts go.</p>
</li>
<li><p><code>hardhat.config.js</code> is where hardhat configurations and helper functions go.</p>
</li>
</ul>
<p>If you need to change these paths, take a look at the <a target="_blank" href="https://hardhat.org/hardhat-runner/docs/config#path-configuration">paths configuration section</a>.</p>
<p>I also prefer to customise <code>yarn</code> commands in <code>package.json</code> file by adding the scripts section in this way:</p>
<pre><code class="lang-json"><span class="hljs-string">"scripts"</span>: {
    <span class="hljs-attr">"clean"</span>: <span class="hljs-string">"npx hardhat clean"</span>,
    <span class="hljs-attr">"test"</span>: <span class="hljs-string">"npx hardhat test"</span>,
    <span class="hljs-attr">"chain"</span>: <span class="hljs-string">"npx hardhat node"</span>,
    <span class="hljs-attr">"coverage"</span>: <span class="hljs-string">"npx hardhat coverage"</span>,
    <span class="hljs-attr">"lint"</span>: <span class="hljs-string">"yarn dlx @biomejs/biome check . &amp; solhint 'contracts/**/*.sol'"</span>,
    <span class="hljs-attr">"lint:fix"</span>: <span class="hljs-string">"yarn dlx @biomejs/biome format . --write &amp; solhint contracts/**/*.sol --fix &amp; prettier --write contracts/**/*.sol"</span>,
    <span class="hljs-attr">"compile"</span>: <span class="hljs-string">"npx hardhat compile"</span>,
    <span class="hljs-attr">"deploy:local"</span>: <span class="hljs-string">"npx hardhat run --network localhost scripts/deploy.ts"</span>,
    <span class="hljs-attr">"deploy:sepolia"</span>: <span class="hljs-string">"npx hardhat run --network sepolia scripts/deploy.ts"</span>,
    <span class="hljs-attr">"prepare"</span>: <span class="hljs-string">"husky install"</span>,
    <span class="hljs-attr">"postinstall"</span>: <span class="hljs-string">"husky install"</span>,
    <span class="hljs-attr">"prepack"</span>: <span class="hljs-string">"pinst --disable"</span>,
    <span class="hljs-attr">"postpack"</span>: <span class="hljs-string">"pinst --enable"</span>
  },
</code></pre>
<h2 id="heading-step-2-adding-lint-and-hooks">Step 2: Adding lint and hooks</h2>
<p>We are gonna use two main lint packages:</p>
<ul>
<li><p><a target="_blank" href="https://biomejs.dev/">biome</a>: for formatting and linting TypeScript code</p>
</li>
<li><p><a target="_blank" href="https://github.com/protofire/solhint">solhint</a>: for formatting and linting Solidity code with <a target="_blank" href="https://hardhat.org/hardhat-runner/plugins/nomiclabs-hardhat-solhint">hardhat plugin</a>.</p>
</li>
</ul>
<p>and for setting up pre-commit or pre-push hooks we will use <a target="_blank" href="https://typicode.github.io/husky/getting-started.html">husky</a>.</p>
<h3 id="heading-biome">Biome</h3>
<ol>
<li>To install Biome, we need to run the following commands in a directory containing a <code>package.json</code> file:</li>
</ol>
<pre><code class="lang-shell">yarn add --dev --exact @biomejs/biome
</code></pre>
<ol>
<li>To create the configuration, run the <code>init</code> command in the root folder of your project:</li>
</ol>
<pre><code class="lang-bash">yarn dlx @biomejs/biome init
</code></pre>
<p>It will create a <code>biome.json</code> file with some default lint configurations.</p>
<ol>
<li>We need to ignore <code>typechain-types</code> files in <code>biome.json</code> like this:</li>
</ol>
<pre><code class="lang-json">{
    <span class="hljs-attr">"$schema"</span>: <span class="hljs-string">"https://biomejs.dev/schemas/1.2.2/schema.json"</span>,
    <span class="hljs-attr">"organizeImports"</span>: {
       <span class="hljs-attr">"enabled"</span>: <span class="hljs-literal">true</span>
    },
    <span class="hljs-attr">"linter"</span>: {
       <span class="hljs-attr">"enabled"</span>: <span class="hljs-literal">true</span>,
       <span class="hljs-attr">"rules"</span>: {
        <span class="hljs-attr">"recommended"</span>: <span class="hljs-literal">true</span>
       }
    },
    <span class="hljs-attr">"files"</span>: {
       <span class="hljs-attr">"ignore"</span>: [<span class="hljs-string">"./typechain-types"</span>]
    }
}
</code></pre>
<p>Now we can run the following commands:</p>
<pre><code class="lang-bash"><span class="hljs-comment"># lint any file </span>
yarn dlx @biomejs/biome check .
<span class="hljs-comment"># format any file</span>
yarn dlx @biomejs/biome format . --write
</code></pre>
<h3 id="heading-solhint">Solhint</h3>
<ol>
<li><p>Make sure to have <a target="_blank" href="https://github.com/protofire/solhint">solhint</a> installed globally</p>
</li>
<li><p>Install <a target="_blank" href="https://hardhat.org/hardhat-runner/plugins/nomiclabs-hardhat-solhint">hardhat-solhint</a> package:</p>
</li>
</ol>
<pre><code class="lang-bash">yarn add --dev @nomiclabs/hardhat-solhint
</code></pre>
<ol>
<li>And add the following statement to your <code>hardhat.config.js</code>:</li>
</ol>
<pre><code class="lang-bash"> import <span class="hljs-string">"@nomiclabs/hardhat-solhint"</span>;
</code></pre>
<ol>
<li>Add solhint configuration file:</li>
</ol>
<pre><code class="lang-bash">solhint --init
</code></pre>
<p>Now you can run the following commands to lint:</p>
<pre><code class="lang-bash">npx hardhat check
<span class="hljs-comment"># or</span>
solhint <span class="hljs-string">'contracts/**/*.sol'</span>
</code></pre>
<p>Or if you configured the package.json with custom commands, you can just run:</p>
<pre><code class="lang-bash"><span class="hljs-comment"># checking .ts and .sol files</span>
yarn run lint
<span class="hljs-comment"># formatting .ts and .sol files</span>
yarn run lint:fix
</code></pre>
<h3 id="heading-husky">Husky</h3>
<blockquote>
<p>You can use it to <strong>lint your commit messages</strong>, <strong>run tests</strong>, <strong>lint code</strong>, etc... when you commit or push. Husky supports <a target="_blank" href="https://git-scm.com/docs/githooks"><strong>all client-side Git hooks</strong></a>.</p>
</blockquote>
<p>Install husky in the project</p>
<pre><code class="lang-bash">yarn dlx husky-init --yarn2 &amp;&amp; yarn
</code></pre>
<p>It will:</p>
<ul>
<li><p>Add <code>prepare</code> script to <code>package.json</code></p>
</li>
<li><p>Create a sample <code>pre-commit</code> hook that you can edit (by default, <code>npm test</code> will run when you commit)</p>
</li>
<li><p>Configure Git hooks path</p>
</li>
</ul>
<p>I prefer running tests and lint on push, to do that I will add a new config by running the command:</p>
<pre><code class="lang-bash">npx husky add .husky/pre-push
</code></pre>
<p>and add following content to created file:</p>
<pre><code class="lang-bash"><span class="hljs-meta">#!/usr/bin/env sh</span>
. <span class="hljs-string">"<span class="hljs-subst">$(dirname -- <span class="hljs-string">"<span class="hljs-variable">$0</span>"</span>)</span>/_/husky.sh"</span>

<span class="hljs-built_in">echo</span> <span class="hljs-string">"🕺 Permorming linting checks..."</span>
yarn lint
<span class="hljs-built_in">echo</span> <span class="hljs-string">"Your code is beatiful, nice job 👍"</span>

<span class="hljs-built_in">echo</span> <span class="hljs-string">"🧪 Testing your ugly code..."</span>
yarn <span class="hljs-built_in">test</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"All tests passed 🚀"</span>
</code></pre>
<p>Now every time we push to the repository we will run lint and tests automatically.</p>
<h2 id="heading-step-3-hardhat-configuration">Step 3: Hardhat configuration</h2>
<p>In this step, we need to enhance hardhat with some more configurations for supported chains.</p>
<ol>
<li>Create a <code>.env</code> file in the project directory with the following content:</li>
</ol>
<pre><code class="lang-bash"><span class="hljs-comment"># Get your api key from: https://etherscan.io/</span>
ETHERSCAN_API_KEY=&lt;your-etherscan-api-key&gt;
<span class="hljs-comment"># Get your api key from: https://www.alchemy.com/pricing</span>
SEPOLIA_URL=https://eth-sepolia.alchemyapi.io/v2/&lt;alchemy key&gt;
<span class="hljs-comment"># export from wallet</span>
PRIVATE_KEY=&lt;your-account-private-key&gt;
<span class="hljs-comment"># for gas extimation in unit tests</span>
REPORT_GAS=<span class="hljs-literal">true</span>
<span class="hljs-comment"># Get your api key from: https://coinmarketcap.com/api/</span>
REPORT_GAS_COIN_MARKET_CAP_KEY=&lt;your-account-key&gt;
<span class="hljs-comment"># in order to transfer ownership after deploy</span>
CONTRACTS_OWNER_ADDRESS= &lt;target address&gt;
</code></pre>
<ol>
<li>Update <code>hardhat.config.ts</code> with the following content:</li>
</ol>
<pre><code class="lang-typescript"><span class="hljs-keyword">import</span> <span class="hljs-string">"@nomicfoundation/hardhat-toolbox"</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">"@nomiclabs/hardhat-solhint"</span>;
<span class="hljs-keyword">import</span> { HardhatUserConfig } <span class="hljs-keyword">from</span> <span class="hljs-string">"hardhat/config"</span>;

<span class="hljs-keyword">const</span> config: HardhatUserConfig = {
    solidity: {
     version: <span class="hljs-string">"0.8.19"</span>,
     settings: {
      <span class="hljs-comment">// property of the solidity compiler</span>
      <span class="hljs-comment">// maximize gas cost savings by taking into consideration how many times the bytecode is expected to run (lifetime)</span>
      <span class="hljs-comment">// and the cost of introducing an optimization at compile time</span>
      optimizer: {
       enabled: <span class="hljs-literal">true</span>,
       runs: <span class="hljs-number">200</span>,
      },
     },
    },
    networks: {
     sepolia: {
      url: process.env.SEPOLIA_URL_URL || <span class="hljs-string">""</span>,
      accounts:
       process.env.PRIVATE_KEY !== <span class="hljs-literal">undefined</span> ? [process.env.PRIVATE_KEY] : [],
     },
    },
    gasReporter: {
     enabled: process.env.REPORT_GAS !== <span class="hljs-literal">undefined</span>,
     coinmarketcap: process.env.REPORT_GAS_COIN_MARKET_CAP_KEY || <span class="hljs-string">""</span>,
    },
    etherscan: {
     apiKey: process.env.ETHERSCAN_API_KEY,
    },
};

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> config;
</code></pre>
<p>Now we should be able (after setting the variables in <code>.env</code>) to deploy and verify the contract on sepolia chain:</p>
<pre><code class="lang-bash"><span class="hljs-comment"># deploy</span>
yarn run deploy:sepolia
<span class="hljs-comment"># contract verification</span>
npx hardhat verify <span class="hljs-string">"&lt;CONTRACT ADDRESS&gt;"</span> --network sepolia
</code></pre>
<h2 id="heading-step-4-continuous-integration-and-github">Step 4: Continuous Integration and GitHub</h2>
<p><a target="_blank" href="https://docs.github.com/en/actions/automating-builds-and-tests/about-continuous-integration">Continuous integration</a> is important for shipping secure, beautiful and reliable code. In this case, we are going to build a CI pipeline with <a target="_blank" href="https://docs.github.com/en/actions/learn-github-actions/understanding-github-actions">GitHub Actions</a>.</p>
<p>This pipeline will:</p>
<ul>
<li><p>Build and test our Solidity code.</p>
</li>
<li><p>Perform static analysis using <a target="_blank" href="https://github.com/crytic/slither">slither</a>.</p>
</li>
</ul>
<blockquote>
<p><strong>Slither</strong> is a Solidity static analysis framework written in Python3. It runs a suite of vulnerability detectors, prints visual information about contract details, and provides an API to easily write custom analyses. Slither enables developers to find vulnerabilities, enhance their code comprehension, and quickly prototype custom analyses.</p>
</blockquote>
<p>To begin let's create a new folder in our project called <code>.github</code>, then another one called <code>workflows</code> and create a new file inside the <code>workflow</code> folder called <code>ci.yml</code>.</p>
<p>Here is the content of <code>ci.yml</code>:</p>
<pre><code class="lang-yaml"><span class="hljs-attr">name:</span> <span class="hljs-string">Continuous</span> <span class="hljs-string">Integration</span>
<span class="hljs-attr">on:</span>
  <span class="hljs-attr">push:</span> <span class="hljs-comment"># run on every push</span>
    <span class="hljs-attr">branches:</span> [ <span class="hljs-string">main</span> ]
  <span class="hljs-attr">pull_request:</span> <span class="hljs-comment"># run on every pull request</span>

<span class="hljs-attr">jobs:</span>
  <span class="hljs-attr">build-n-test:</span> <span class="hljs-comment"># building smart contract, running lint and tests</span>
    <span class="hljs-attr">runs-on:</span> <span class="hljs-string">ubuntu-latest</span>
    <span class="hljs-attr">steps:</span>
      <span class="hljs-bullet">-</span> <span class="hljs-attr">uses:</span> <span class="hljs-string">actions/checkout@v2</span>

      <span class="hljs-bullet">-</span> <span class="hljs-attr">name:</span> <span class="hljs-string">Install</span> <span class="hljs-string">dependencies</span>
        <span class="hljs-attr">run:</span> <span class="hljs-string">yarn</span> <span class="hljs-string">install</span>

      <span class="hljs-bullet">-</span> <span class="hljs-attr">name:</span> <span class="hljs-string">Compile</span> <span class="hljs-string">smart</span> <span class="hljs-string">contract</span>
        <span class="hljs-attr">run:</span> <span class="hljs-string">yarn</span> <span class="hljs-string">compile</span>

      <span class="hljs-bullet">-</span> <span class="hljs-attr">name:</span> <span class="hljs-string">Run</span> <span class="hljs-string">linting</span> <span class="hljs-string">checks</span>
        <span class="hljs-attr">run:</span> <span class="hljs-string">yarn</span> <span class="hljs-string">lint</span>

      <span class="hljs-bullet">-</span> <span class="hljs-attr">name:</span> <span class="hljs-string">Run</span> <span class="hljs-string">tests</span>
        <span class="hljs-attr">run:</span> <span class="hljs-string">yarn</span> <span class="hljs-string">test</span>

  <span class="hljs-attr">code-analysis:</span> <span class="hljs-comment"># static code analysis</span>
    <span class="hljs-attr">runs-on:</span> <span class="hljs-string">ubuntu-latest</span>
    <span class="hljs-attr">permissions:</span>
      <span class="hljs-comment"># required for uploading slither sarif results</span>
      <span class="hljs-attr">security-events:</span> <span class="hljs-string">write</span>
    <span class="hljs-attr">steps:</span>
      <span class="hljs-bullet">-</span> <span class="hljs-attr">uses:</span> <span class="hljs-string">actions/checkout@v2</span>

      <span class="hljs-bullet">-</span> <span class="hljs-attr">name:</span> <span class="hljs-string">Run</span> <span class="hljs-string">static</span> <span class="hljs-string">analysis</span> <span class="hljs-string">with</span> <span class="hljs-string">slither</span>
        <span class="hljs-attr">uses:</span> <span class="hljs-string">crytic/slither-action@v0.3.0</span>
        <span class="hljs-attr">id:</span> <span class="hljs-string">slither</span>
        <span class="hljs-attr">continue-on-error:</span> <span class="hljs-literal">true</span>
        <span class="hljs-attr">with:</span>
          <span class="hljs-attr">sarif:</span> <span class="hljs-string">results.sarif</span>
          <span class="hljs-attr">slither-args:</span> <span class="hljs-string">--filter-paths</span> <span class="hljs-string">node_modules</span> <span class="hljs-comment"># exclude other contracts (open zeppelin ones)</span>

      <span class="hljs-bullet">-</span> <span class="hljs-attr">name:</span> <span class="hljs-string">Upload</span> <span class="hljs-string">static</span> <span class="hljs-string">anaylisis</span> <span class="hljs-string">SARIF</span> <span class="hljs-string">output</span> <span class="hljs-string">file</span> <span class="hljs-comment"># it should also add relevant comments on PR</span>
        <span class="hljs-attr">uses:</span> <span class="hljs-string">github/codeql-action/upload-sarif@v2</span>
        <span class="hljs-attr">with:</span>
          <span class="hljs-attr">sarif_file:</span> <span class="hljs-string">${{</span> <span class="hljs-string">steps.slither.outputs.sarif</span> <span class="hljs-string">}}</span>
</code></pre>
<p>This pipeline will be triggered by a pull request and will run in parallel <code>build-n-test</code> and <code>code analysis</code> jobs. Slither’s result will be <a target="_blank" href="https://docs.github.com/en/code-security/code-scanning/integrating-with-code-scanning/uploading-a-sarif-file-to-github">exported</a> to the Security tab of the GitHub project and some comments will be added to the PR, for example:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697118854260/6cf49fc5-586c-4825-a621-d353fca602a7.gif" alt /></p>
<blockquote>
<p>It will work only when Code scanning feature is available (so the repo need to be public), in alternative you could use <a target="_blank" href="https://github.com/marketplace/actions/slither-action#example-workflow-markdown-report">Markdown report</a> workflow.</p>
</blockquote>
<p>In order to keep everything up to date, I also suggest creating a <code>dependabot.yml</code> file in <code>.github</code> folder with the following content:</p>
<pre><code class="lang-yaml"><span class="hljs-attr">version:</span> <span class="hljs-number">2</span>
<span class="hljs-attr">updates:</span>
  <span class="hljs-bullet">-</span> <span class="hljs-attr">package-ecosystem:</span> <span class="hljs-string">"npm"</span> 
    <span class="hljs-attr">directory:</span> <span class="hljs-string">"/"</span> <span class="hljs-comment"># Location of package manifests</span>
    <span class="hljs-attr">schedule:</span>
      <span class="hljs-attr">interval:</span> <span class="hljs-string">"daily"</span>
  <span class="hljs-bullet">-</span> <span class="hljs-attr">package-ecosystem:</span> <span class="hljs-string">"github-actions"</span>
    <span class="hljs-attr">directory:</span> <span class="hljs-string">"/"</span>
    <span class="hljs-attr">schedule:</span>
      <span class="hljs-attr">interval:</span> <span class="hljs-string">"daily"</span>
</code></pre>
<h2 id="heading-conclusion">Conclusion</h2>
<p>Setting up a Hardhat repository for Solidity smart contract development is a straightforward process that provides you with a robust environment to build, test, and deploy your contracts. By following the steps outlined in this blog post, you can quickly get started with your blockchain development journey and leverage the power of Hardhat along with other tools to develop secure and reliable smart contracts on EVM blockchains.</p>
<p>You can find the complete source code, or use it as a template, in the GitHub repository: <a target="_blank" href="https://github.com/olich97/solidity-hardhat-template">https://github.com/olich97/solidity-hardhat-template</a></p>
]]></content:encoded></item><item><title><![CDATA[Self-hosted analytics platform with Umami, Supabase, and Vercel]]></title><description><![CDATA[A complete guide to setup a self-hosted, privacy-focused alternative to Google Analytics platform with Umami, Supabase and Vercel for free.]]></description><link>https://hashnode.olich.me/self-hosted-analytics-platform-with-umami-supabase-and-vercel</link><guid isPermaLink="true">https://hashnode.olich.me/self-hosted-analytics-platform-with-umami-supabase-and-vercel</guid><category><![CDATA[Vercel]]></category><category><![CDATA[umami]]></category><category><![CDATA[supabase]]></category><category><![CDATA[privacy]]></category><category><![CDATA[analytics]]></category><dc:creator><![CDATA[Oleh Andrushko]]></dc:creator><pubDate>Fri, 12 May 2023 08:11:33 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1683878459304/0ed3879a-7b4d-4f6f-8722-62d73da3010d.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Are you tired of using website analytics platforms that compromise your users' privacy and deal with consent banners on your website? Do you want to build your own analytics platform that respects your users' privacy and gives you full control over your data? If so, then this guide is for you.</p>
<p>Traditional website analytics platforms often compromise user privacy by tracking user data without their consent or knowledge and storing it on servers the user has no control over. While there are privacy-respecting platforms available like <a target="_blank" href="https://plausible.io/">Plausible</a>, sometimes all you need is a basic user traffic analytics system that is easy to set up and use.</p>
<p>In this article, I will explore how to build your own privacy-focused website analytics platform using open-source tools like Umami, Supabase, and Vercel. With this guide, you can learn how to create a platform that respects user privacy, gives you full control over your data, and is accessible to everyone.</p>
<h2 id="heading-why-umami">Why Umami?</h2>
<p><a target="_blank" href="https://umami.is/">Umami</a> is a great choice for a privacy-focused website analytics platform because it is open-source and easy to set up. It provides all the basic features you need to track user traffic and customize your analytics.</p>
<p>One of the key benefits of using <a target="_blank" href="https://umami.is/">Umami</a> is that it is lightweight (<a target="_blank" href="https://bundlephobia.com/package/umami@0.1.0">package size</a>) and fast, which means it won't slow down your website. Additionally, <a target="_blank" href="https://umami.is/">Umami</a> is designed to be privacy-focused, which means it doesn't collect any personally identifiable information (PII) from your users. This is important because it helps you comply with data privacy regulations like GDPR and CCPA.</p>
<p><a target="_blank" href="https://umami.is/">Umami</a> provides a clean and intuitive user interface, making it easy to navigate and understand your analytics data. You can view your website traffic by day, week, or month, and see which pages are the most popular. You can also see where your traffic is coming from, including countries, cities, and referral sources. <a target="_blank" href="https://umami.is/">Umami</a> also provides real-time analytics, which means you can see your traffic data updated in real-time as users visit your site.</p>
<blockquote>
<p><a target="_blank" href="https://analytics.umami.is/share/LGazGOecbDtaIwDr/umami.is">Umami Dashboard Live Demo</a></p>
</blockquote>
<h2 id="heading-prerequisites">Prerequisites</h2>
<ul>
<li><p><a target="_blank" href="https://git-scm.com/">Git</a> version control system installed</p>
</li>
<li><p>A free subscription of <a target="_blank" href="https://vercel.com/signup">Vercel</a> account</p>
</li>
<li><p>A <a target="_blank" href="https://github.com/signup">GitHub</a> account</p>
</li>
<li><p>A <a target="_blank" href="https://supabase.com/">Supabase</a> account</p>
</li>
<li><p><a target="_blank" href="https://yarnpkg.com/">yarn</a> package manager: install with <code>npm install -g yarn</code></p>
</li>
</ul>
<h2 id="heading-step-1-setup-supabase">Step 1: Setup Supabase</h2>
<p><a target="_blank" href="https://supabase.com/">Supabase</a> is an open-source backend-as-a-service platform that makes it easy to build serverless applications. We will use Supabase to store our analytics data.</p>
<ol>
<li><p>Login to Supabase and create a new project in a target region:</p>
<p> <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1683878844095/9aa0d635-f369-41c4-8be1-0ed6afd87bbb.png" alt /></p>
</li>
<li><p>Go to <strong>Project Settings &gt; Database</strong> page of the newly created project.</p>
</li>
<li><p>Copy the connection string from Connection Polling Section:</p>
<p> <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1683878816624/601edb59-8237-4962-844b-fe012a7fe09e.png" alt /></p>
</li>
<li><p>Add <code>?pgbouncer=true</code> to the connection string you just copied and save it for later:</p>
</li>
</ol>
<pre><code class="lang-bash">postgres://postgres:[YOUR-PASSWORD]@db.blablabla.supabase.co:5432/postgres?pgbouncer=<span class="hljs-literal">true</span>
</code></pre>
<h2 id="heading-step-2-setup-umami">Step 2: Setup Umami</h2>
<ol>
<li><p>Go to <a target="_blank" href="https://github.com/umami-software/umami">Umami GitHub repository</a></p>
</li>
<li><p><a target="_blank" href="https://docs.github.com/en/get-started/quickstart/fork-a-repo">Fork</a> the repository</p>
</li>
<li><p>Clone the forked repository locally:</p>
</li>
</ol>
<pre><code class="lang-bash">git <span class="hljs-built_in">clone</span> https://github.com/olich97/umami.git
</code></pre>
<ol>
<li>Go to cloned folder and create a <code>.env</code> file in the root of the repository with the content (connection string to the supabase database created at Step 1):</li>
</ol>
<pre><code class="lang-bash">DATABASE_URL=postgres://postgres:[YOUR-PASSWORD]@db.blablabla.supabase.co:5432/postgres?pgbouncer=<span class="hljs-literal">true</span>
</code></pre>
<ol>
<li>Install dependencies:</li>
</ol>
<pre><code class="lang-bash">yarn install
</code></pre>
<ol>
<li>Build the project:</li>
</ol>
<pre><code class="lang-bash">yarn build
</code></pre>
<ol>
<li>Start the project:</li>
</ol>
<pre><code class="lang-bash">yarn start
</code></pre>
<ol>
<li>Follow the <a target="_blank" href="https://umami.is/docs/login">first Login</a> guide and change your default <strong>username</strong> and <strong>password</strong></li>
</ol>
<h2 id="heading-step-3-deploy-umami-on-vercel">Step 3: Deploy Umami on Vercel</h2>
<ol>
<li><p>Login to your <a target="_blank" href="https://vercel.com/">Vercel account</a></p>
</li>
<li><p>Add new project</p>
</li>
<li><p>Choose, cloned at Step 2, umami repository:</p>
<p> <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1683878766603/c0930606-548b-4f57-a3d9-b0cbc23a47f6.png" alt /></p>
</li>
<li><p>In the configuration page, under <strong>Environment Variables</strong>, specify your <code>DATABASE_URL</code> variable:</p>
<p> <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1683878752747/a0a7c61b-db92-4562-90f5-d30704a055b4.png" alt /></p>
</li>
<li><p>Press <strong>Deploy</strong></p>
<p> <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1683878719850/291c087f-ff0f-4bbf-afd7-26ea7e86f4ef.png" alt /></p>
</li>
</ol>
<blockquote>
<p>Vercel will assign a random domain to the deployed website. Follow <a target="_blank" href="https://vercel.com/docs/concepts/projects/domains/add-a-domain">the guide</a> to set up a custom domain.</p>
</blockquote>
<h2 id="heading-step-4-track-your-website">Step 4: Track your website</h2>
<ol>
<li><p>Login to the newly deployed Umami frontend</p>
</li>
<li><p>Create a new website: <strong>Settings</strong> -&gt; <strong>Websites</strong> -&gt; <strong>Add Website</strong>.</p>
<p> <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1683878699395/8bc5b322-8cb8-48ad-8e99-946cd3bd28d2.png" alt /></p>
</li>
<li><p>Get the tracking code: <strong>Settings</strong> -&gt; <strong>Websites</strong> -&gt; <strong>Your Website</strong> -&gt; <strong>Edit</strong> -&gt; <strong>Tracking Code</strong>.</p>
</li>
<li><p>Add the script to the website you want to track.</p>
</li>
<li><p>Enjoy!</p>
</li>
</ol>
<blockquote>
<p>For more about umami's capabilities read <a target="_blank" href="https://umami.is/docs/getting-started">the documentation</a>.</p>
</blockquote>
<h2 id="heading-conclusion">Conclusion</h2>
<p>By using Umami, Supabase, and Vercel, you can create a simple platform that respects user privacy and gives you complete control over your data. Open-source tools allow you to create an analytics platform that is accessible to everyone, regardless of budget or technical expertise. So, go ahead and give it a try!</p>
]]></content:encoded></item><item><title><![CDATA[Building a personal blog with Hugo and Vercel]]></title><description><![CDATA[Create and deploy your website with Hugo and Vercel]]></description><link>https://hashnode.olich.me/building-a-personal-blog-with-hugo-and-vercel</link><guid isPermaLink="true">https://hashnode.olich.me/building-a-personal-blog-with-hugo-and-vercel</guid><category><![CDATA[Hugo]]></category><category><![CDATA[Programming Blogs]]></category><category><![CDATA[blog]]></category><category><![CDATA[Vercel]]></category><category><![CDATA[Open Source]]></category><dc:creator><![CDATA[Oleh Andrushko]]></dc:creator><pubDate>Tue, 15 Feb 2022 10:11:11 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1644919763180/6lFGwTW-Q.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>It's been a while that I was wondering what could be a better way to improve my English communication and writing skills? <br />
What about starting a technical blog? <br />
I think in this way I could have fun, keep learning a lot of new stuff and improve my skills by actually practicing it, while building something of my own.<br /></p>
<p>So here I am writing my first blog post on how I actually build this blog 🙂 Let's goooooooooo 💪 <br /></p>
<blockquote>
<p>P.S: Sorry in advance for my rough English
In this post I'll try to explain how to set up a personal site or blog with <a target="_blank" href="https://gohugo.io/">Hugo</a> for <strong>free</strong>, by creating a <a target="_blank" href="https://en.wikipedia.org/wiki/Continuous_deployment">continuous deployment workflow</a> with <a target="_blank" href="https://github.com/features/actions">GitHub Actions</a>, deploying and hosting our site on <a target="_blank" href="https://vercel.com">Vercel</a> platform.</p>
</blockquote>
<h2 id="heading-why-hugo">Why Hugo?</h2>
<p><a target="_blank" href="https://gohugo.io/">Hugo</a> is a <strong>cross-platform</strong>, <strong>open-source</strong> static site generator written in <a target="_blank" href="https://golang.org/">Go</a>.</p>
<blockquote>
<p>A static site generator is a build tool that produces sites that have pages built beforehand and do not change often between user visits. 
Those pre-rendered pages are hosted where users can access them, a quite different approach unlike dynamic sites we're used to see where a page is built when a user requests it. <br />
Static sites involve a lot fewer moving parts and are easier to build, secure, and maintain. They are fast since pages are already pre-rendered and are served as is. Scaling static sites is easier compared to other sites with more complex infrastructures.
There are a lot of <a target="_blank" href="https://jamstack.org/generators">static sites generators</a>, but here I'll list some of advantages/features that I found quite useful and for which I chose Hugo.</p>
</blockquote>
<h3 id="heading-fast-andamp-flexible">Fast &amp; Flexible</h3>
<p>Hugo is pretty flexible. </p>
<p>I started this blog with simple open source theme (<a target="_blank" href="https://themes.gohugo.io/">there are a lot of them</a>), then changed it completely over time. 
Sometimes I want to do things (e.g custom tracking, comments integration) in my website that are out of the scope of a simple blog, and Hugo allows to create those things.</p>
<p>Another reason I chose Hugo is because it is fast. 
First, it has Go at the core, which is known to be a fast compiled language. Also <a target="_blank" href="https://golangbot.com/goroutines/">Go's Goroutines</a> are a perfect match for many of the tasks that Hugo performs on every build. Things are made to be as fast as possible. </p>
<p>For example, it comes with <a target="_blank" href="https://gohugo.io/getting-started/usage/#livereload">Live Reload</a> built in and every time changes are detected it takes milliseconds to see the result:</p>
<pre><code class="lang-shell">Change detected, rebuilding site.
2021-11-09 23:57:27.805 +0100
Source changed "path\\to\\post\\personal-website-with-hugo-and-vercel\\index.md": WRITE
Total in 17 ms
</code></pre>
<h3 id="heading-markdown">Markdown</h3>
<p>I think hugo is a right choice for a technical blog, especially if you are a developer and you’re willing to write in Markdown. Non-tech people might just refuse to use Markdown, and it’s perfectly understandable.</p>
<p>Also, you have to be prepared for a Git-centric workflow for publishing your posts (or you can use some cms solutions like <a target="_blank" href="https://strapi.io/integrations/hugo-cms">Strapi</a> or <a target="_blank" href="https://forestry.io/">Forestry</a>). Most people would disagree with me on using <code>git commit</code> and <code>git push</code> just for publishing a post, but I found it pretty cool.</p>
<p>For example, my process for writing a new post is:</p>
<ol>
<li>Write a post using Markdown</li>
<li>Commit my changes to a GitHub develop branch</li>
<li>Some glue technology deploys the changes on the Vercel preview server</li>
<li>Take some days to review the post in preview environment, make some improvements and then I am satisfied create a pull request towards the main branch</li>
<li>After self-accepting the PR, the same glue technology deploys all new stuff on the main server visible to all the world</li>
</ol>
<h2 id="heading-prerequisites">Prerequisites</h2>
<p>Here are some stuff that you're gonna need in order to make the magic happens:</p>
<ul>
<li><a target="_blank" href="https://git-scm.com/">Git</a> version control system installed</li>
<li><a target="_blank" href="https://gohugo.io/getting-started/installing/">Installation</a> of Hugo version <code>0.80.0</code> or higher</li>
<li>A free subscription of <a target="_blank" href="https://vercel.com/signup">Vercel</a> account</li>
<li>A <a target="_blank" href="https://github.com/signup">GitHub</a> account</li>
</ul>
<h2 id="heading-step-1-preparation">Step 1: Preparation</h2>
<p>First of all, we need to check a correctness of <strong>Git</strong> installation in your system, just by running the <code>git version</code> command on your terminal.
You should see something like this on your terminal, as an output:</p>
<pre><code class="lang-bash">git version 2.31.1.windows.1
</code></pre>
<p>If you don't have git, you can install it by downloading from <a target="_blank" href="https://git-scm.com/">the official website</a>.</p>
<blockquote>
<p>Make sure to connect Git with you GitHub account. Read more about GitHub and Git setup from <a target="_blank" href="https://docs.github.com/en/get-started/quickstart/set-up-git">here</a>.
Now we need to install <strong>Hugo</strong>:</p>
</blockquote>
<p>1) Go to <a target="_blank" href="https://gohugo.io/getting-started/installing/">Hugo installation section</a> of documentation and follow the steps for your operating system</p>
<p>... and create an empty <strong>repository</strong> in your GitHub account:</p>
<p>2) Create a new repository with the name you like (for this tutorial I'll be using <code>hugo-example-blog</code>)</p>
<blockquote>
<p>You can create a new repository on GitHub using a web browser or GitHub CLI: check out <a target="_blank" href="https://docs.github.com/en/get-started/quickstart/create-a-repo">the documentation</a>.
3) <a target="_blank" href="https://docs.github.com/en/repositories/creating-and-managing-repositories/cloning-a-repository">Clone</a> the newly created repository on your computer.</p>
</blockquote>
<p>Now, you should be ready to go! 🎉</p>
<h2 id="heading-step-2-create-your-website">Step 2: Create your website</h2>
<h3 id="heading-creating-a-new-site">Creating a new site</h3>
<p>Creating a website is pretty easy, just fire the command <code>hugo new site hugo-example-blog</code> in a directory of your choice:</p>
<pre><code class="lang-shell">$ hugo new site hugo-example-blog
Congratulations! Your new Hugo site is created in \path\to\hugo-example-blog.
</code></pre>
<p>Lets add a newly created project <a target="_blank" href="https://docs.github.com/en/github/importing-your-projects-to-github/importing-source-code-to-github/adding-an-existing-project-to-github-using-the-command-line">to our GitHub repository</a>:</p>
<ol>
<li>In the command line, navigate to the newly created directory (/path/to/hugo-example-blog)</li>
<li>Initialize the local directory as a Git repository:<pre><code class="lang-shell">$ git init -b main
Initialized empty Git repository in /path/to/hugo-example-blog/.git/
</code></pre>
</li>
<li>Stage and commit all the files in your project:<pre><code class="lang-shell">$ git add .
$ git commit -m "Initial commit"
</code></pre>
</li>
<li>Push all files to GitHub repository:<pre><code class="lang-shell"># add remote for the repo
$ git remote add origin &lt;REMOTE_URL&gt;
# sets the new remote
$ git remote -v
# for pulling unrelated remote history if there are conflicts
$ git pull origin main --allow-unrelated-histories
# push the changes to remote repository
$ git push origin main
</code></pre>
<h3 id="heading-adding-the-theme">Adding the theme</h3>
</li>
</ol>
<p>We need to choose a theme for our website. 
You can do it by choosing the one you like from the <a target="_blank" href="https://themes.gohugo.io/">official hugo site</a>.
Almost all the themes are open source so you can modify them freely.</p>
<p>For this example I've chosen <a target="_blank" href="https://github.com/nanxiaobei/hugo-paper">a paper theme</a>.</p>
<p>Now we need to "install" our theme on newly created website.</p>
<p>There are two ways to do that: </p>
<ul>
<li>Installing a theme as a <a target="_blank" href="https://git-scm.com/book/en/v2/Git-Tools-Submodules">Git submodule</a>: particularly useful when you want to keep your theme updated with new features:<pre><code class="lang-shell"># navigate to the themes directory inside the site repository
$ cd themes
# add the theme as a submodule
$ git submodule add &lt;THEME_REMOTE_URL&gt;
</code></pre>
</li>
<li>Manually installing a theme: it's recommended when your are aware of making a lot of custom changes on the theme<pre><code class="lang-shell"># navigate to the themes directory inside the site repository
$ cd themes
# clone the theme
$ git clone &lt;THEME_REMOTE_URL&gt; &lt;THEME_NAME&gt;
</code></pre>
If you do not have Git installed:</li>
<li>Download the .zip file from GitHub</li>
<li>Unzip it to your site's themes directory</li>
<li>Rename the directory to the theme name</li>
</ul>
<p>Here the commands for installing as a submodule our paper theme:</p>
<pre><code class="lang-shell">$ cd themes
$ git submodule add https://github.com/nanxiaobei/hugo-paper.git
</code></pre>
<h3 id="heading-configuring-and-testing-the-website">Configuring and testing the website</h3>
<ol>
<li>Copy a content of <code>themes/hugo-paper/exampleSite</code> folder to the root website folder <code>hugo-example-blog</code>.</li>
<li>Modify <code>config.toml</code> file based on your needs</li>
<li>Start a website by firing <code>hugo server</code> on your terminal inside site folder </li>
<li>Enjoy the result:
<code>http://localhost:1313</code></li>
</ol>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1644918942565/QU_DsxvoG.gif" alt="Paper Hugo Theme Result" /></p>
<blockquote>
<p>You can add a new pages or posts anytime by creating files inside <code>hugo-example-blog\content</code> folder</p>
<h2 id="heading-step-3-deploy-your-website">Step 3: Deploy your website</h2>
</blockquote>
<p>In this section we will setup a simple <a target="_blank" href="https://github.com/features/actions">GitHub Actions</a> workflow with <code>production</code> (main) and <code>develop</code> (develop) branches that will build and deploy our website on Vercel.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1644918903226/yegvJRpqR.png" alt="Workflow Diagram (build with draw.io)" /></p>
<blockquote>
<p><a target="_blank" href="https://vercel.com/docs">Vercel</a> is a platform for frontend frameworks and static sites, built to integrate with your headless content, commerce, or database.  It is easy to develop, preview, and ship delightful user experiences, where performance is the default.</p>
</blockquote>
<h3 id="heading-configuring-vercel">Configuring Vercel</h3>
<p>In order to deploy our Hugo site from GitHub we need in some way give to GitHub Actions access to our Vercel account.</p>
<p>We're gonna need three essential values from Vercel:</p>
<ul>
<li><a target="_blank" href="https://vercel.com/account/tokens">Vercel Account Token</a></li>
<li>Organization ID</li>
<li>Project ID</li>
</ul>
<p>For generation a new <strong>Account Token</strong> just visit https://vercel.com/account/tokens and click on <code>create</code>, give it a name (in my case 'hugo-example-site') and save it for later use.
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1644919459806/2B6e3V3pG.gif" alt="Vercel Token Creation" />
To obtain a <strong>Organization ID</strong> and <strong>Project ID</strong> a new project needs to be created. 
For this we’ll use the <strong>Vercel CLI</strong>. 
Use either the <code>npm</code> or <code>yarn</code> package manager to install the <strong>Vercel CLI</strong> in your local development environment:</p>
<pre><code class="lang-shell">$ npm i -g vercel
</code></pre>
<h3 id="heading-deploying-to-vercel">Deploying to vercel</h3>
<p>Now we create a new project on our vercel account:</p>
<ol>
<li>Login on vercel locally by firing <code>vercel</code> command on the root of our project and following instructions: </li>
</ol>
<pre><code><span class="hljs-string">$</span> <span class="hljs-string">vercel</span>
<span class="hljs-string">Vercel</span> <span class="hljs-string">CLI</span> <span class="hljs-number">23.1</span><span class="hljs-number">.2</span>
<span class="hljs-attr">No existing credentials found. Please log in:</span>
<span class="hljs-string">Log</span> <span class="hljs-string">in</span> <span class="hljs-string">to</span> <span class="hljs-string">Vercel</span> <span class="hljs-string">github</span>
<span class="hljs-string">Success!</span> <span class="hljs-string">GitHub</span> <span class="hljs-string">authentication</span> <span class="hljs-string">complete</span> <span class="hljs-string">for</span> <span class="hljs-string">example@email.com</span>
</code></pre><ol>
<li>Now that you’re logged in run the <code>vercel</code> command again to configure and deploy our site to Vercel as a new project:</li>
</ol>
<pre><code>$ vercel
Vercel CLI <span class="hljs-number">23.1</span><span class="hljs-number">.2</span>
? Set up and deploy “path\to\hugo<span class="hljs-operator">-</span>example<span class="hljs-operator">-</span>blog”? [Y<span class="hljs-operator">/</span>n] y
? Which scope do you want to deploy to? olich97
? Link to existing project? [y<span class="hljs-operator">/</span>N] n
? What’s your project’s name? hugo<span class="hljs-operator">-</span>example<span class="hljs-operator">-</span>blog
? In which directory <span class="hljs-keyword">is</span> your code located? ./
<span class="hljs-operator">&gt;</span> Upload [<span class="hljs-operator">=</span><span class="hljs-operator">=</span><span class="hljs-operator">=</span><span class="hljs-operator">=</span><span class="hljs-operator">=</span><span class="hljs-operator">=</span><span class="hljs-operator">=</span><span class="hljs-operator">=</span><span class="hljs-operator">=</span><span class="hljs-operator">=</span><span class="hljs-operator">=</span><span class="hljs-operator">=</span><span class="hljs-operator">=</span><span class="hljs-operator">=</span><span class="hljs-operator">=</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span><span class="hljs-operator">-</span>] <span class="hljs-number">72</span><span class="hljs-operator">%</span> <span class="hljs-number">1</span>.5sAuto-detected Project Settings (Hugo):
<span class="hljs-operator">-</span> Build Command: `npm run build` or `hugo <span class="hljs-operator">-</span>D <span class="hljs-operator">-</span><span class="hljs-operator">-</span>gc`
<span class="hljs-operator">-</span> Output Directory: `<span class="hljs-keyword">public</span>` or `publishDir` <span class="hljs-keyword">from</span> the `config` file
<span class="hljs-operator">-</span> Development Command: hugo server <span class="hljs-operator">-</span>D <span class="hljs-operator">-</span>w <span class="hljs-operator">-</span>p $PORT
? Want to <span class="hljs-keyword">override</span> the settings? [y<span class="hljs-operator">/</span>N] n
🔗  Linked to olich97<span class="hljs-operator">/</span>hugo<span class="hljs-operator">-</span>example<span class="hljs-operator">-</span>blog (created .vercel and added it to .gitignore)
🔍  Inspect: https:<span class="hljs-comment">//vercel.com/olich97/hugo-example-blog/ESNvEjydmACDsiibPJt2YCwrYyS9</span>
</code></pre><p>Once deployed search the newly generated local <code>.vercel</code> directory for the <code>project.json</code> file, it’ll contain your new organisation and project IDs:</p>
<pre><code class="lang-json">{<span class="hljs-attr">"orgId"</span>:<span class="hljs-string">"your-secret-organization-id"</span>,<span class="hljs-attr">"projectId"</span>:<span class="hljs-string">"prj_your-secret-project-id"</span>}
</code></pre>
<p>We need to configure our GitHub site repository with newly created secrets.</p>
<p>Go to GitHub site <code>repository &gt; Settings &gt; Secrets &gt; Actions &gt; New repository secret</code>.</p>
<p>Add all three vercel values created:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1644919655062/K9xRS_lky.png" alt="GitHub Secrets" /></p>
<p>Since GitHub Actions will be responsible for building Hugo site our new project settings need be updated so that Vercel doesn’t attempt to <strong>build</strong> the site again once GitHub Actions has pushed the content to Vercel.</p>
<p>In the Vercel dashboard navigate to the <strong>Settings</strong> of new project. Click the switch to override the BUILD COMMAND. Leave the input <strong>field empty</strong>:
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1644919544773/wjOWDO6vI.gif" alt="Vercel Build Settings" /></p>
<h3 id="heading-creating-the-github-actions-workflow-files">Creating the GitHub Actions Workflow Files</h3>
<p>First of all we need to create a <code>develop</code> branch by running git checkout on the root of our hugo website:</p>
<pre><code class="lang-shell">$ git checkout -b develop
Switched to a new branch 'develop'
</code></pre>
<p>In a  <code>.github/workflows</code> directory at the root of our project create two new files:</p>
<ul>
<li><code>deploy-preview.yaml</code> with content: <strong><a target="_blank" href="https://gist.github.com/olich97/a1fb2750a3ca07015ddb8e01685b7b7c">preview gist</a></strong></li>
<li><code>deploy-production.yaml</code> with content: <strong><a target="_blank" href="https://gist.github.com/olich97/5ba50f83f008d483ab8215ef09b84d10">prod gist</a></strong></li>
</ul>
<p>Let push our changes on GitHub and see what happens.</p>
<pre><code class="lang-shell">$ git add .
$ git commit -m "ci: add GitHub Actions workflow"
$ git push --set-upstream origin develop
</code></pre>
<p>If we take a look on our GitHub project page we can see that a new environment was created: 
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1645098287929/PG8E_fZR6.png" alt="GitHub Environments" /></p>
<p>Now we can view our preview deployment just by clicking on <strong>View deployment</strong> link inside GitHub Project deployments page:
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1644918699379/P14arf2q7.png" alt="GitHub View Deployment" /></p>
<p>As you can see vercel generate a temporary url for our deployment so we can check it:
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1644918737826/PsOREw9BA.gif" alt="Vercel Deploy Result" /></p>
<p>Now we just have to release everything in production and it is pretty easy: just create and merge a new pull request against <code>main</code> branch.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1644918751078/J5F-tMuzb.gif" alt="GitHub Pull Request" /></p>
<p>GitHub Actions will build  <code>main</code> branch automatically and deploy to vercel production.
Now it could be navigated from free vercel domain assigned to our website: https://hugo-example-blog.vercel.app. </p>
<p>The domain can be customized on Vercel Project Settings page: </p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1644919694800/0kc6Thuoy.gif" alt="Vercel Domain Configuration" /></p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>In this article we've created a new static website from zero and configured an automatic deployment workflows for two environments:</p>
<ul>
<li><code>preview</code>: for our testing purposes</li>
<li><code>production</code>: for final users</li>
</ul>
<p>Another advantage of using a static website (in this case build with Hugo) is pages load time (that is pretty fast in this case). 
You can check performance of our newly created website on <a target="_blank" href="https://gtmetrix.com/">GTmetrix</a> and see it by yourself.</p>
<p>Complete Source Code: https://github.com/olich97/hugo-example-blog</p>
]]></content:encoded></item></channel></rss>