tuutti.iki.fi

Automatically updated Composer repository using GitHub actions and pages

#automation #composer #github-actions

Install and configure Satis

$ composer require composer/satis

Create satis.json:

{
    "name": "your-organization/composer-repository",
    "description": "Repository for your custom libraries",
    "homepage": "https://your-custom-repository.url",
    "include-filename": "all.json",
    "repositories": [
        {
            "packagist.org": false
        },
        {
            "type": "vcs",
            "url": "git@github.com:your-organization/custom-repository.git"
        }
    ]
}

Test it by running php vendor/bin/satis build satis.json public.

Set up GitHub action to build and publish the package repository

Create .github/workflows/build.yml:

name: Build package repository
on:
  push:
  # Allow external repositories to trigger builds
  workflow_dispatch:
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: php-actions/composer@v3

      - name: Configure access token
        run: composer -g config github-oauth.github.com ${{ secrets.GITHUB_TOKEN }}

      - name: Build package repository
        run: vendor/bin/satis build satis.json public

      - name: Deploy
        uses: peaceiris/actions-gh-pages@v3
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          publish_dir: ./public
          user_name: 'github-actions[bot]'
          user_email: 'github-actions[bot]@users.noreply.github.com'

Dispatch re-build events from an external package

The package repository needs to be rebuilt every time you make changes to a package. This can be achieved with something like this:

.github/workflows/update-repository.yml:

on: [push]
name: Update Composer repository
jobs:
  update:
    runs-on: ubuntu-latest
    steps:
      - name: Re-build Composer repository
        uses: benc-uk/workflow-dispatch@v1
        with:
          workflow: Build package repository
          repo: your-organization/composer-repository
          token: ${{ secrets.ACCESS_TOKEN }}
          # Replace this with your Satis repository's default branch.
          ref: "main"

NOTE: The automatically provided token e.g. ${{ secrets.GITHUB_TOKEN }} can not be used, GitHub prevents this token from being able to fire the workflow_dispatch event. The reasons are explained in the docs.