Introduction
GitHub Actions is a feature provided by GitHub that helps you automate your software development process. It is used for Continuous Integration (CI) and Continuous Deployment (CD). With GitHub Actions, you can automatically build, test, and deploy your code whenever changes are made.
What is CI/CD?
- Continuous Integration (CI) means automatically checking and testing your code when new code is added. It helps to catch errors early.
- Continuous Deployment (CD) means automatically deploying (publishing) your software to a server after it passes all tests.
This helps in saving time and reducing manual work.
What is GitHub Actions?
GitHub Actions is a workflow automation tool inside GitHub. It runs based on events like pushing code, creating pull requests, or creating a release.
You can use GitHub Actions to.
- Build and test your code
- Run scripts or commands
- Deploy your code to servers
- Send notifications, and more
How GitHub Actions Work?
GitHub Actions use something called Workflows.
Workflow
A workflow is a set of instructions written in a file named .yml (YAML format). This file is placed inside .github/workflows/ folder in your GitHub repository.
Each workflow is made of.
- Trigger (Event): What starts the workflow
- Jobs: A group of tasks
- Steps: The actual commands inside a job
- Actions: Reusable code (shared by GitHub community or created by you)
Example Workflow File
Let’s see a simple workflow example that runs when code is pushed to the main branch.
name: CI
on:
push:
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 tests
run: npm test
Explanation
- name: The name of the workflow (you can give any name).
- on: Specifies the event that triggers the workflow. Here it is pushed to the main branch.
- Jobs: Defines what tasks will be performed.
- runs-on: The virtual machine where the job will run (Ubuntu here).
- steps: A list of tasks to be performed in order
Common Actions You Can Use
GitHub provides many ready-to-use actions. Some common ones are.
- actions/checkout: To clone the code from your repo.
- actions/setup-node: To install Node.js.
- actions/setup-dotnet: To install .NET.
- actions/upload-artifact: To save build outputs.
- actions/download-artifact: To get previously saved files.
Adding Deployment (CD) to the Workflow
Here is an example of adding deployment after successful build and test.
name: CI/CD
on:
push:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Install dependencies
run: npm install
- name: Run tests
run: npm test
- name: Deploy to Server
run: |
echo "Deploying..."
# Add deployment commands here
You can replace the Deploy to Server step with actual deployment scripts like FTP upload, SSH commands, or calls to cloud services (like Azure, AWS, or Heroku).
Example Use Cases of GitHub Actions
- Web App Deployment: Deploy your frontend or backend automatically to a server when you push to the main branch.
- Run Unit Tests: Automatically test your code for errors when a pull request is created.
- Code Formatting: Automatically format code using tools like Prettier or ESLint.
- Send Notifications: Send messages to Slack or email when deployment fails.
Benefits of Using GitHub Actions
- It is built into GitHub. No need for external tools.
- It works with all languages and platforms.
- It saves time and reduces manual work.
- It increases code quality by running tests and checks automatically.
Pricing
- Free for public repositories.
- For private repositories, there is a monthly free limit (based on minutes and storage). After that, usage is charged.
Final Thoughts
GitHub Actions is a powerful tool for automating your development workflow. Once set up, it can save a lot of manual effort. You can build, test, and deploy with confidence.
Start small. Try adding a workflow that runs tests. Later, add deployment, notifications, and other tasks as needed.