GitHub Action
Let's get the full picture of what GitHub Action is and break it down into several components,
In any software projects, there are repetitive tasks that can be benefited by GitHub Actions. Code reviews, branch management, pull request labelling, regression testing, issue triaging, release management, npm package publication, and cloud deployment are examples of such tasks. These are hard to do manually or consistently.
🍁Workflows
A workflow is an automated🤖 process composed of a series📃 of jobs that gets executed when its triggered by an event. Workflows are defined in YAML files and are stored in a .github/workflows
at the root of the repository. A repository can also have multiple workflows.
🍁Events
An event is anything that can happen in GitHub repository. This includes pushing a code, creating a branch🌿, opening a pull request, and even commenting on an issue. There's a hell lot of triggers, you can have a look👁 here
🍁Jobs
A job is a series📃 of tasks that gets executed in a workflow upon being triggered by an event. Each step is either a script or a Github action. A workflow can have multiple jobs that run in parallel.
🍁Runners
Runners are processes on a server that run the workflow when its triggered. Each runner is responsible for executing a given job. Runners are hosted in the cloud but they can also be self-hosted in custom cloud environments.
🍁Actions
Actions are individual tasks: they are called inside a job. Actions are used to perform complex tasks that you may call multiple times and import into your workflows. Some examples of actions are: installing node packages, hosting the code, running a script, etc.
Building a Workflow file
Let's jump🦘 right into making a workflow and see how it's work,
name: Sample
on:
push:
branches:
- main
schedule: 0 12 * * *
jobs:
print_hello:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- with:
app_name: "hello world!"
api_key: "1212@#!@!@$32323"
- run: |
echo "Hello World!"
echo "Hey!"
The workflow is defined in a YML file inside the workflows
folders📂. Remember the proper indentation.
on:
push:
branches:
- main
schedule: 0 12 * * *
- Inside the
on
section, you have to add the events (push , pull_request
) as subsections. Then, each event is linked to a specific branch🌿 (main , dev
). -
schedule
this will be triggered on 12pm (UTC) each day.
jobs:
print_hello:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- with:
app_name: "hello world!"
api_key: "1212@#!@!@$32323"
- run: |
echo "Hello World!"
echo "Hey!"
- Tasks are defined inside
jobs
section. -
print_hello
is the name of the job. - `
runs-on
, this is where the job will run. Possible values are ubuntu-latest, windows-latest, ubuntu-16.04. There are many more versions. -
steps
contains list📃 of step. If one fails, the next steps are skipped. There are ways to trigger it to. -
uses
use other's actions and publish yours. Hereactions
is the username,checkout
is the repo name.v1
is the tag. -
with
takes the input from outside to act on, you can pass this usingwith
keywords. -
run
, this | enables us to use multiple commands, good for formatting. You can a list of commands to run.
Why to use GitHub actions?
GitHub Actions enable you to use others actions in your workflow which just make your workflow file small and speed💨 up your workflow building steps. Also you can write your own actions and publish it to actions marketplace in GitHub that can be used by others.
Some examples of useful workflows and actions
Now that you've understand all the workings of the GitHub Actions. Let's see some example and go through typical use cases,
🍁Updating latest blog post on README.md of GitHub
If you need to update your blog post on GitHub's README.md
file, you can use this workflow by gautamkrishnar and add it in .github/workflows
directory.
`
name: Latest blog post workflow
on:
schedule:
# Runs every hour
- cron: "0 */1 * * *"
jobs:
update-readme-with-blog:
name: Update this repo's README with latest blog posts
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: gautamkrishnar/blog-post-workflow@master
with:
feed_list: "URL" #type your RSS URL
`
🍁Deploy code on Heroku whenever you push it to GitHub
To deploy your code whenever you push it to GitHub, you can use this workflow to simply your hectic work. There are many more options like using docker etc, you can check it out here
`
name: Deploy
on:
push:
branches:
- master
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: akhileshns/heroku-deploy@v3.6.8 # This is the action
with:
heroku_api_key: "HEROKU_API_KEY"
heroku_app_name: "APP_NAME" #Must be unique in Heroku
heroku_email: "YOUR_EMAIL"
appdir: "FOLDER_NAME"
procfile: "web: npm start"
justlogin: true
working-directory: "FOLDER_DIRECTORY"
run: |
heroku container:login
heroku container:push --recursive -a "APP_NAME"
heroku container:release -a "APP_NAME" web
`
If youre curious about what you can do, you can check the following resources.
References📚:
I hope you have like this small tutorial. Happy coding💗
Always remember, no one reached to the top in one shot. It took them a lot more struggle and hard work than you can imagine. So strive for knowledge, and keep moving forward. Thank you
Top comments (0)