|
1 | 1 | # 🔄 CI/CD |
| 2 | + |
| 3 | +**Continuous Integration and Continuous Deployment** — automate builds, tests, and deployments so every push can ship safely. |
| 4 | + |
| 5 | +CI/CD is essential for production Web3 apps: run tests on every commit, deploy contracts and frontends from a single pipeline, and avoid manual mistakes. |
| 6 | + |
| 7 | +--- |
| 8 | + |
| 9 | +## 🎯 Why CI/CD Matters for Web3 |
| 10 | + |
| 11 | +- **Catch bugs before mainnet** — Run tests and lint on every PR |
| 12 | +- **Reproducible deployments** — Same steps every time, no "I forgot to run X" |
| 13 | +- **Audit trail** — Who deployed what, when, from which commit |
| 14 | +- **Security** — Run secret scanning, dependency checks, and contract verification in the pipeline |
| 15 | + |
| 16 | +--- |
| 17 | + |
| 18 | +## 📚 Key Concepts |
| 19 | + |
| 20 | +| Term | Meaning | |
| 21 | +|------|--------| |
| 22 | +| **CI** | Automatically build and test code on every push/PR | |
| 23 | +| **CD** | Automatically deploy (or prepare artifacts) after successful CI | |
| 24 | +| **Pipeline** | Sequence of steps (install → test → build → deploy) | |
| 25 | +| **Workflow** | The config file that defines your pipeline | |
| 26 | + |
| 27 | +--- |
| 28 | + |
| 29 | +## 🛠️ Popular CI/CD Platforms |
| 30 | + |
| 31 | +- [**GitHub Actions**](https://docs.github.com/en/actions) — Native to GitHub, free for public repos, generous free tier for private |
| 32 | +- [**GitLab CI/CD**](https://docs.gitlab.com/ee/ci/) — Built into GitLab, powerful and flexible |
| 33 | +- [**CircleCI**](https://circleci.com/docs/) — Fast, good for complex workflows |
| 34 | +- [**Vercel**](https://vercel.com/docs/concepts/deployments/overview) / [**Netlify**](https://docs.netlify.com/site-deploys/overview/) — Auto-deploy from Git (CD-focused) |
| 35 | +- [**Railway**](https://docs.railway.app/deploy/deployments) / [**Render**](https://render.com/docs/deploys) — Git-based deploys with env and Docker support |
| 36 | + |
| 37 | +--- |
| 38 | + |
| 39 | +## 🚀 GitHub Actions: Quick Start |
| 40 | + |
| 41 | +Create `.github/workflows/ci.yml` in your repo: |
| 42 | + |
| 43 | +```yaml |
| 44 | +# .github/workflows/ci.yml |
| 45 | +name: CI |
| 46 | + |
| 47 | +on: |
| 48 | + push: |
| 49 | + branches: [main, develop] |
| 50 | + pull_request: |
| 51 | + branches: [main] |
| 52 | + |
| 53 | +jobs: |
| 54 | + test: |
| 55 | + runs-on: ubuntu-latest |
| 56 | + steps: |
| 57 | + - uses: actions/checkout@v4 |
| 58 | + |
| 59 | + - name: Setup Node.js |
| 60 | + uses: actions/setup-node@v4 |
| 61 | + with: |
| 62 | + node-version: '20' |
| 63 | + cache: 'npm' |
| 64 | + |
| 65 | + - run: npm ci |
| 66 | + - run: npm run lint |
| 67 | + - run: npm test |
| 68 | +``` |
| 69 | +
|
| 70 | +This runs `npm ci`, `npm run lint`, and `npm test` on every push and PR to `main`/`develop`. |
| 71 | + |
| 72 | +--- |
| 73 | + |
| 74 | +## 📦 Build and Test (Node / Hardhat) |
| 75 | + |
| 76 | +Example for a typical Web3 monorepo (app + contracts): |
| 77 | + |
| 78 | +```yaml |
| 79 | +# .github/workflows/build-and-test.yml |
| 80 | +name: Build and Test |
| 81 | +
|
| 82 | +on: |
| 83 | + push: |
| 84 | + branches: [main] |
| 85 | + pull_request: |
| 86 | + branches: [main] |
| 87 | +
|
| 88 | +jobs: |
| 89 | + test-contracts: |
| 90 | + name: Test contracts |
| 91 | + runs-on: ubuntu-latest |
| 92 | + steps: |
| 93 | + - uses: actions/checkout@v4 |
| 94 | +
|
| 95 | + - name: Setup Node |
| 96 | + uses: actions/setup-node@v4 |
| 97 | + with: |
| 98 | + node-version: '20' |
| 99 | + cache: 'npm' |
| 100 | +
|
| 101 | + - run: npm ci |
| 102 | + - name: Compile contracts |
| 103 | + run: npx hardhat compile |
| 104 | + - name: Run contract tests |
| 105 | + run: npx hardhat test |
| 106 | +
|
| 107 | + test-app: |
| 108 | + name: Test app |
| 109 | + runs-on: ubuntu-latest |
| 110 | + steps: |
| 111 | + - uses: actions/checkout@v4 |
| 112 | +
|
| 113 | + - name: Setup Node |
| 114 | + uses: actions/setup-node@v4 |
| 115 | + with: |
| 116 | + node-version: '20' |
| 117 | + cache: 'npm' |
| 118 | +
|
| 119 | + - run: npm ci |
| 120 | + - run: npm run build |
| 121 | +``` |
| 122 | + |
| 123 | +--- |
| 124 | + |
| 125 | +## 🔐 Using Secrets in CI/CD |
| 126 | + |
| 127 | +Never commit API keys or private keys. Use your platform’s **secrets** and inject them as environment variables. |
| 128 | + |
| 129 | +**GitHub Actions:** [Encrypted secrets](https://docs.github.com/en/actions/security-guides/encrypted-secrets) |
| 130 | + |
| 131 | +1. Repo → **Settings** → **Secrets and variables** → **Actions** |
| 132 | +2. Add e.g. `PRIVATE_KEY`, `ALCHEMY_API_KEY`, `VERCEL_TOKEN` |
| 133 | + |
| 134 | +```yaml |
| 135 | +# Use secrets in a job |
| 136 | +jobs: |
| 137 | + deploy: |
| 138 | + runs-on: ubuntu-latest |
| 139 | + env: |
| 140 | + PRIVATE_KEY: ${{ secrets.DEPLOYER_PRIVATE_KEY }} |
| 141 | + RPC_URL: ${{ secrets.SEPOLIA_RPC_URL }} |
| 142 | + steps: |
| 143 | + - uses: actions/checkout@v4 |
| 144 | + - run: npm ci |
| 145 | + - run: npx hardhat run scripts/deploy.js --network sepolia |
| 146 | +``` |
| 147 | + |
| 148 | +**Best practice:** Use a **deployer wallet** with minimal funds; never use a wallet holding real user assets. |
| 149 | + |
| 150 | +--- |
| 151 | + |
| 152 | +## 🚢 Deploy from CI (CD) |
| 153 | + |
| 154 | +### Deploy frontend to Vercel |
| 155 | + |
| 156 | +[Vercel GitHub integration](https://vercel.com/docs/concepts/git/vercel-for-github) deploys automatically. For custom control, use the [Vercel CLI](https://vercel.com/docs/cli) in Actions: |
| 157 | + |
| 158 | +```yaml |
| 159 | +# .github/workflows/deploy-vercel.yml |
| 160 | +name: Deploy to Vercel |
| 161 | +
|
| 162 | +on: |
| 163 | + push: |
| 164 | + branches: [main] |
| 165 | +
|
| 166 | +jobs: |
| 167 | + deploy: |
| 168 | + runs-on: ubuntu-latest |
| 169 | + steps: |
| 170 | + - uses: actions/checkout@v4 |
| 171 | +
|
| 172 | + - name: Setup Node |
| 173 | + uses: actions/setup-node@v4 |
| 174 | + with: |
| 175 | + node-version: '20' |
| 176 | + cache: 'npm' |
| 177 | +
|
| 178 | + - run: npm ci |
| 179 | + - run: npm run build |
| 180 | +
|
| 181 | + - name: Deploy to Vercel |
| 182 | + uses: amondnet/vercel-action@v25 |
| 183 | + with: |
| 184 | + vercel-token: ${{ secrets.VERCEL_TOKEN }} |
| 185 | + vercel-org-id: ${{ secrets.VERCEL_ORG_ID }} |
| 186 | + vercel-project-id: ${{ secrets.VERCEL_PROJECT_ID }} |
| 187 | + working-directory: ./ |
| 188 | +``` |
| 189 | + |
| 190 | +### Deploy with Docker (e.g. Railway) |
| 191 | + |
| 192 | +```yaml |
| 193 | +# .github/workflows/deploy-railway.yml |
| 194 | +name: Deploy to Railway |
| 195 | +
|
| 196 | +on: |
| 197 | + push: |
| 198 | + branches: [main] |
| 199 | +
|
| 200 | +jobs: |
| 201 | + deploy: |
| 202 | + runs-on: ubuntu-latest |
| 203 | + steps: |
| 204 | + - uses: actions/checkout@v4 |
| 205 | +
|
| 206 | + - name: Install Railway CLI |
| 207 | + run: npm i -g @railway/cli |
| 208 | +
|
| 209 | + - name: Deploy |
| 210 | + env: |
| 211 | + RAILWAY_TOKEN: ${{ secrets.RAILWAY_TOKEN }} |
| 212 | + run: railway up --service my-app |
| 213 | +``` |
| 214 | + |
| 215 | +See [Railway’s deployment docs](https://docs.railway.app/deploy/deployments) for tokens and service names. |
| 216 | + |
| 217 | +--- |
| 218 | + |
| 219 | +## ✅ Good Practices |
| 220 | + |
| 221 | +- **Run tests and lint on every PR** — Block merge if they fail |
| 222 | +- **Use matrix builds** for multiple Node versions if you support them |
| 223 | +- **Cache dependencies** — `cache: 'npm'` or [actions/cache](https://github.com/actions/cache) to speed up runs |
| 224 | +- **Pin actions by tag** — Prefer `@v4` over `@main` |
| 225 | +- **Store all secrets in the platform** — Never log or commit them |
| 226 | +- **Document your workflows** — Brief comment in the YAML or a CONTRIBUTING.md |
| 227 | + |
| 228 | +--- |
| 229 | + |
| 230 | +## 🔗 Further Reading |
| 231 | + |
| 232 | +- [GitHub Actions documentation](https://docs.github.com/en/actions) |
| 233 | +- [Hardhat CI documentation](https://hardhat.org/hardhat-runner/docs/guides/ci) |
| 234 | +- [Vercel deployments](https://vercel.com/docs/concepts/deployments/overview) |
| 235 | +- [Environment Variables](/basics/environment-variables) — Keep secrets out of the repo |
0 commit comments