Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Building GitHub Actions in PHP with Minicli

Building GitHub Actions in PHP with Minicli

In this talk for Laracon EU 2022, I walk through what GitHub Actions are and how you can create your own Actions using Minicli, a microframework for the command line in PHP.

Erika Heidi

April 26, 2022
Tweet

More Decks by Erika Heidi

Other Decks in Programming

Transcript

  1. Erika Heidi @ Laracon EU 2022

    View full-size slide

  2. 1. Github Actions in a Nutshell

    View full-size slide

  3. GitHub Actions Overview
    ● Feature that allows GitHub users to create
    automated pipelines that can be triggered by
    events or run on schedule
    ● Popular for CI/CD
    ● Open source repos get unlimited action runs for
    free
    ● The GitHub Marketplace has a large collection of
    ready-to-use Actions contributed by the
    community

    View full-size slide

  4. GitHub Actions Overview
    ● Common use cases: run tests and code
    quality checks on merge / PR etc
    ● Less common use cases: something like
    Dynacover

    View full-size slide

  5. The GitHub Actions Ecosystem
    ● Actions
    ● Workflows
    ● Triggers
    ● Action Secrets

    View full-size slide

  6. Actions
    ● Self-contained units of execution that can be
    combined in workflows
    ● Actions live in dedicated repositories with an
    action.yml file that defines its configuration
    ● You can combine actions from the
    marketplace with your own custom actions
    ● Actions must be packaged as Docker images

    View full-size slide

  7. Workflows
    ● A configurable process that runs one or more
    jobs
    ● Each job can have multiple steps
    ● Steps can be either an Action or a shell script
    ● Steps run in order and share data
    ● Jobs run in parallel unless otherwise specified
    ● Defined in a YAML file committed to the
    repository
    ● You can have several different workflows in the
    same repository

    View full-size slide

  8. Workflow Example: PHP Composer
    name: PHP Composer
    on:
    push:
    branches: [ main ]
    jobs:
    build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - name: Install dependencies
    run: composer install --prefer-dist --no-progress
    - name: Run test suite
    run: composer test

    View full-size slide

  9. Triggers
    ● Workflows can be triggered by events such as
    new commits, new issues, when a PR is
    created, etc
    ● Workflows can also run on schedule, similar to
    cron jobs
    ● When on schedule, the minimal interval is five
    minutes

    View full-size slide

  10. Action Secrets
    ● Encrypted environment variables
    accessible only by GitHub Actions
    ● Can be set at repository level or per
    environment
    ● Ideal for storing API keys and auth
    tokens

    View full-size slide

  11. 2. Minicli Overview &
    The Demo App

    View full-size slide

  12. Minicli Overview
    ● Microframework for CLI PHP apps
    ● Simple command architecture
    ● Commands defined as callback (single-file
    app) or controllers (structured app)
    ● Dependency-free (except for tests)
    ● Ideal for small cron jobs, bots, simple
    workers

    View full-size slide

  13. The Demo app

    View full-size slide

  14. 3. Turning a Minicli app into a
    GitHub Action

    View full-size slide

  15. Preparation
    ● What will trigger this action?
    ● Do I need to combine this action with others?
    ● Do I need to use repository secrets or environment variables to configure my
    action?
    ● If my action creates output that needs to be persisted, where this content goes?

    View full-size slide

  16. Packaging Minicli with Docker
    FROM php:8.1-cli
    RUN apt-get update && apt-get install -y git curl libxml2-dev zip unzip
    RUN apt-get clean && rm -rf /var/lib/apt/lists/*
    COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
    RUN mkdir /application
    COPY . /application/
    RUN cd /application && composer install
    ENTRYPOINT [ "php", "/application/minicli" ]
    CMD ["import", "dev"]

    View full-size slide

  17. The action.yml file
    name: 'Import DEV.to posts'
    description: 'Imports posts from DEV.to as markdown files'
    outputs:
    response:
    description: 'Output from command'
    runs:
    using: 'docker'
    image: 'Dockerfile'

    View full-size slide

  18. Tagging your action
    git add .
    git commit -m "My first action is ready"
    git tag -a -m "My first action release" v1
    git push --follow-tags

    View full-size slide

  19. Creating a Workflow
    name: Import posts from DEV
    on:
    schedule:
    - cron: "0 1 * * *"
    workflow_dispatch
    :
    jobs:
    main:
    runs-on: ubuntu-latest
    steps:
    (...)

    View full-size slide

  20. Creating a Workflow: Steps
    - uses: actions/checkout@v2
    - uses: erikaheidi/[email protected]
    name: "Import posts from DEV"
    env:
    DEVTO_USERNAME: erikaheidi
    APP_DATA_DIR: ${{ github.workspace }}/devto
    - name: Create a PR
    uses: peter-evans/create-pull-request@v3
    with:
    commit-message: Import posts from DEV
    title: "[automated] Import posts from DEV"
    token: ${{ secrets.GITHUB_TOKEN }}

    View full-size slide

  21. Complete workflow for reference

    View full-size slide

  22. Testing the workflow

    View full-size slide

  23. References / Useful Links
    ● ImportDevTo Action Repository
    ○ https://github.com/erikaheidi/importDevTo
    ● How to Create a GitHub Action to Import Posts from DEV to a Repository you Own (tutorial)
    ○ https://bit.ly/38nANwc
    ● GitHub Actions documentation
    ○ https://docs.github.com/en/actions
    ● Minicli documentation
    ○ https://docs.minicli.dev
    ● Other actions built with Minicli
    ○ Dynacover: https://github.com/erikaheidi/dynacover-actions
    ○ Update CONTRIBUTORS: https://github.com/minicli/action-contributors

    View full-size slide

  24. Thank You!
    @erikaheidi

    View full-size slide