CI/CD from Scratch: Automate Linux Server Deploy with GitHub Actions
CI/CD (Continuous Integration / Continuous Delivery) is the practice of automating code validation and delivery. Instead of manual and risky deploys, each push to the repository triggers a sequence of automated steps that test, package, and deliver code to production.
- CI (Continuous Integration): every commit is automatically tested before entering the main branch
- CD (Continuous Delivery): what passed the tests goes to production without manual intervention
GitHub Actions
GitHub Actions is the CI/CD tool integrated into GitHub. It is free for public repositories and has 2,000 minutes/month for private repositories on the free plan.
A workflow is defined in YAML files inside .github/workflows/. It defines when to run (trigger), where to run (runner), and what to do (jobs/steps).
Workflow Structure
# .github/workflows/ci.yml
name: CI — Testa a cada push
on:
push:
branches: [main, develop]
pull_request:
branches: [main]
jobs:
test:
runs-on: ubuntu-latest # runner: VM Ubuntu limpa
steps:
- name: Baixar o código
uses: actions/checkout@v4
- name: Configurar Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
- name: Instalar dependências
run: npm ci
- name: Rodar testes
run: npm test
- name: Verificar lint
run: npm run lint
Automatic Deploy via SSH
After testing, the second job deploys to the Linux server:
# .github/workflows/deploy.yml
name: CI/CD — Testa e faz deploy
on:
push:
branches: [main]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: npm ci && npm test
deploy:
needs: test # só roda se test passar
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Deploy via SSH
uses: appleboy/ssh-action@v1
with:
host: ${{ secrets.SERVER_HOST }}
username: ec2-user
key: ${{ secrets.SSH_PRIVATE_KEY }}
script: |
cd /home/plataforma
git pull origin main
npm install --production
pm2 restart app || pm2 start server.js --name app
Configuring Secrets
Never put passwords, keys, or IPs directly in YAML. Use GitHub Secrets:
Settings → Secrets and variables → Actions → New repository secret
SERVER_HOST = 54.123.45.67
SSH_PRIVATE_KEY = (conteúdo da sua chave privada .pem)
Secrets are available via ${{ secrets.NOME }} and never appear in logs.
Pipeline with Docker
For containerized applications, the pipeline builds, pushes the image, and updates the container in production:
name: Build e Deploy Docker
on:
push:
branches: [main]
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Login no Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_USER }}
password: ${{ secrets.DOCKER_TOKEN }}
- name: Build e push da imagem
run: |
IMAGE=${{ secrets.DOCKER_USER }}/minha-app
docker build -t $IMAGE:${{ github.sha }} -t $IMAGE:latest .
docker push $IMAGE:${{ github.sha }}
docker push $IMAGE:latest
- name: Deploy no servidor
uses: appleboy/ssh-action@v1
with:
host: ${{ secrets.SERVER_HOST }}
username: ec2-user
key: ${{ secrets.SSH_PRIVATE_KEY }}
envs: DOCKER_USER,GITHUB_SHA
script: |
docker pull $DOCKER_USER/minha-app:$GITHUB_SHA
docker stop minha-app || true
docker rm minha-app || true
docker run -d --name minha-app --restart unless-stopped -p 80:3000 $DOCKER_USER/minha-app:$GITHUB_SHA
env:
DOCKER_USER: ${{ secrets.DOCKER_USER }}
GITHUB_SHA: ${{ github.sha }}
Important Concepts
- Jobs: independent steps (or dependent with
needs:). Each job runs in its own clean VM. - Steps: commands inside a job, executed in sequence.
- Actions: reusable community blocks (like
actions/checkout,appleboy/ssh-action). - Matrix: runs the same job on multiple versions (Node 18, 20, 22 in parallel).
- Artifacts: files passed between jobs (e.g.: compiled binary in build, consumed in deploy).
CI/CD for Sysadmins
Even without being a developer, sysadmins benefit directly from pipelines:
- Ansible configuration deploys on every push to the infra repository
- Automatic sitemap or static file regeneration after merge
- Connectivity tests and Terraform lint before applying changes to production
- Slack notification when a deploy fails
Understanding CI/CD is today a requirement for any mid-level sysadmin position onwards — the difference between someone who "manages servers" and who "delivers infrastructure".
That was just one piece. The course teaches the whole path — from your first command to exam day.