This Repo is a working example of a ci/cd pipeline for Dapps using motoko built on the Internet Computer
base64 -i identity.pem
For example DEVELOPEMNT, STAGING and PROD would be the name of your repository secrets and the workflow would access them using secrets.DEVELOPEMNT, secrets.STAGING and secrets.PROD
This repository uses GitHub Actions to automate testing and building of the project. The CI workflow is triggered on pull requests to the main
, master
, and development
branches.
The GitHub Actions workflow is defined in the .github/workflows/ci.yml
file. Below is an explanation of the workflow configuration:
The workflow is triggered on pull_request
events targeting the following branches:
main
master
development
The workflow defines a single job named test
that uses a matrix strategy to run tests in parallel with different versions of Node.js, MOPS, and the Motoko compiler.
The job strategy specifies the following:
- Max Parallel: 2 (Runs a maximum of 2 jobs in parallel)
- Matrix:
moc-version
: [latest]mops-version
: [ic-mops@latest]node-version
: [20]
The job runs on an ubuntu-latest
runner and includes the following steps:
-
Checkout Repository:
- uses: actions/checkout@v4
-
Setup Node.js:
- uses: actions/setup-node@v4 with: node-version: ${{ matrix.node-version }} cache: 'npm'
-
Install DFX:
- name: Install dfx uses: dfinity/setup-dfx@main
-
Confirm DFX Installation:
- name: Confirm successful installation run: dfx --version
-
Install DFX Cache:
- name: Installing dfx cache run: dfx cache install
-
Cache MOPS Packages:
- name: Cache mops packages uses: actions/cache@v4 with: key: mops-packages-${{ hashFiles('mops.toml') }} restore-keys: | mops-packages-${{ hashFiles('mops.toml') }} mops-packages- path: | ~/.cache/mops
-
Install NPM Packages for CLI (Conditionally):
- name: Install npm packages if: ${{ matrix.mops-version == './cli' }} run: cd cli && npm install
-
Build Local CLI (Conditionally):
- name: Build local cli if: ${{ matrix.mops-version == './cli' }} run: cd cli && npm run prepare
-
Install MOPS:
- name: Install mops run: npm i -g ${{ matrix.mops-version }}
-
Install MOPS Packages:
- name: Install mops packages run: mops install
-
Select Moc Version:
- name: Select moc version run: mops toolchain use moc ${{ matrix.moc-version }}
-
Run Tests:
- name: Run tests run: mops test
-
Add Workflow File: Save the provided YAML content into a file named
ci.yml
in the.github/workflows/
directory of your repository. -
Push to Repository: Commit and push the workflow file to your GitHub repository.
-
Create Pull Requests: Create pull requests targeting the
main
,master
, ordevelopment
branches to trigger the CI workflow.
This repository uses GitHub Actions to automate the deployment of the project. The CD workflow is triggered on closed pull requests to the main
, master
, staging
, and development
branches.
The GitHub Actions workflow is defined in the .github/workflows/cd.yml
file. Below is an explanation of the workflow configuration:
The workflow is triggered on pull_request
events with the closed
type, targeting the following branches:
main
master
staging
development
The workflow defines a single job named check-branch
that runs if the pull request is merged. It includes steps for setting up the environment, caching packages, installing dependencies, and deploying to the appropriate network.
The job runs on an ubuntu-latest
runner and includes the following steps:
-
Checkout Repository:
- uses: actions/checkout@v4
-
Setup Node.js:
- uses: actions/setup-node@v4 with: node-version: 20 cache: 'npm'
-
Cache MOPS Packages:
- name: Cache mops packages uses: actions/cache@v4 with: key: mops-packages-${{ hashFiles('mops.toml') }} restore-keys: | mops-packages-${{ hashFiles('mops.toml') }} mops-packages- path: | ~/.cache/mops
-
Install DFX:
- name: Install dfx uses: dfinity/setup-dfx@main
-
Confirm DFX Installation:
- name: Confirm successful installation run: dfx --version
-
Install DFX Cache:
- name: Installing dfx cache run: dfx cache install
-
Install MOPS:
- name: Install mops run: npm i -g ic-mops@latest
-
Install MOPS Packages:
- name: Install mops packages run: mops install
-
Deploy to Development:
- name: Deploy to development if: github.base_ref == 'development' run: | echo ${{ secrets.DEVELOPMENT }} | base64 --decode > development.pem dfx identity import --storage-mode=plaintext development development.pem dfx identity use development dfx deploy --network development -y
-
Deploy to Staging:
- name: Deploy to staging if: github.base_ref == 'staging' run: | if [[ "${{ github.head_ref }}" != "development" ]]; then echo "Only changes from the development branch can be merged into staging." exit 1 else echo ${{ secrets.STAGING }} | base64 --decode > staging.pem dfx identity import --storage-mode=plaintext staging staging.pem dfx identity use staging dfx deploy --network staging -y fi
-
Check Source for Master:
- name: Check source for master if: github.base_ref == 'master' run: | if [[ "${{ github.head_ref }}" != "staging" ]]; then echo "Only changes from the staging branch can be merged into master." exit 1 else echo ${{ secrets.PROD }} | base64 --decode > prod.pem dfx identity import --storage-mode=plaintext prod prod.pem dfx identity use prod dfx deploy --network ic -y fi
By following these steps, you'll set up a robust CI pipeline that automatically tests your code using the specified matrix of Node.js, MOPS, and Motoko versions aswell as a robust CD pipeline that automatically deploys your code to different environments, ensuring higher reliability and consistency in your deployment process.