Table of contents
Example 1: Basic CI Pipeline
This example shows a simple CI pipeline that runs tests on every push to the main branch.
Workflow YAML:
name: CI Pipeline
on:
push:
branches:
- main
jobs:
build-and-test:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: '14'
- name: Install dependencies
run: npm install
- name: Run tests
run: npm test
Explanation:
- on: push: Triggers the workflow on pushes to the main branch.
- jobs: Defines the job that will run.
- steps: Lists the steps to be executed, such as checking out the code, setting up Node.js, installing dependencies, and running tests.
Example 2: CI/CD Pipeline with Deployment
This example shows a CI/CD pipeline that builds, tests, and deploys a Node.js application to an Azure App Service.
Workflow YAML:
name: CI/CD Pipeline
on:
push:
branches:
- main
jobs:
build-test-deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: '14'
- name: Install dependencies
run: npm install
- name: Run tests
run: npm test
- name: Build project
run: npm run build
- name: Deploy to Azure Web App
uses: azure/webapps-deploy@v2
with:
app-name: 'my-web-app'
publish-profile: $
package: .
Explanation:
- on: push: Triggers the workflow on pushes to the main branch.
- jobs: Defines the job that will run.
- steps: Lists the steps to be executed, including checking out the code, setting up Node.js, installing dependencies, running tests, building the project, and deploying to Azure Web App.
- secrets: Securely stores sensitive information like the Azure publish profile.
Example 3: Scheduled Workflow
This example demonstrates a workflow that runs tests every day at midnight.
Workflow YAML:
name: Scheduled Test Run
on:
schedule:
- cron: '0 0 * * *'
jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.8'
- name: Install dependencies
run: pip install -r requirements.txt
- name: Run tests
run: pytest
Explanation:
- on: schedule: Triggers the workflow based on a cron schedule (every day at midnight).
- jobs: Defines the job that will run.
- steps: Lists the steps to be executed, such as checking out the code, setting up Python, installing dependencies, and running tests.
Example 4: Multi-Environment Deployment
This example shows how to deploy to different environments (staging and production) based on the branch.
Workflow YAML:
name: Multi-Environment Deployment
on:
push:
branches:
- main
- staging
jobs:
build-and-deploy:
runs-on: ubuntu-latest
strategy:
matrix:
environment: [staging, production]
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: '14'
- name: Install dependencies
run: npm install
- name: Run tests
run: npm test
- name: Build project
run: npm run build
- name: Deploy to Azure Web App
uses: azure/webapps-deploy@v2
with:
app-name: $
publish-profile: $ }}
package: .
Explanation:
- on: push: Triggers the workflow on pushes to the main and staging branches.
- strategy: matrix: Defines a matrix strategy to run the job for both staging and production environments.
- steps: Lists the steps to be executed, including checking out the code, setting up Node.js, installing dependencies, running tests, building the project, and deploying to Azure Web App.
- conditional logic: Uses conditional logic to deploy to the appropriate environment based on the branch.