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.

Avatar for Erika Heidi

Erika Heidi

April 26, 2022
Tweet

More Decks by Erika Heidi

Other Decks in Programming

Transcript

  1. 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
  2. GitHub Actions Overview • Common use cases: run tests and

    code quality checks on merge / PR etc • Less common use cases: something like Dynacover
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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?
  10. 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"]
  11. 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'
  12. 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
  13. Creating a Workflow name: Import posts from DEV on: schedule:

    - cron: "0 1 * * *" workflow_dispatch : jobs: main: runs-on: ubuntu-latest steps: (...)
  14. 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 }}
  15. 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