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 need | Version / notes |
|---|---|
| GitHub repository | See the GitHub guide |
| A project with tests | e.g. npm test works locally |
| YAML basics | Indentation with spaces |
3. Local setup
1Create the workflow folder
Workflows are YAML files in this exact path.
BASHmkdir -p .github/workflows2Optional: 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
1Write a CI workflow
Runs on pushes to
mainand on every pull request; tests on two Node versions with npm caching..github/workflows/ci.ymlname: 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-present2Push and watch it run
Open the Actions tab, or follow it with gh.
BASHgit add .github/workflows/ci.yml git commit -m "Add CI" git push gh run watch3Make it a required check
In Settings → Rules/Branches, require the
testjobs to pass before merging intomain.
✅ 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.
env:
NODE_ENV: production
jobs:
deploy:
environment: production # approvals + env secrets
steps:
- run: ./deploy.sh
env:
DATABASE_URL: ${{ secrets.DATABASE_URL }}Key dependencies
| Package / tool | Used for |
|---|---|
actions/checkout | Get the repository code |
actions/setup-node / setup-python / setup-go | Install language runtimes with caching |
docker/login-action, docker/build-push-action | Build and push images |
aws-actions/configure-aws-credentials | Log in to AWS with OIDC (no stored keys) |
actions/upload-artifact | Keep build outputs and reports |
Folder structure
.github/
└── workflows/
├── ci.yml # tests on every PR
├── docker.yml # build + push image
└── deploy.yml # deploy on main / tagsEssential commands
| Command | What it does |
|---|---|
gh workflow list / run NAME | List or manually run workflows |
gh run list / view / watch | Follow runs |
gh run rerun ID --failed | Retry failed jobs |
gh secret set NAME | Add a secret |
act | Run workflows locally |
6. Production deployment
Server & hosting requirements
| Requirement | Recommendation |
|---|---|
| Target | A server (SSH), Kubernetes cluster, AWS, or static host |
| Credentials | Repository/environment secrets or OIDC roles |
| Approvals | Environment protection rules for production |
| Artifacts | Build once, deploy the same artifact/image |
Deployment steps
1Add deployment secrets
Create a dedicated SSH key pair; put the public key in the server user’s
~/.ssh/authorized_keysand the private key in the secret.BASHssh-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.102Deploy over SSH after tests pass
Builds, uploads with rsync and restarts the app.
needs: testkeeps broken code out of production..github/workflows/deploy.ymlname: 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'3Or build and push a Docker image to GHCR
The built-in
GITHUB_TOKENcan push to the repository’s container registry..github/workflows/docker.ymlname: 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 }}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.
YAMLpermissions: 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.
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.