DevOps & Deployment

GitHub Actions Setup & Deployment Guide

Automate testing and deployment inside GitHub – write workflows, use secrets and environments, build Docker images and deploy to a server or AWS.

Updated Official documentation ↗

1. Introduction & use cases

GitHub Actions runs automation workflows stored in your repository under .github/workflows/. A workflow is triggered by events (push, pull request, schedule, manual), and contains jobs that run on GitHub-hosted (or your own) runners. Each job is a list of steps – shell commands or reusable actions from the Marketplace.

Public repositories get free minutes; private ones have a monthly free quota. Secrets, environments with approvals, caching and OIDC login to clouds make it a complete CI/CD system.

Where it is used

  • Run tests and linters on every pull request
  • Build and publish Docker images and packages
  • Deploy to servers, Kubernetes, AWS, Vercel or GitHub Pages
  • Scheduled jobs: backups, reports, dependency checks

2. Prerequisites

You needVersion / notes
GitHub repositorySee the GitHub guide
A project with testse.g. npm test works locally
YAML basicsIndentation with spaces

3. Local setup

  1. 1Create the workflow folder

    Workflows are YAML files in this exact path.

    BASH
    mkdir -p .github/workflows
  2. 2Optional: run workflows locally with act

    act runs workflows in Docker on your machine for quick feedback.

    BASH
    # macOS: brew install act   ·   Windows: winget install nektos.act
    act pull_request

4. Create & run a project

  1. 1Write a CI workflow

    Runs on pushes to main and on every pull request; tests on two Node versions with npm caching.

    .github/workflows/ci.yml
    name: ci
    on:
      push:
        branches: [main]
      pull_request:
    jobs:
      test:
        runs-on: ubuntu-latest
        strategy:
          matrix:
            node: [20, 22]
        steps:
          - uses: actions/checkout@v4
          - uses: actions/setup-node@v4
            with:
              node-version: ${{ matrix.node }}
              cache: npm
          - run: npm ci
          - run: npm run lint --if-present
          - run: npm test
          - run: npm run build --if-present
  2. 2Push and watch it run

    Open the Actions tab, or follow it with gh.

    BASH
    git add .github/workflows/ci.yml
    git commit -m "Add CI"
    git push
    gh run watch
  3. 3Make it a required check

    In Settings → Rules/Branches, require the test jobs to pass before merging into main.

✅ When it works you should see output like Figure 1.

5. Development configuration

Environment variables

Add secrets in Settings → Secrets and variables → Actions. Read them as ${{ secrets.NAME }}; they are masked in logs and not passed to workflows from forks. Use environments (e.g. production) to add required reviewers and environment-specific secrets.

workflow snippet
env:
  NODE_ENV: production
jobs:
  deploy:
    environment: production       # approvals + env secrets
    steps:
      - run: ./deploy.sh
        env:
          DATABASE_URL: ${{ secrets.DATABASE_URL }}

Key dependencies

Package / toolUsed for
actions/checkoutGet the repository code
actions/setup-node / setup-python / setup-goInstall language runtimes with caching
docker/login-action, docker/build-push-actionBuild and push images
aws-actions/configure-aws-credentialsLog in to AWS with OIDC (no stored keys)
actions/upload-artifactKeep build outputs and reports

Folder structure

project structure
.github/
└── workflows/
    ├── ci.yml       # tests on every PR
    ├── docker.yml   # build + push image
    └── deploy.yml   # deploy on main / tags

Essential commands

CommandWhat it does
gh workflow list / run NAMEList or manually run workflows
gh run list / view / watchFollow runs
gh run rerun ID --failedRetry failed jobs
gh secret set NAMEAdd a secret
actRun workflows locally

6. Production deployment

Server & hosting requirements

RequirementRecommendation
TargetA server (SSH), Kubernetes cluster, AWS, or static host
CredentialsRepository/environment secrets or OIDC roles
ApprovalsEnvironment protection rules for production
ArtifactsBuild once, deploy the same artifact/image

Deployment steps

  1. 1Add deployment secrets

    Create a dedicated SSH key pair; put the public key in the server user’s ~/.ssh/authorized_keys and the private key in the secret.

    BASH
    ssh-keygen -t ed25519 -f gh_deploy -N ""
    gh secret set SSH_PRIVATE_KEY < gh_deploy
    gh secret set PROD_HOST --body "203.0.113.10"
    ssh-copy-id -i gh_deploy.pub deploy@203.0.113.10
  2. 2Deploy over SSH after tests pass

    Builds, uploads with rsync and restarts the app. needs: test keeps broken code out of production.

    .github/workflows/deploy.yml
    name: deploy
    on:
      push:
        branches: [main]
    jobs:
      test:
        runs-on: ubuntu-latest
        steps:
          - uses: actions/checkout@v4
          - uses: actions/setup-node@v4
            with: { node-version: lts/*, cache: npm }
          - run: npm ci && npm test
      deploy:
        needs: test
        runs-on: ubuntu-latest
        environment: production
        steps:
          - uses: actions/checkout@v4
          - uses: actions/setup-node@v4
            with: { node-version: lts/*, cache: npm }
          - run: npm ci && npm run build
          - name: Upload and restart
            env:
              KEY: ${{ secrets.SSH_PRIVATE_KEY }}
              HOST: ${{ secrets.PROD_HOST }}
            run: |
              install -m 600 /dev/null key && echo "$KEY" > key
              ssh-keyscan -H "$HOST" >> ~/.ssh/known_hosts
              rsync -az --delete -e "ssh -i key" ./ deploy@$HOST:/var/www/app/ --exclude .git --exclude node_modules
              ssh -i key deploy@$HOST 'cd /var/www/app && npm ci --omit=dev && pm2 reload app'
  3. 3Or build and push a Docker image to GHCR

    The built-in GITHUB_TOKEN can push to the repository’s container registry.

    .github/workflows/docker.yml
    name: docker
    on:
      push:
        tags: ['v*']
    permissions:
      contents: read
      packages: write
    jobs:
      image:
        runs-on: ubuntu-latest
        steps:
          - uses: actions/checkout@v4
          - uses: docker/login-action@v3
            with:
              registry: ghcr.io
              username: ${{ github.actor }}
              password: ${{ secrets.GITHUB_TOKEN }}
          - uses: docker/build-push-action@v6
            with:
              push: true
              tags: ghcr.io/${{ github.repository }}:${{ github.ref_name }}
  4. 4Deploy to AWS without stored keys (OIDC)

    Create an IAM role that trusts GitHub’s OIDC provider for your repository, then assume it in the workflow.

    YAML
    permissions:
      id-token: write
      contents: read
    steps:
      - uses: aws-actions/configure-aws-credentials@v4
        with:
          role-to-assume: arn:aws:iam::123456789012:role/github-deploy
          aws-region: ap-south-1
      - run: aws s3 sync dist/ s3://my-site-bucket --delete

🗺 The whole path from your code to the live site is drawn in the deployment diagram.

7. Images & screenshots

Illustrations drawn for this guide from the commands above. Product dashboards change often, so for screens such as cloud consoles follow the step text and the official documentation.

terminal$ gh run watch✓ main ci · 1234567890Triggered via push less than a minute agoJOBS✓ test (20) in 48s (ID 1) ✓ Set up job ✓ Run actions/checkout@v4 ✓ Run actions/setup-node@v4 ✓ Run npm ci ✓ Run npm test✓ test (22) in 51s (ID 2)
Figure 1: terminal output of the sample GitHub Actions project (section 4).
1Push / PR2CI: test + build3Approval(environment)4Deploy job (SSH /image / AWS)5Live + logs inActions
Figure 2: production deployment flow (section 6).
.github/└── workflows/ ├── ci.yml # tests on every PR ├── docker.yml # build + push image └── deploy.yml # deploy on main / tags
Figure 3: project folder structure (section 5).

8. Common errors & solutions

Workflow does not start

File must be in .github/workflows/ with .yml/.yaml, valid YAML, and the trigger must match (branch names, paths).

Error: Process completed with exit code 1

A command failed – open the step log; reproduce locally with the same Node version and npm ci.

Host key verification failed during SSH deploy

Add the server to known_hosts with ssh-keyscan (as in the example) before connecting.

denied: installation not allowed to Write organization package

Add permissions: packages: write and allow the repository in the package’s settings.

Secrets are empty in pull requests from forks

By design – secrets are not shared with forks. Run deployments only on push to protected branches.

Best practices

  • Pin actions to a major version (or a commit SHA for third-party actions).
  • Set minimal permissions: per workflow.
  • Use environments with required reviewers for production.
  • Prefer OIDC over long-lived cloud keys.
  • Build once and promote the same artifact/image to each environment.