Efficiency and Reliability: Optimize Your Github Actions Workflow

Freddy Domínguez
4 min readJan 15, 2023

--

Github Actions can automate, streamline, and optimize development workflows.

Source: https://lubus.in/blog/github-actions-to-release-wordpress-plugin-3656

Github Actions is a powerful tool that allows developers to automate their workflow, from building and testing code to deploying and releasing software. One of the ways to set up and manage workflows in GitHub Actions is by using yaml files. In this article, we will discuss how to create a basic yaml file for Github and how to use it to automate your workflow.

The blog is divided in 4 sections:

  1. How to create a basic YAML file for Github
  2. How to use YAML files to automate your workflow
  3. Best practices for using YAML files in github Actions.
  4. Recommended github actions

Section 1: How to create a basic YAML file for Github

A basic YAML file for Github is called a .github/workflows/main.yml file. It should include the following structure:

name: <workflow_name>
on: <trigger_event>
jobs:
<job_name>:
runs-on: <runner>
steps:
- name: <step_name>
run: <command>
  • name is the name of your workflow, which will be displayed on the Github Actions page.
  • on is the trigger event that will start the workflow, such as push, pull_request, or schedule.
  • jobs is a list of jobs that will run as part of the workflow.
  • runs-on specifies the environment where the job will run, such as ubuntu-latest or windows-latest.
  • steps is a list of steps that will run in the job.
  • name is the name of the step, which will be displayed on the GitHub Actions page.
  • run is the command that will be executed in the step.

For example, let’s say you want to set up a workflow that builds and deploys your code when you push it to the repository. Your YAML file would look like this:

name: Build and Deploy
on: push
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout code
run: git checkout <branch>
- name: Build
run: <command>
- name: Deploy
run: <command>

This “Build and Deploy” workflow runs when user pushes code. The job runs on an ubuntu-latest environment and it has 3 steps, first is to check out the code, then build the code, and finally deploy the code.

Section 2: How to use YAML files to automate your workflow

Once you have created your YAML file, you can use it to automate your workflow in several ways. Here are a few examples:

  • Building and testing code: You could use YAML files to set up a workflow that it autommatically builds and tests your code whenever you push/ merge it to the repository. Therefore, you can ensure the “main” branch whenever point you are really thrust it.
  • Deploying code: You can use YAML files to set up a workflow that automatically deploys your coode to production environment
  • Releasing software: Allows you to send your build to any PAS(plataform As a Service) to be consumed
  • Automating repetitive tasks: You can use YAML files to automate repetitive tasks such as code linting, formatting, documentation generation and resizing images, and so on.

Section 3: Best practices for using YAML files in GitHub Actions

When using YAML files in GitHub Actions, it’sb important to follow best practices to ensure that your workflows are reliable and efficient:

  • Use clear acnd descriptive names for your workflows and jobs. This will make it easier to understand what each workflow does and how it is configured.
  • Test your workflows completely before deploying them to production. This will help everyonce in the team to catch any errors or issues before you noticed them.

Section 4: Recommended Github actions

Here I will present some github actions I usually use in my projects:

jupyter execute notebook.ipynb
  • Use clear and descriptive names for your workflows and jobs. This will make it easier to understand what each workflow does and how it is configured.
  • Use version control for your yaML files. This will allow you to track changes and roll back to previous versions if something goes wrong.
  • Test your workflows thoroughly before deploying them to production. This will help you catch any errors or issues before they can affect your users.
  • Keep your yaml files simple and modular. This would give you an easy path to maintain and update your workflows over time.

Conclusion

Github Actions is a powerful tool that allows developers to automate their workflow and streamline their development process. By following the best practices that I suggest you in this article, you can use YAML files to create workflows to reduce the risk of errors in your daily work.

--

--

Freddy Domínguez

Peruvian #Software #Engineer CIP 206863, #Business #Intelligence #Data #Science. I work with people to create ideas, deliver outcomes, and drive change