Date
3 min read

Automate workflow using GitHub Actions

Table of contents:

What are GitHub Actions?

GitHub Actions is a powerful automation deployment platform provided by GitHub. It allows developers to define ‘workflows’ and automate various tasks related directly within GitHub repositories. These workflows can be triggered by events such as code pushes, pull requests, issues, and more.

Key aspects of GitHub Actions:

Workflows:

GitHub Actions is centered around workflows, which are sets of automated steps that can be triggered by specific events. Workflows are defined in YAML files and can include a variety of actions and jobs to perform tasks like building, testing, and deploying.

Actions:

Actions are the individual units of work within a workflow. They can be created by GitHub or by the community and are reusable pieces of automation. GitHub provides a marketplace where you can find pre-built actions for common tasks, or you can create your own custom actions.

Importance of setting up a workflow:

Running a GitHub Action workflow after deploying to a branch or making a new pull request serves a crucial purpose. It helps ensure code quality, reliability, and efficient collaboration.

Purpose:

An example of use is to run a GitHub Action workflow after deploying to a branch or creating a pull request automate various tasks such as building, testing, and deploying code changes.

Their primary purposes include:

Quality Assurance: Automated testing ensures that new code changes do not introduce bugs or regressions. This maintains the overall quality and stability of the software.

Consistency: Workflow automation enforces consistent development practices, making it easier to catch issues early and maintain a reliable codebase.

Efficiency: Automated workflows save time by reducing manual tasks. Developers can focus on coding, while actions handle repetitive, time-consuming operations.

Example use using Laravel:

The below should be fairly straight forward to understand, however, each step within the below YAML file is a reflection of an action being performed i.e.

  1. Copy .env
  2. Install Dependencies
  3. Generate key
  4. Directory Permissions
  5. Create Database
  6. Execute tests (Unit and Feature tests) via PHPUnit
name: Master

on:
  push:
    branches: [ "master" ]
  pull_request:
    branches: [ "master" ]

jobs:
  laravel-tests:

    runs-on: ubuntu-latest

    steps:
    - uses: shivammathur/setup-php@15c43e89cdef867065b0213be354c2841860869e
      with:
        php-version: '8.1'
    - uses: actions/checkout@v3
    - name: Copy .env
      run: php -r "file_exists('.env') || copy('.env.pipeline', '.env');"
    - name: Install Dependencies
      run: composer install --ignore-platform-reqs -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist
    - name: Generate key
      run: php artisan key:generate
    - name: Directory Permissions
      run: chmod -R 777 storage bootstrap/cache
    - name: Create Database
      run: |
        mkdir -p database
        touch database/database.sqlite
    - name: Execute tests (Unit and Feature tests) via PHPUnit
      env:
        DB_CONNECTION: sqlite
        DB_DATABASE: database/database.sqlite
      run: vendor/bin/phpunit
.git
.github
- workflows
- - develop.yml
- - master.yml
- - staging.yml
app
bootstrap
config
database
public
resources
routes
storage
tests
.env
.env.example

If all goes well then we get presented with this:

Screenshot of valid GitHub Workflow

You might also like...

  • Read article

    Import your project from different Git providers

    Switch Git providers effortlessly - move your project without losing history.

Let's work together 🤝

Line
Christopher Kelker

Chriscreates