← All Tutorials
DevOpsMar 12, 20269 min read

Set Up CI/CD with GitHub Actions

Automate deployments with GitHub Actions, environment secrets, and rollback strategies.

Introduction

Continuous Integration and Continuous Deployment eliminates manual deployment steps and ensures every change is tested before reaching production. This tutorial covers setting up a complete CI/CD pipeline with GitHub Actions that tests, builds, and deploys your application to Rackline automatically on every push to main.

Prerequisites

  • A Rackline account with a deployed application
  • A GitHub repository for your project
  • Rackline CLI API token

Step 1 — Generate a Rackline API Token

Create a scoped API token that GitHub Actions will use to authenticate with Rackline. Use a deploy-only token with minimal permissions for security.

bash
# Generate a deploy-scoped API token
rackline auth token create \
  --name github-actions-deploy \
  --scope deploy,env:read \
  --expiry 365d

# Copy the token — it's only shown once
# Add it to GitHub repository secrets:
# Settings > Secrets and variables > Actions > New repository secret
# Name: RACKLINE_API_TOKEN

Pro Tip

Create separate tokens for staging and production environments. This follows the principle of least privilege and limits the blast radius if a token is compromised.

Step 2 — Create the GitHub Actions Workflow

Create a workflow file that runs tests, builds your application, and deploys to Rackline. The workflow uses GitHub's hosted runners and caches dependencies for faster builds.

yaml
# .github/workflows/deploy.yml
name: Deploy to Rackline

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

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 22
          cache: npm
      - run: npm ci
      - run: npm test
      - run: npm run build

  deploy:
    needs: test
    if: github.ref == 'refs/heads/main'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Install Rackline CLI
        run: npm install -g @rackline/cli@latest
      - name: Deploy
        env:
          RACKLINE_API_TOKEN: ${{ secrets.RACKLINE_API_TOKEN }}
        run: rackline deploy --environment production --yes

Step 3 — Add Preview Deployments for PRs

Create preview deployments for every pull request so reviewers can test changes in an isolated environment. Rackline generates a unique URL for each preview that is automatically posted as a PR comment.

yaml
  preview:
    if: github.event_name == 'pull_request'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Install Rackline CLI
        run: npm install -g @rackline/cli@latest
      - name: Deploy Preview
        env:
          RACKLINE_API_TOKEN: ${{ secrets.RACKLINE_API_TOKEN }}
        run: |
          URL=$(rackline deploy --preview --branch ${{ github.head_ref }} --json | jq -r '.url')
          echo "Preview: $URL" >> $GITHUB_STEP_SUMMARY

Step 4 — Manage Environment Secrets

Use GitHub environment protection rules to require approval before production deployments. Configure separate staging and production environments with different secrets and approval requirements.

yaml
  deploy-staging:
    needs: test
    environment: staging
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - run: npm install -g @rackline/cli@latest
      - run: rackline deploy --environment staging --yes
        env:
          RACKLINE_API_TOKEN: ${{ secrets.RACKLINE_STAGING_TOKEN }}

  deploy-production:
    needs: deploy-staging
    environment: production  # Requires manual approval
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - run: npm install -g @rackline/cli@latest
      - run: rackline deploy --environment production --yes
        env:
          RACKLINE_API_TOKEN: ${{ secrets.RACKLINE_PRODUCTION_TOKEN }}

Step 5 — Configure Rollback Strategy

Set up automatic rollback if a deployment fails health checks. Rackline keeps the last 10 deployment versions and can instantly roll back to any previous version without rebuilding.

bash
# List recent deployments
rackline deployments list --limit 10

# Roll back to a specific deployment
rackline deployments rollback --to dpl_abc123

# Roll back to the previous deployment
rackline deployments rollback --previous

# Set up automatic rollback on health check failure
rackline deploy --environment production \
  --health-check /health \
  --health-timeout 30s \
  --auto-rollback

Pro Tip

Always include a /health endpoint in your application that checks critical dependencies (database, cache, external APIs). Rackline uses this endpoint to determine deployment success.

Conclusion

Your CI/CD pipeline is now fully automated. Every push to main runs tests, builds the application, and deploys to Rackline. Pull requests get preview deployments, and failed deployments automatically roll back. This pipeline ensures that only tested, working code reaches production.