Pranjalmishra30 / GithubActions

To learn and test GitHub actions

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

GithubActions

Github actions allow us to automate, build and deploy customized workflows.
This repository documents my findings from testing and learning about Github-Actions

Github actions scripts are saved as YAML files and stored in .github/workflows directory.

You can check out the results in the actions tab.

Index

  1. Commonly used Github Actions
  2. Customized workflow
  3. References

Commonly used Github Actions

name: Create branch  
on: [push]  
jobs:  
  create-branch:
    runs-on: ubuntu-latest  
    steps:  
      - uses: peterjgrainger/action-create-branch@v2.2.0
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          branch: Created-Actions
          sha: ${{ github.event.pull_request.head.sha }} 
name: Delete branch  
on: [push]  
jobs:  
  delete-branch:
    runs-on: ubuntu-latest  
    steps:  
      - uses: dawidd6/action-delete-branch@v3
        with:
          github_token: ${{github.token}}
          branches: Sept-2022
name: Merge branch  
on: [push]  
jobs:  
  merge-branch:
    runs-on: ubuntu-latest  
    steps:  
      - uses: actions/checkout@v3
      - uses: everlytic/branch-merge@1.1.2
        with:
          github_token: ${{ github.token }}
          source_ref: ${{ github.ref }}
          target_branch: 'main'
          commit_message_template: '[Automated] Merged {source_ref} into target {target_branch}'

Execute a shell script

Replace with your script name

name: Execute script  
on: [push]  
jobs:  
  exec-script:
    runs-on: ubuntu-latest  
    steps:  
      - uses: actions/checkout@v2
      - run: |
          chmod +x script.sh
          ./script.sh
        shell: bash 

Schedule-task

name: Schedule Event every 5 mins
on:
  schedule:
    - cron: '*/5 * * * *'
jobs:
  test-schedule:
    runs-on: ubuntu-latest
    steps:
      - run: echo "Job has run at scheduled time"

cron statements are used to give schedule for tasks to execute. The time format used is UTC, so convert your local time to UTC. Use Crontab to build your custom schedule statements. Checkout the documentation.
There is no guarantee that the scheduled tasks will run at the desired time. There is generally a delay. Checkout this blog post to understand this issue better.

name: Email action
on: [push]  
jobs:  
  send-email:  
    runs-on: ubuntu-latest  
    steps:
      - name: send mail  
        uses: dawidd6/action-send-mail@v3
        with:
          server_address: smtp.gmail.com
          server_port: 465
          username: ${{secrets.MAIL_USERNAME}}
          password: ${{secrets.MAIL_PASSWORD}}
          subject: Email sent via Github actions
          to: pranjalmishra2022@gmail.com,bunumishu@gmail.com
          from: Github action bot
          secure: true
          body: Email sent by actions !!!

It's advised to use an app password for sending emails. Checkout this guide
Email username and password need to saved as secrets for your repository

Customized Workflow

Using individual scripts to build an action for a customized workflow.

The Problem:
Imagine a repository which has 2 branches, main and a month branch. The main branch has all tested codes whereas the month branch is where all trials and experiments happen.

At the end of each month, all changes in the month branch get merged into main. The old branch is deleted and a new branch for next month is created. The readme is also updated with date of merging. Branch name convention: "Month-Year" (July-2022). An automated email should be sent to all contributors, notifying them of the change.

This workflow needs to be automated with the help of github actions.

Solution:

This workflow is scheduled to run at 1:00 AM IST on the 1st day of every month.

Workflow file:

name: Branch Create and Delete workflow
on:
  schedule:
    - cron: '30 7 1 * *'
jobs:
  mrg-dlt-crt:
    runs-on: ubuntu-latest
    steps:

      # Prev and current month names
      - run: echo "Curr=$(date "+%B-%Y")" >> $GITHUB_ENV
      - run: echo "Prev=$(date -d "$(date +%Y-%m-1) -1 month" +%B-%Y)" >> $GITHUB_ENV
      
      # name: Merge prev month branch
      - uses: everlytic/branch-merge@1.1.2
        with:
          github_token: ${{ github.token }}
          source_ref: ${{ env.Prev }}
          target_branch: 'main'
          commit_message_template: '[Automated] Merged {source_ref} into main branch'
      
      # name: Delete old branch
      - uses: dawidd6/action-delete-branch@v3
        with:
          github_token: ${{github.token}}
          branches: ${{ env.Prev }}
      
      # name: Update README
      - uses: actions/checkout@v2
      - run: |
          chmod +x update.sh
          ./update.sh
        shell: bash 

      # name: commit and push
      - run: |
          git config --global user.email "pranjalmishra2022@gmail.com"
          git config --global user.name "Pranjal"
          git pull
          git add -A
          git commit -m "edited readme"
          git push 
      
      - uses: actions/checkout@v2

      # Create branch and Email TO BE FIXED
      # - uses: peterjgrainger/action-create-branch@v2.2.0
      #   env:
      #     GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      #   with:
      #     branch: ${{ env.Curr }}
      #     sha: '${{ github.event.pull_request.head.sha }}'

Update README script

#!/bin/bash
txt=$(date "+%d %B %Y")
sed -i "s/main:.*/main: $txt/" README.md 

REFERENCES

  1. Github Actions quickstart
  2. Issue with inbuilt cron scheduler
  3. Schedule jobs not running on time (github community)

About

To learn and test GitHub actions


Languages

Language:Shell 100.0%