Script Valley
CI/CD with GitHub Actions
Deployment WorkflowsLesson 4.5

How to implement rollback in a GitHub Actions deployment pipeline

rollback strategy, previous image tag, deployment failure detection, if: failure(), manual rollback workflow, workflow_dispatch inputs, deployment tracking

Why Rollback Matters

Deployment failure triggering rollback

Automated rollback returns a system to its last known good state when a deployment fails. The key is tracking what was deployed before the failing version.

Automatic Rollback on Failure

steps:
  - name: Deploy new version
    id: deploy
    run: ./deploy.sh ${{ github.sha }}

  - name: Run smoke tests
    id: smoke
    run: ./smoke-tests.sh

  - name: Rollback on smoke test failure
    if: failure() && steps.smoke.outcome == 'failure'
    run: |
      PREVIOUS_SHA=$(git rev-parse HEAD~1)
      ./deploy.sh $PREVIOUS_SHA

Manual Rollback Workflow

on:
  workflow_dispatch:
    inputs:
      version:
        description: 'Image tag to roll back to'
        required: true
        type: string

jobs:
  rollback:
    runs-on: ubuntu-latest
    environment: production
    steps:
      - run: ./deploy.sh ${{ inputs.version }}

A manual rollback workflow with a workflow_dispatch input gives operators a reliable escape hatch. The environment gate ensures an approver reviews rollbacks to production. Combine both approaches: automatic rollback for smoke test failures and manual rollback for post-deployment issues discovered later.