CI/CD Pipeline

Setting up continuous integration and deployment for Rust blockchain projects.

Advanced⏱️ 50 min📚 Prerequisites: 1

CI/CD Pipeline

CI/CD (Continuous Integration/Continuous Deployment) automates testing, building, and deploying Rust blockchain applications.

CI/CD Benefits

  • Automated testing: Run tests on every commit
  • Consistent builds: Same process every time
  • Fast feedback: Know immediately if code breaks
  • Automated deployment: Deploy to production automatically
  • Rollback capability: Quick revert if issues

GitHub Actions

Basic Workflow

YAML
name: CI

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

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: actions-rs/toolchain@v1
        with:
          toolchain: stable
      - name: Run tests
        run: cargo test --verbose
      - name: Run clippy
        run: cargo clippy -- -D warnings
      - name: Check format
        run: cargo fmt -- --check

  build:
    needs: test
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: actions-rs/toolchain@v1
      - name: Build release
        run: cargo build --release
      - name: Upload artifact
        uses: actions/upload-artifact@v3
        with:
          name: blockchain-node
          path: target/release/blockchain-node

GitLab CI

YAML
stages:
  - test
  - build
  - deploy

test:
  stage: test
  image: rust:1.75
  script:
    - cargo test --verbose
    - cargo clippy -- -D warnings
    - cargo fmt -- --check

build:
  stage: build
  image: rust:1.75
  script:
    - cargo build --release
  artifacts:
    paths:
      - target/release/blockchain-node

deploy:
  stage: deploy
  image: docker:latest
  script:
    - docker build -t blockchain-node .
    - docker push registry.gitlab.com/your-project/blockchain-node
  only:
    - main

Deployment Pipeline

Stages

  1. Test: Run all tests
  2. Lint: Check code quality (clippy, fmt)
  3. Security: Audit dependencies
  4. Build: Create release binary
  5. Docker: Build container image
  6. Deploy: Push to registry and deploy

Example Workflow

YAML
name: Deploy

on:
  push:
    branches: [ main ]
    tags:
      - 'v*'

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Build Docker image
        run: docker build -t blockchain-node .
      - name: Push to registry
        run: |
          echo ${{ secrets.DOCKER_PASSWORD }} | docker login -u ${{ secrets.DOCKER_USERNAME }} --password-stdin
          docker push blockchain-node:latest
      - name: Deploy to production
        run: |
          # Deploy to your cloud platform
          kubectl set image deployment/blockchain-node blockchain-node=blockchain-node:latest

Testing in CI

YAML
- name: Run tests
  run: cargo test --all-features --verbose

- name: Test with coverage
  run: cargo test --coverage

- name: Integration tests
  run: cargo test --test integration

Security Scanning

YAML
- name: Security audit
  run: cargo audit

- name: Dependency check
  run: cargo deny check

Best Practices

  • Fast feedback: Run quick tests first
  • Parallel jobs: Run tests in parallel
  • Cache dependencies: Speed up builds
  • Matrix builds: Test multiple Rust versions
  • Conditional deployment: Only deploy on main branch

Code Examples

GitHub Actions Workflow

Complete CI/CD workflow

RUST
fn main() {
    println!("CI/CD Pipeline stages:");
    println!("1. Checkout code");
    println!("2. Install Rust toolchain");
    println!("3. Run tests");
    println!("4. Run linter (clippy)");
    println!("5. Check formatting");
    println!("6. Build release binary");
    println!("7. Build Docker image");
    println!("8. Deploy to production");
}

Explanation:

CI/CD pipelines automate the entire development workflow. They ensure code quality, run tests, and deploy automatically when all checks pass.

Test Automation

Automated testing in CI

RUST
#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn test_blockchain_validation() {
        assert!(true);
    }
    #[test]
    fn test_transaction_processing() {
        assert!(true);
    }
}
fn main() {
    println!("CI/CD runs:");
    println!("- Unit tests: cargo test");
    println!("- Integration tests: cargo test --test integration");
    println!("- Linting: cargo clippy");
    println!("- Formatting: cargo fmt --check");
    println!("- Security: cargo audit");
}

Explanation:

Automated testing in CI ensures code quality before deployment. All tests must pass before code is merged or deployed to production.

Exercises

CI Workflow

Create a basic GitHub Actions workflow!

Medium

Starter Code:

RUST
# .github/workflows/ci.yml
# Create a workflow that:
# - Runs on push to main
# - Checks out code
# - Installs Rust
# - Runs cargo test