DEV Community

abid karim
abid karim

Posted on

πŸš€ Automate Your Workflow with GitHub Actions: A Beginner-Friendly Guide

✨ What is GitHub Actions?
GitHub Actions is a powerful CI/CD tool built directly into GitHub that allows you to automate your software workflows, such as:

Building and testing code automatically when you push.

Deploying applications.

Running scheduled jobs.

Automating tasks like code linting, formatting, or tagging.

πŸ›  Real-World Use Case: Automating Tests for a Node.js App
Let’s walk through a real-world example where we automatically:

Run tests on every push.

Lint code using ESLint.

Only allow merge if tests pass.

πŸ“ Project Structure
Here’s a simple Node.js app structure:
my-node-app/
β”œβ”€β”€ .github/
β”‚ └── workflows/
β”‚ └── nodejs.yml
β”œβ”€β”€ app.js
β”œβ”€β”€ package.json
β”œβ”€β”€ .eslintrc.json
└── test/
└── app.test.js
βœ… Step-by-Step: Creating Your First Workflow
πŸ“ Step 1: Create Workflow File
Create a file in your repo:
.github/workflows/nodejs.yml

🧩 Step 2: Add This Workflow YAML
name: Node.js CI

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout code
  uses: actions/checkout@v3

- name: Set up Node.js
  uses: actions/setup-node@v3
  with:
    node-version: '18'

- name: Install dependencies
  run: npm install

- name: Run Linter
  run: npm run lint

- name: Run Tests
  run: npm test
Enter fullscreen mode Exit fullscreen mode

βœ… This workflow is triggered on every push or pull request to the main branch.

πŸ–Ό Image: GitHub Actions UI
Once your workflow is pushed, go to the Actions tab in your repo to see it run:
πŸ§ͺ Real-Time Output
If you include a test like this in test/app.test.js:
const assert = require('assert');

describe('Sample Test', () => {
it('should return true', () => {
assert.strictEqual(true, true);
});
});
You’ll see this in the Actions logs:

mocha

Sample Test
βœ“ should return true

1 passing (12ms)
πŸŽ‰ Now every push runs your tests!

🌎 Deploy to GitHub Pages or AWS
You can deploy to:

GitHub Pages: Use peaceiris/actions-gh-pages

AWS S3: Use jakejarvis/s3-sync-action

Example:

  • name: Deploy to S3 uses: jakejarvis/s3-sync-action@master with: args: --acl public-read --delete env: AWS_S3_BUCKET: ${{ secrets.AWS_BUCKET }} AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY }} AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_KEY }} πŸ” Using Secrets Store sensitive info like AWS keys in Settings > Secrets and variables > Actions.

Then access them like:
env:
TOKEN: ${{ secrets.MY_SECRET_TOKEN }}
πŸ‘€ Monitoring and Debugging
Use core.set-output to debug values.

Add continue-on-error: true to experiment safely.

Use matrix to test across different Node versions.

πŸ”š Final Thoughts
GitHub Actions makes DevOps easier right inside your GitHub repo. Whether you're building apps, running tests, or deploying live, GitHub Actions provides a robust and scalable way to automate it all.

Top comments (0)