Adapting GitHub action PHPUnit tests for Codeberg/Forgejo
I’ve recently been moving most of my projects from GitHub over to Codeberg and a self-hosted Forgejo instance. A few of my projects make use of GitHub actions to perform automated testing, so I ideally also wanted to transfer over this testing.
Luckily, Forgejo (Which is the underlying project Codeberg uses) has support for GitHub-like actions in a semi-compatible way, using self-hosted runners. I won’t go into detail in setting up the runners since that is well documented by Forgejo, but I thought it might be useful to others to see the changes needed adapt a GitHub PHPUnit actions workflow for Forgejo actions.
Below is an example of the changes made for my asserthtml library:
name: phpunit
on: [push, pull_request]
jobs:
test:
- runs-on: ubuntu-latest
+ runs-on: docker
+ container:
+ image: node:20-bullseye
strategy:
matrix:
php: [8.1, 8.2, 8.3]
steps:
- name: Checkout code
- uses: actions/checkout@v2
+ uses: https://code.forgejo.org/actions/checkout@v4
- name: Cache dependencies
- uses: actions/cache@v2
+ uses: https://code.forgejo.org/actions/cache@v4
with:
path: ~/.composer/cache/files
key: dependencies-php-${{ matrix.php }}-composer-${{ hashFiles('composer.json') }}
restore-keys: |
dependencies-php-${{ matrix.php }}-composer-
dependencies-php-
- name: Setup PHP
- uses: shivammathur/setup-php@v2
+ uses: https://github.com/shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: mbstring
coverage: none
- name: Install dependencies
run: composer install --prefer-dist --no-interaction --no-suggest
+ env:
+ COMPOSER_AUTH: '{"github-oauth": {"github.com": "${{ secrets.GH_TOKEN }}"}}'
- name: Execute tests
run: vendor/bin/phpunit
Changes Made
- Updated
runs-on
to be generic to docker (to match a label of my runner instance) and specified acontainer.image
value to ping the image used for the flow.- The Forgejo docs recommended the
node:20-bullseye
to be a close compatible image forubuntu-22.04
on GitHub.
- The Forgejo docs recommended the
- Updated step
uses
values to be fully absolute paths.- Some I pointed to forgejo action repos, since they maintain mirrors of common GitHub actions.
- For setup-php, I pointed this at the GitHub project since that’s where it exists. This is needed otherwise the runner will attempt to find
shivammathur/setup-php
at it’s own configured base URL. - I updated a few step versions at the same time, that is somewhat irrelevant.
- Added
COMPOSER_AUTH
env option for the composer install step.- I have a personal GitHub token, with no privileges, defined as a
GH_TOKEN
secret in my Codeberg/Forgejo account for this. - This gets around an auth issue:
- By default the
setup-php
will use aGITHUB_TOKEN
env option and configure that for use with composer during setup. - Forgejo provides a
GITHUB_TOKEN
for compatibility and system use, but this is not an real GitHub token so requests to GitHub fail during composer operations (install, update etc…). - By setting a
COMPOSER_AUTH
we can directly override the use ofGITHUB_TOKEN
in composer when used.
- By default the
- There may be a better way about this, or a way to not use GitHub auth when not getting near rate limits, but I already spent too long trying to find a working setup so haven’t explored that more.
- I have a personal GitHub token, with no privileges, defined as a
- I also renamed my
.github
directory to.forgejo
, so it makes better sense on the project’s new home.