CI/CD with GitHub Actions

Run tests on every pull request and deploy automatically on every merge: workflow YAML explained, secrets, SSH deploys to a VPS, building artifacts in CI, environments with approvals, and safe concurrency.

Intermediate⏱ 6 min readLesson 9 of 17#ci-cd#github-actions#deployment#automation#devops

The big idea

Today you SSH into the server and type the deploy commands yourself. That works until you're on holiday, or you forget npm ci, or two people deploy at once.

CI/CD hands those steps to a robot that does them the same way every time:

  • CI (continuous integration): every push and pull request is automatically installed, linted, tested and built. Broken code is caught before it's merged.
  • CD (continuous delivery/deployment): every merge to main is automatically released to the server.

A push triggers a GitHub Actions workflow: the test job runs first, and the deploy job runs only if tests pass and the branch is main

GitHub Actions vocabulary

TermMeaning
WorkflowA YAML file in .github/workflows/ that describes an automation
Trigger (on:)What starts it: push, pull_request, a schedule, a manual button
JobA group of steps that runs on one fresh machine (a runner)
StepOne command (run:) or a reusable action (uses:)
RunnerThe virtual machine: ubuntu-latest, or your own self-hosted runner
SecretAn encrypted value (SSH key, API token) available to workflows but never shown in logs
EnvironmentA deployment target (staging, production) with its own secrets and optional approval

Step 1: CI, test every push and pull request

.github/workflows/ci.yml:

name: CI

on:
  pull_request:
  push:
    branches: [main]

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-node@v4
        with:
          node-version: 22
          cache: npm                 # reuse downloaded packages between runs

      - run: npm ci
      - run: npm run lint
      - run: npm test
      - run: npm run build           # a build error fails the PR, not production

Then protect the branch: Settings → Branches → Add rule for main → Require status checks to pass (select test). Now nobody can merge a red pull request.

Step 2: Store secrets

Go to Settings → Secrets and variables → Actions and add:

SecretValue
SSH_HOST203.0.113.10
SSH_USERdeploy
SSH_PRIVATE_KEYA new private key made just for CI (not your personal key)
SSH_KNOWN_HOSTSOutput of ssh-keyscan 203.0.113.10 (protects against fake servers)
# On your laptop: a dedicated key for GitHub Actions
ssh-keygen -t ed25519 -f gha_deploy -N "" -C "github-actions"
ssh-copy-id -i gha_deploy.pub deploy@203.0.113.10
cat gha_deploy          # paste into the SSH_PRIVATE_KEY secret, then delete the local file

🔒 Secrets are hidden in logs and are not given to workflows triggered by pull requests from forks. Still: give the CI user the minimum permissions (it only needs to deploy and restart one service).

Step 3: CD, deploy over SSH after tests pass

Add a second job to the same workflow:

  deploy:
    needs: test                                     # only if the test job passed
    if: github.ref == 'refs/heads/main' && github.event_name == 'push'
    runs-on: ubuntu-latest
    environment: production                         # shows up under "Deployments"
    concurrency:
      group: production
      cancel-in-progress: false                     # never run two deploys at once
    steps:
      - name: Set up SSH
        run: |
          mkdir -p ~/.ssh
          echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_ed25519
          chmod 600 ~/.ssh/id_ed25519
          echo "${{ secrets.SSH_KNOWN_HOSTS }}" > ~/.ssh/known_hosts

      - name: Deploy
        run: |
          ssh ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }} \
            "bash ~/bin/deploy.sh ${{ github.sha }}"

The server runs the same deploy.sh you'd run by hand (lessons 6 and 11), and the workflow fails (red ❌) if the script exits with a non-zero code, for example when the health check fails.

Better: build in CI, copy the result

Building on the server has downsides: it uses the server's CPU and RAM (a big Next.js build can crash a 1 GB VPS), and the build happens while the site is serving users.

Building in CI and shipping the result is more robust:

  deploy:
    needs: test
    if: github.ref == 'refs/heads/main'
    runs-on: ubuntu-latest
    environment: production
    concurrency: production
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with: { node-version: 22, cache: npm }
      - run: npm ci
      - run: npm run build

      - name: Package the release
        run: tar -czf release.tgz .next public package.json package-lock.json next.config.*

      - name: Set up SSH
        run: |
          mkdir -p ~/.ssh
          echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_ed25519 && chmod 600 ~/.ssh/id_ed25519
          echo "${{ secrets.SSH_KNOWN_HOSTS }}" > ~/.ssh/known_hosts

      - name: Upload and activate
        env:
          TARGET: ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }}
          RELEASE: ${{ github.sha }}
        run: |
          scp release.tgz "$TARGET:/tmp/release-$RELEASE.tgz"
          ssh "$TARGET" "bash ~/bin/activate-release.sh $RELEASE"

activate-release.sh unpacks into a new release folder, runs npm ci --omit=dev, switches a symlink and restarts the service. That pattern is lesson 11.

💡 Build once, deploy the same artifact everywhere. If staging and production each run their own build, you can't be sure production gets exactly what you tested.

Environments and approvals

Settings → Environments → New environment: production:

  • Required reviewers: a person must click "Approve" before the deploy job runs.
  • Deployment branches: only main may deploy to production.
  • Environment secrets: production SSH keys live only here, so a staging job can't read them.

A common flow:

Drawing diagram…

Rollbacks from CI

Make it possible to deploy any earlier commit with a manual button:

on:
  workflow_dispatch:
    inputs:
      ref:
        description: "Commit SHA or tag to deploy"
        required: true

Or keep previous releases on the server and switch back in seconds (lesson 11). Rollback should be faster than a fix.

Speed and reliability tips

TipHow
Cache dependenciesactions/setup-node with cache: npm
Run checks in parallelSeparate lint, test and build jobs
Fail fastLint (seconds) before end-to-end tests (minutes)
Avoid double deploysconcurrency: production
Pin action versionsactions/checkout@v4 (or a full commit SHA for strict security)
NotifyA Slack/Discord step with if: failure()
Keep secrets out of logsNever echo a secret; use env: instead of inline interpolation in long scripts

Other CI/CD tools

ToolNotes
GitHub ActionsBuilt into GitHub, huge marketplace of actions
GitLab CI.gitlab-ci.yml, built into GitLab, strong self-hosting
JenkinsSelf-hosted, very flexible, more maintenance (lessons 15 and 16)
CircleCI, BuildkiteHosted CI services
Argo CD, FluxGitOps for Kubernetes: the cluster pulls changes from Git

The ideas are the same everywhere: trigger → jobs → steps → secrets → deploy only after checks pass. Lesson 14 explains these shared ideas in depth.

Key takeaways

  • CI tests every push and PR; protect main so red builds can't merge.
  • CD deploys automatically after tests pass, using needs:, if: and environment:.
  • Store SSH keys and tokens as secrets, with a dedicated, low-privilege CI key.
  • Prefer building in CI and shipping the artifact; deploy the same build everywhere.
  • Use concurrency to prevent overlapping deploys, and keep rollback one click away.

Try it yourself

Add the CI workflow above to one of your repositories, open a pull request that breaks a test, and confirm GitHub blocks the merge. Then fix it and watch the checks turn green.