GitHub Actions is a powerful automation platform integrated directly into GitHub repositories, allowing you to automate workflows for building, testing, and deploying your code. This guide covers various aspects of GitHub Actions, including its core concepts and a demo workflow to illustrate how to create, run, and check results.
- What is GitHub Actions
- Terms: Workflows, Events, Jobs, Steps
- How to use GitHub Actions — with step-by-step Demo
- Detailed Explanation of demo workflow
GitHub Actions is a CI/CD (Continuous Integration and Continuous Deployment) service that enables you to automate tasks within your software development lifecycle. With GitHub Actions, you can create custom workflows directly in your GitHub repository, triggered by events such as code pushes, pull requests, or scheduled tasks.
- Automation: Automate repetitive tasks such as building, testing, and deploying code.
- Integration: Easily integrate with other services and tools using pre-built actions from the GitHub Marketplace.
- Customization: Write custom actions in any programming language or script.
- Parallel Execution: Run multiple jobs in parallel to speed up your workflows.
- Community: Leverage a vast community of pre-built actions and reusable components.
- CI/CD Pipelines: Automatically build, test, and deploy your applications.
- Issue Management: Automatically label issues or close stale issues.
- Code Quality: Run static code analysis and linters on every pull request.
- Notification: Send notifications to team members via Slack or email when certain events occur.
In GitHub Actions, a workflow is an automated process set up in a repository to accomplish a specific task, such as CI/CD (Continuous Integration/Continuous Deployment).
Example:
name: CI
on:
push:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
steps:
- 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
Events in GitHub Actions are specific triggers that start workflows. These can include pushing code, creating a pull request, scheduling a time, or any other GitHub event.
Example:
- push event: Triggers when code is pushed to the repository.
- pull_request event: Triggers when a pull request is opened, synchronized, or reopened.
- schedule event: Triggers at specific times using cron syntax.
on:
push:
branches:
- main
pull_request:
branches:
- main
schedule:
- cron: '0 0 * * *'
Jobs are units of work within a workflow. Each job runs in a fresh instance of a virtual environment, and multiple jobs can run in parallel or sequentially depending on their dependencies.
Example:
jobs:
build:
runs-on: ubuntu-latest
steps:
- 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
deploy:
runs-on: ubuntu-latest
needs: build # This job depends on the 'build' job
steps:
- uses: actions/checkout@v2
- name: Deploy
run: npm run deploy
Steps are individual tasks within a job. Each step can run commands, use an action, or set up the environment.
Example:
- Checkout code: uses: actions/checkout@v2
- Set up Node.js: uses: actions/setup-node@v2
- Run shell commands: run: npm install
steps:
- 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: Deploy
run: npm run deploy
Here's a complete example that demonstrates how workflows, events, jobs, and steps are structured in GitHub Actions:
name: CI/CD Pipeline
on:
push:
branches:
- main
pull_request:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
steps:
- 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
deploy:
runs-on: ubuntu-latest
needs: build
steps:
- uses: actions/checkout@v2
- name: Deploy
run: npm run deploy
- To use GitHub Actions, you need to create a workflow file in your GitHub repository. This file defines the events that trigger the workflow, the jobs to run, and the steps within those jobs.
https://github.com/CydexCode/github-actions-demo
Navigate to GitHub and create a new repository or use an existing one.
- Create a .github/workflows directory in your repository.
Click on the “Actions” tab at the top of the page.
Click the “Set up a workflow yourself” button
change the yaml file name
Open the demo.yml file and add the following content:
name: demo
on: push
jobs:
my-job:
runs-on: ubuntu-latest
steps:
- name: my-step
run: echo "Hello World!"
Commit and push the demo.yml file to your repository.
Navigate to the “Actions” tab in your repository to see the workflow runs. Click on a workflow run to view the details and logs of each step.
Click on a workflow run to view the details and logs of each step.
You will see the my-job job and within it, the my-step step displaying "Hello World!".
- name: demo: The name of the workflow.
- on: push: The workflow is triggered by a push event to the repository.
- jobs: my-job: The workflow contains one job named my-job.
- runs-on: ubuntu-latest: The job runs on the latest version of an Ubuntu virtual environment.
- steps:: The list of steps to be executed in the job.
- name: my-step: A step named my-step.
- run: echo "Hello World!": The command to be executed in the step, which prints "Hello World!" to the console.
By following these steps and understanding the structure of workflows, events, jobs, and steps, you can start leveraging GitHub Actions to automate and streamline your development workflows effectively.
Thank you for your support! Follow CydexCode on YouTube , TikTok , Linkedin ,GitHub , Facebook , and Telegram for more exciting content! 🎉📱💻📢