GitHub actions example

ifeanyichima

Ifeanyi Chima

Posted on March 18, 2024

GitHub actions example

GitHub actions is a CI/CD tool for GitHub.

  • It is used to automate testing and delivery of our code.
  • GitHub actions respond to any GitHub event; such as push, merge etc
  • We have thousands of pre-built actions from the community.

Image description

This image perfectly sums up GitHub actions.


Creating a GitHub action

You create a GitHub action to automate a specific task.


name: Integration file

on: 
    push: 
        branches: [main]
    pull_request: 
        branches: [main]

jobs:
    api_build:
        runs-on: ubuntu-latest

        steps:
            - name: Checkout
              uses: actions/checkout@v2
            - name: Setup node
              uses: actions/setup-node@v2
              with:
                node-version: 20
                cache: "npm"
            - name: Install dependencies
              run: npm ci        
Enter fullscreen mode Exit fullscreen mode

Undestanding the syntax

workflow: We create a workflow file which is written in yaml syntax in the project directory .github/workflows/actions.yml we have given the actions a name - integration file

events: We tell GitHub when this action would run, everytime we do a push to the main branch, this action would be executed. (the trigger is a push to the main branch).

jobs: This is the property that tells GitHub actions, what to execute when the trigger condition is met. The name of the job is api_build. You can have multiple jobs in one workflow file.

runs-on: You define the environment, where the action would be run. [ubuntu-latest]: this means the latest docker image of the ubuntu OS that GitHub actions has available.

steps: These are the steps we need to take in order to run the job correctly. This contains actions, we give each step a name, we have multiple steps. (You can have multiple steps).

actions: These are the individual tasks. We have 3 pre-built actions by members of the GitHub community in our file.

  • Checkout: This will git checkout main branch. We are using pre-built actions.
  • Setup node: This will install node @v20 on ubuntu OS. We are using pre-built actions.
  • Install dependencies

Buy Me A Coffee

Thank you, Please follow me

HTML GitHub

💖 💪 🙅 🚩
ifeanyichima
Ifeanyi Chima

Posted on March 18, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

GitHub actions example
javascript GitHub actions example

March 18, 2024