In modern software development, continuous delivery (CD) is essential for maintaining rapid deployment cycles and ensuring reliable releases. Integrating Axum, a powerful web framework for Rust, with GitHub Actions streamlines this process, enabling automated testing and deployment. This tutorial guides you through setting up a seamless CI/CD pipeline for your Axum application using GitHub Actions.

Prerequisites

  • Basic knowledge of Rust and Axum framework
  • GitHub account with a repository containing your Axum project
  • Docker installed locally for testing containerization
  • Access to a server or cloud platform for deployment (e.g., AWS, DigitalOcean)

Step 1: Prepare Your Axum Application

Ensure your Axum project is structured correctly with a clear entry point. Your Cargo.toml should include necessary dependencies, and your main server code should be ready for deployment. Test your application locally to confirm it runs without errors.

Step 2: Create a Dockerfile

Containerizing your application simplifies deployment and ensures consistency across environments. Below is a sample Dockerfile for an Axum app:

FROM rust:latest

WORKDIR /app

COPY . .

RUN cargo build --release

EXPOSE 8080

CMD ["./target/release/your_app_name"]

Step 3: Set Up GitHub Actions Workflow

Create a new workflow file in your repository at .github/workflows/deploy.yml. This file will define the steps for testing, building, and deploying your Axum application.

name: CI/CD for Axum App

on:
  push:
    branches:
      - main

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout repository
        uses: actions/checkout@v3

      - name: Set up Rust
        uses: actions/setup-rust@v1
        with:
          rust-version: '1.65'

      - name: Cache cargo registry
        uses: actions/cache@v2
        with:
          path: ~/.cargo/registry
          key: ${{ runner.os }}-cargo-registry-${{ hashFiles('**/Cargo.lock') }}
          restore-keys: |
            ${{ runner.os }}-cargo-registry-

      - name: Cache cargo build
        uses: actions/cache@v2
        with:
          path: target
          key: ${{ runner.os }}-cargo-build-${{ hashFiles('**/Cargo.lock') }}
          restore-keys: |
            ${{ runner.os }}-cargo-build-

      - name: Build
        run: cargo build --release

      - name: Run tests
        run: cargo test

      - name: Build Docker image
        run: |
          docker build -t axum-app .

      - name: Log in to Docker Hub
        uses: docker/login-action@v2
        with:
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_PASSWORD }}

      - name: Push Docker image
        run: |
          docker push yourdockerhub/axum-app:latest

      - name: Deploy to server
        run: |
          ssh user@your_server 'docker pull yourdockerhub/axum-app:latest && docker stop axum-container || true && docker run -d --name axum-container -p 80:8080 yourdockerhub/axum-app:latest'
        env:
          SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }}

Step 4: Configure Secrets in GitHub

For secure deployment, store sensitive data such as Docker Hub credentials and SSH private keys as secrets in your GitHub repository. Navigate to Settings > Secrets and add DOCKER_USERNAME, DOCKER_PASSWORD, and SSH_PRIVATE_KEY.

Step 5: Push Changes and Automate Deployment

Commit and push your code along with the workflow file. GitHub Actions will automatically trigger the pipeline on pushes to the main branch. Monitor the Actions tab to verify successful execution.

Conclusion

Integrating Axum with GitHub Actions streamlines your development workflow, enabling continuous testing and deployment. By containerizing your application and automating the process, you ensure faster releases and improved reliability. Experiment with customizing your pipeline further to suit your deployment environment and project needs.