Stepping to Github Actions
GitHub Actions is an automation and continuous integration (CI)/continuous deployment (CD) service provided by GitHub. It allows developers to create custom workflows and automate various tasks directly within their GitHub repositories. This service is integrated with GitHub, making it easy to build, test, and deploy code changes without the need for external CI/CD tools.
Key features of GitHub Actions include:
- Workflows: A workflow is a customizable automated process that you define within your repository. It consists of one or more jobs that run on different runners (virtual machines or containers). Each job contains a series of steps that perform specific actions, such as building code, running tests, or deploying applications.
- Marketplace: GitHub Actions offers a wide range of pre-built actions available in the GitHub Marketplace. These actions can be easily integrated into your workflows, saving you time and effort.
- Event-Driven Triggers: Workflows in GitHub Actions can be triggered based on various events, such as push events (when code is pushed to the repository), pull request events, schedule events, manual events, and more. This allows you to automate specific tasks based on different actions taken in the repository. (pull, pull_request and worlflow_dispatch)
- Environment Variables and Secrets: GitHub Actions allows you to set environment variables and store secrets (e.g., API keys, passwords) securely, which can be accessed during workflow execution.
- Self-Hosted Runners: In addition to GitHub-hosted runners, you can set up your own self-hosted runners to execute workflows on your infrastructure, providing more control over your build and deployment environments.
Let’s walk through a simple example of using GitHub Actions to create a workflow that automatically runs tests whenever new code is pushed to a GitHub repository. For this example, we’ll assume you have a repository set up.
Step 1: Create a Workflow File Inside your GitHub repository, create a new directory called .github/workflows. In that directory, create a YAML file (e.g., ci.yml) to define your workflow.
name: sample workflow
on:
push:
branches:
- main #Event will trigger once code changes push to the main branch
workflow_dispatch: #This will give you a option to manually trigger the action in github actions
jobs:
hello1:
runs-on: ubuntu-latest #Set up runner OS eg: ubuntu, windows
steps:
- uses: actions/checkout@v2
- name: hello 1
run: echo "hello 1"
shell: bash
hello2:
runs-on: ubuntu-latest
steps:
- name: hello 2
run: echo '${{ toJSON((github.event))}}' | jq #print github event to identify the properties inside the event
shell: bash
Once you’ve created the ci.yml file and added the above content, commit the file to your repository and push it to GitHub. This will trigger the workflow defined in the file to start running.
Go to your repository on GitHub, click on the “Actions” tab, and you should see your workflow listed under “All workflows.” Click on the workflow to see its progress.
By leveraging GitHub Actions, developers can create efficient and automated software development workflows, ensuring code quality, reducing manual work, and speeding up the development and deployment process.
Thanks for reading. Let’s look more about Github actions in up coming stories.