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.
A workflow is an automated🤖 process composed of a series📃 of jobs that gets executed when it’s 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.
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
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 are processes on a server that run the workflow when it’s 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 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
onsection, you have to add the events (
push , pull_request) as subsections. Then, each event is linked to a specific branch🌿 (
main , dev).
schedulethis 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
print_hellois 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.
stepscontains list📃 of step. If one fails, the next steps are skipped. There are ways to trigger it to.
usesuse other's actions and publish yours. Here
actionsis the username,
checkoutis the repo name.
v1is the tag.
withtakes the input from outside to act on, you can pass this using
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 other’s 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
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: firstname.lastname@example.org # 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 you’re curious about what you can do, you can check the following resources.
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
Did you find this article valuable?
Support Sagar Medtiya by becoming a sponsor. Any amount is appreciated!