GitHub Actions: A Comprehensive Guide to Automation from Scratch




Introduction:
GitHub Actions is a powerful automation tool that enables you to streamline your development workflows. From continuous integration to deployment and beyond, GitHub Actions allows you to automate various tasks within your GitHub repository. In this guide, we'll cover the fundamental concepts of GitHub Actions, explaining each component and its purpose, along with real-world examples to illustrate their practical usage.

 GitHub Actions Flow 






1. Workflows:
Workflows are the heart of GitHub Actions. They define a set of jobs and the events that trigger them. A workflow is written in YAML format and resides in the `.github/workflows` directory of your repository. Workflows can be triggered by events such as pushes, pull requests, or scheduled intervals.
name: Hello World
on:
push: #Can declare on which action workflow run Such as push,pull_request ...
branches: #Can on which branches workflow need to run ,and ignored ,...
- main
jobs:
build:
runs-on: ubuntu-latest #Specifies on which runner job running
steps:
- name: Checkout repository
uses: actions/checkout@v2

- name: Print Hello World
run: echo "Hello, World!"

In this example, the workflow is triggered on a push event to the main branch. It consists of a single job named "build," which runs on an Ubuntu environment. The job performs steps such as Checking-out code from repo and execute a command to print "Hello , World!". You can do 🪄 magic's in DevOps CI CD process with this tool.
2. Jobs:
Jobs are units of work within a workflow. They run in parallel by default, but you can also specify dependencies between jobs. Each job can have one or more steps to be executed. A job typically represents a specific task, such as building, testing, or deploying your code.

name: Hello World
on:
push:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
    steps:
    
- name: Checkout repository         uses: actions/checkout@v2
- name: Build job
run: echo "This build job "
test:
    runs-on: ubuntu-latest
    steps:
    
- name: Checkout repository
        uses: actions/checkout@v2
- name: Test job
run: echo "This test job "

In the above Example there is two jobs named build , test a workflow can contain N number of jobs declared according to requirements.
3. Steps:
Steps are individual actions within a job. They represent discrete units of work and can be a shell command or reference an action. Steps execute sequentially, and the success or failure of each step affects the overall job status.
name: Hello World
on:
push:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
    steps:
    
- name: Checkout repository         uses: actions/checkout@v2
- name: Build Job
run: echo "This build job "
test:
    runs-on: ubuntu-latest
    steps:
    
- name: Checkout repository
        uses: actions/checkout@v2
- name: Test job
run: echo "This test job "
In the above Example there are two jobs each has some steps been declared .
They are same likegoing to execute on each job and each meant for specific usage .

4. Actions:
Actions are reusable units of code that encapsulate a specific task. They can be created by the community or by yourself. Actions can be referenced in your workflow files, allowing you to easily incorporate pre-built functionality into your workflows. The GitHub Marketplace offers a wide range of actions for various purposes, such as deploying to cloud platforms, sending notifications, or running code quality checks.

Example Action Usage:
```yaml
steps:
- name: Use a custom action
  uses: actions/checkout@v2
  
- name: Use an action from the Marketplace
  uses: actions/setup-node@v2
  with:
    node-version: '14'
```

In this example, the first step uses the `actions/checkout` action from the GitHub repository. It checks out the repository's code. The second step uses the `actions/setup-node` action from the Marketplace to set up a Node.js environment with version 14.

Let's See a sample example that performs Infra automation -> Dev -> QA ,deploys in environments .


name: Infra automation -> Dev -> QA on: workflow_dispatch: inputs: manual_approval: description: 'Approve the job manually (true -> approve) (false -> cancel) ' required: true env: REGION: us-east-1 jobs: test: runs-on: ubuntu-latest if: ${{ github.event.inputs.manual_approval == 'true' }} steps: - name: checkout code from repo uses: actions/checkout@v3 dev_cdk-diff: runs-on: ubuntu-latest needs: test steps: - name: checkout code from repo uses: actions/checkout@v3 - name: Configure AWS Credentials to Dev uses: aws-actions/configure-aws-credentials@v2 with: role-to-assume: ${{ secrets.AWS_ROLE_ARN_DEV }} aws-region: ${{ env.REGION }} mask-aws-account-id: 'no' if: startsWith(github.ref_name, 'develop') - name: Configure AWS Credentials to QA uses: aws-actions/configure-aws-credentials@v2 with: role-to-assume: ${{ secrets.AWS_ROLE_ARN_QA }} aws-region: ${{ env.REGION }} mask-aws-account-id: 'no' if: startsWith(github.ref_name, 'testing/') - name: cdk diff run: npm run cdk -- diff dev_cdk-deploy: runs-on: ubuntu-latest if: ${{ github.event.inputs.manual_approval == 'true' }} needs: dev_cdk-diff permissions: id-token: write contents: read steps: - name: checkout code from repo uses: actions/checkout@v3 - name: Configure AWS Credentials to Dev uses: aws-actions/configure-aws-credentials@v2 with: role-to-assume: ${{ secrets.AWS_ROLE_ARN_DEV }} aws-region: ${{ env.REGION }} mask-aws-account-id: 'no' if: startsWith(github.ref_name, 'develop') - name: Configure AWS Credentials to QA uses: aws-actions/configure-aws-credentials@v2 with: role-to-assume: ${{ secrets.AWS_ROLE_ARN_QA }} aws-region: ${{ env.REGION }} mask-aws-account-id: 'no' if: startsWith(github.ref_name, 'testing/') - name: cdk deploy run: npm run cdk -- deploy --require-approval never qa_cdk-diff: runs-on: ubuntu-latest needs: test steps: - name: checkout code from repo uses: actions/checkout@v3 - name: Configure AWS Credentials to Dev uses: aws-actions/configure-aws-credentials@v2 with: role-to-assume: ${{ secrets.AWS_ROLE_ARN_DEV }} aws-region: ${{ env.REGION }} mask-aws-account-id: 'no' if: startsWith(github.ref_name, 'develop') - name: Configure AWS Credentials to QA uses: aws-actions/configure-aws-credentials@v2 with: role-to-assume: ${{ secrets.AWS_ROLE_ARN_QA }} aws-region: ${{ env.REGION }} mask-aws-account-id: 'no' if: startsWith(github.ref_name, 'testing/') - name: cdk diff run: npm run cdk -- diff qa_cdk-deploy: runs-on: ubuntu-latest if: ${{ github.event.inputs.manual_approval == 'true' }} needs: qa_cdk-diff permissions: id-token: write contents: read steps: - name: checkout code from repo uses: actions/checkout@v3 - name: Configure AWS Credentials to Dev uses: aws-actions/configure-aws-credentials@v2 with: role-to-assume: ${{ secrets.AWS_ROLE_ARN_DEV }} aws-region: ${{ env.REGION }} mask-aws-account-id: 'no' if: startsWith(github.ref_name, 'develop') - name: Configure AWS Credentials to QA uses: aws-actions/configure-aws-credentials@v2 with: role-to-assume: ${{ secrets.AWS_ROLE_ARN_QA }} aws-region: ${{ env.REGION }} mask-aws-account-id: 'no' if: startsWith(github.ref_name, 'testing/') - name: cdk deploy run: npm run cdk -- deploy --require-approval never
The mentioned above jobs run on automation in  dev and qa environments when approval is
(true -> approve) deploys to QA and DEV based on branch checkout
(false -> cancel) Skips the job

Some references:
https://docs.github.com/en/actions
https://www.youtube.com/playlist?list=PLArH6NjfKsUhvGHrpag7SuPumMzQRhUKY

Conclusion:
GitHub Actions provides a versatile platform for automating your development workflows. By understanding the core components—workflows, jobs, steps, and actions—you can effectively automate tasks, improve collaboration, and ensure consistent practices. With real-world examples, you can begin creating your own workflows and exploring the vast range of actions available in the GitHub Marketplace. Embrace the power of automation with GitHub Actions and take your development process to the next level.

Comments

Popular posts from this blog

Remote Friendly Companies

Introduction to Istio, Kiali, Jaeger, Grafana, and Prometheus