Create a basic github action

Key takeaways

  • GitHub actions allows us to create workflows to automate tasks directly within the github repositories.

  • Workflows are defined in YAML files located in the .github/workflows directory of your repository.

  • We can set specific events (like push or pull_request) that trigger the workflow, enabling automation based on changes to your codebase.

  • Workflows consist of jobs and each job contains steps that execute commands or actions, allowing us to automate tasks such as building, testing, or deploying our application.

  • We can choose different runner environments (e.g., ubuntu-latest, windows-latest) to match the requirements of the project, ensuring compatibility and optimal performance.

GitHub provides a powerful automation tool named github Actions that has the capability to run workflows directly within our custom github repositories. It allows us to create workflows to automate tasks directly within the github repositories. These workflows are most commonly used for automation purposes, such as testing, continuous integration, and continuous deployment (CI/CD).

How github Actions work

GitHub Actions are defined in YAML files stored in the .github/workflows directory of the repository. Each workflow consists of a series of steps that are executed in response to specific events, known as triggers. The common triggers we encounter will be through push and pull requests, or scheduled events. As the trigger occurs, the associated workflow is executed and the steps will be performed.

Requirements

We will need a github account, a personal access token, and an empty repository. Details about acquiring a While generating a personal access token token, select the repo and the workflow scopes.

Example for github Actions workflow

Workflows live in the .github/workflows directory. For the purposes of our example, we're going to create a file called first.yaml which will hold our first workflow. The contents of the first.yaml file will include specific steps that the workflow will execute. These steps could involve checking out code, running scripts, or performing various automated tasks.

The contents of this file are shown below:

name: our-first-workflow 
on: push
jobs:
  echo:
    runs-on: ubuntu-latest
    steps:
      - name: echo step
        run: echo 'hello world!'
The code for our first workflow

Let's understand our first workflow in detail:

  • name: specifies the name of the workflow

  • on: This section defines the event that will trigger the workflow. In this case, the workflow is set to run on any push event to the repository. This means that whenever we push changes to any branch in the repository, this workflow will be executed.

  • jobs: This section defines a job named echo. Jobs are a collection of steps that run in the same environment. We can have multiple jobs defined here.

  • runs-on: ubuntu-latest: This specifies the environment where the job will execute. ubuntu-latest means that the job will run on the latest version of the Ubuntu operating system provided by github Actions.

  • steps: This section lists the individual tasks that the job will execute.

  • name: This step is named echo step.

  • run: The run keyword allows us to specify shell commands that will be executed. In this case, it uses the echo command, which prints the provided string to the output log of the workflow.

Run the following commands in the above terminal to trigger the workflow:

cd usercode
git config --global user.name github_user
git config --global user.email github_email
git init && git remote add origin github_repo
git remote -v
git add .
git commit -am 'Our First Workflow'
git branch main
git remote set-url origin https://<username>:<personal-access-token>@github.com/<username>/<repository>.git
git push origin main

You will have to fill the commands with your own values, as follows:

  • github_email: Our github account's email.

  • github_user: Our github account's username.

  • github_repo: The link to our empty github repository.

After running these commands, redirect to the github repository and click on the Actions tab. Here we should see that our workflow has executed:

Our workflow being successfully executed
Our workflow being successfully executed

This workflow is a straightforward example of how to create a basic github action. It triggers on every push to the repository and runs a single job that echoes "hello world!" to the console.

Conclusion

GitHub Actions provide an effective way to automate workflows within the repositories.

By defining workflows in YAML, we can streamline our development process and implement CI/CD practices. This simple example serves as a foundation for more complex workflows, enabling us to integrate testing, deployment, and other automation tasks into your projects.

Frequently asked questions

Haven’t found what you were looking for? Contact Us


Can I learn github without coding?

Yes, we can learn github without coding. We can focus on understanding version control concepts, repository management, collaboration features, and using github for project management.


How do I include comments in my YAML file?

We can include comments in a YAML file by using the # symbol.


How can I cancel a workflow?

To cancel a running workflow, go to the “Actions” tab of your repository, select the workflow you want to see, and click the name of the queued or in progress run that you want to cancel from the list of workflow runs. In the upper-right corner of the workflow, click “Cancel workflow”.


Free Resources

Copyright ©2025 Educative, Inc. All rights reserved