AI Hints
CI/CD with GitHub Actions · Lesson 91 of 138

Deploying over SSH

The simplest deploy.

⏱ 2 min read▶ Video at 1:44:56🎓 Free
Watch this lesson in the CI/CD with GitHub Actions courseWatch on YouTube ↗Full CI/CD with GitHub Actions course

The simplest deployment is copying files to a server, and restarting the app. A workflow can do that over SSH, using a private key stored as a secret. It's not fancy, but it works for many small projects.

Deploying over SSH
From the video: Deploying over SSH
  • Copy files to a server
  • Restart the app
  • Over SSH

An SSH deploy job

The job uses the production environment, so it gets that environment's secrets. The first step writes the private key from a secret, and locks down its permissions. It also writes the server's known host key, so SSH can check it's talking to the right server. Then rsync copies the app, and SSH restarts the service.

.github/workflows/deploy.ymlyaml
jobs:
  deploy:
    runs-on: ubuntu-latest
    environment: production
    steps:
      - uses: actions/checkout@v4
      - name: Set up the SSH key
        run: |
          mkdir -p ~/.ssh
          echo "${{ secrets.DEPLOY_KEY }}" > ~/.ssh/id_ed25519
          chmod 600 ~/.ssh/id_ed25519
          echo "${{ secrets.KNOWN_HOSTS }}" > ~/.ssh/known_hosts
      - name: Copy and restart
        run: |
          rsync -az app.py deploy@shop.example.com:/srv/shop/
          ssh deploy@shop.example.com "sudo systemctl restart shop"

Doing it safely

Create a separate deploy key, used only by CI. Give the deploy user on the server as little power as possible. Store the key in an environment secret, so only approved deploys can use it. And always check the host key, rather than turning checking off.

keyvalue
Deploy keyonly for CI
Deploy userfew permissions
Env secretproduction only
known_hostsnever skip it

Summary

SSH deploys copy files and restart the app, with a key from a secret. Keep the key and the user limited. Next, publishing a website with GitHub Pages.

Key takeaways
  • Key in a secret
  • rsync + ssh
  • Check the host key
Next up: GitHub Pages — free static hosting