GitHub Actions
What are GitHub Actions?
GitHub Actions is a CI/CD platform that allows you to automate build, test, and deployment workflows directly from your GitHub repository.
Basic Workflow
# .github/workflows/ci.yml
name: CI
on:
push:
branches: [main, develop]
pull_request:
branches: [main]
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: '18'
- name: Install dependencies
run: npm ci
- name: Run tests
run: npm test
- name: Build
run: npm run buildMulti-Stack Workflows
Angular
name: Angular CI/CD
on: [push, pull_request]
jobs:
angular:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: '18'
cache: 'npm'
- run: npm ci
- run: ng lint
- run: ng test --watch=false --code-coverage
- run: ng build --configuration production
- name: Deploy to Firebase
if: github.ref == 'refs/heads/main'
run: |
npm install -g firebase-tools
firebase deploy --token ${{ secrets.FIREBASE_TOKEN }}.NET
name: .NET CI/CD
on: [push, pull_request]
jobs:
dotnet:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-dotnet@v3
with:
dotnet-version: '8.0.x'
- run: dotnet restore
- run: dotnet build --no-restore -c Release
- run: dotnet test --no-build -c Release
- run: dotnet publish -c Release -o ./publish
- name: Deploy to Azure
if: github.ref == 'refs/heads/main'
uses: azure/webapps-deploy@v2
with:
app-name: my-dotnet-app
publish-profile: ${{ secrets.AZURE_PUBLISH_PROFILE }}
package: ./publishNode.js with Databases
name: Node.js with Databases
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
services:
postgres:
image: postgres:15
env:
POSTGRES_PASSWORD: postgres
options: >-
--health-cmd pg_isready
--health-interval 10s
mongodb:
image: mongo:6
ports:
- 27017:27017
redis:
image: redis:7
ports:
- 6379:6379
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: '18'
- run: npm ci
- name: Run migrations
run: npm run migrate
env:
DATABASE_URL: postgresql://postgres:postgres@localhost:5432/test
- name: Run tests
run: npm test
env:
DATABASE_URL: postgresql://postgres:postgres@localhost:5432/test
MONGODB_URL: mongodb://localhost:27017/test
REDIS_URL: redis://localhost:6379Matrix Builds
name: Matrix Build
on: [push]
jobs:
test:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
node: [16, 18, 20]
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node }}
- run: npm ci
- run: npm testCaching
jobs:
build:
steps:
- uses: actions/checkout@v3
- uses: actions/cache@v3
with:
path: ~/.npm
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-
- run: npm ci
- run: npm testArtifacts
jobs:
build:
steps:
- run: npm run build
- uses: actions/upload-artifact@v3
with:
name: dist
path: dist/
deploy:
needs: build
steps:
- uses: actions/download-artifact@v3
with:
name: dist
- run: ./deploy.shSecrets Management
jobs:
deploy:
steps:
- name: Deploy
env:
API_KEY: ${{ secrets.API_KEY }}
DATABASE_URL: ${{ secrets.DATABASE_URL }}
run: ./deploy.shReusable Workflows
# .github/workflows/reusable-test.yml
name: Reusable Test
on:
workflow_call:
inputs:
node-version:
required: true
type: string
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: ${{ inputs.node-version }}
- run: npm ci
- run: npm test# .github/workflows/main.yml
name: Main
on: [push]
jobs:
test-node-18:
uses: ./.github/workflows/reusable-test.yml
with:
node-version: '18'Docker Build
name: Docker Build
on: [push]
jobs:
docker:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Login to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Build and push
uses: docker/build-push-action@v4
with:
context: .
push: true
tags: myapp:${{ github.sha }},myapp:latestInterview Tips
- Explain GitHub Actions: CI/CD platform for GitHub
- Show workflows: Multi-stack examples
- Demonstrate services: Database integration
- Discuss matrix: Multiple OS/versions
- Mention caching: Speed optimization
- Show secrets: Secure credential management
Summary
GitHub Actions automates CI/CD workflows directly in GitHub repositories. Supports multi-stack applications including Angular, .NET, Node.js with databases. Use matrix builds for multiple environments. Implement caching for speed. Manage secrets securely. Integrate with Docker and cloud platforms.
Test Your Knowledge
Take a quick quiz to test your understanding of this topic.