Files
FOSSBilling/.github/workflows/ci.yml
T
Adam Daley 5f5a6e5a6a Refactor CI with build-push-action and Docker caching (#3545)
* Switch CI to build-push-action and add GHCR caching

* Set YAML `indent_size` to 2 in .editorconfig

* CI: Reuse Docker steps

* Update docker/login-action to v4

* Upload preview Docker image artifact in CI

* Add reusable docker-build workflow
2026-05-14 22:29:17 +01:00

258 lines
8.7 KiB
YAML

name: CI
permissions:
contents: read
on:
push:
pull_request:
branches: main
types: [opened, reopened, synchronize, labeled, unlabeled]
pull_request_target:
branches: main
types: [opened, reopened, synchronize]
concurrency:
group: ${{ github.workflow }}-${{ github.event_name }}-${{ github.event_name == 'pull_request' && (github.event.action == 'labeled' || github.event.action == 'unlabeled') && 'labels' || 'run' }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true
jobs:
merge-hold:
name: 'Check for Merge Hold'
if: ${{ github.event_name == 'pull_request' }}
runs-on: ubuntu-latest
timeout-minutes: 5
steps:
- name: 'Check merge-hold label'
run: |
if [[ "${{ contains(github.event.pull_request.labels.*.name, 'merge-hold') }}" == 'true' ]]; then
echo "Pull request labeled 'merge-hold'"
echo "The pull request cannot be merged until the 'merge-hold' label is removed."
exit 1
fi
echo "Pull request not labeled 'merge-hold'"
echo "The pull request can be merged as no 'merge-hold' label is set."
labeler:
name: 'Pull Request Labeler'
permissions:
contents: read
pull-requests: write
if: ${{ github.event_name == 'pull_request_target' }}
runs-on: ubuntu-latest
timeout-minutes: 5
steps:
- uses: actions/labeler@f27b608878404679385c85cfa523b85ccb86e213 # v6
with:
repo-token: '${{ secrets.GITHUB_TOKEN }}'
sync-labels: true
release-drafter:
name: 'Release Drafter'
permissions:
contents: write
pull-requests: read
if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' }}
runs-on: ubuntu-latest
timeout-minutes: 5
steps:
- uses: release-drafter/release-drafter@c2e2804cc59f45f57076a99af580d0fedb697927 # v7
with:
config-name: release-drafter.yml
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
spellcheck:
name: 'Spellcheck'
runs-on: ubuntu-latest
timeout-minutes: 10
if: &normal_ci ${{ (github.event_name == 'pull_request' && github.event.action != 'labeled' && github.event.action != 'unlabeled' && github.event.pull_request.base.repo.id != github.event.pull_request.head.repo.id) || github.event_name == 'push' }}
steps:
- &checkout_step
name: 'Checkout Repository'
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6
- name: 'Perform Spellcheck'
uses: streetsidesoftware/cspell-action@de2a73e963e7443969755b648a1008f77033c5b2 # v8
with:
incremental_files_only: true
php-build-test:
name: 'PHP Build and Test (${{ matrix.php }})'
permissions:
contents: read
packages: write
needs: [ spellcheck ]
if: *normal_ci
runs-on: ubuntu-latest
timeout-minutes: 60
strategy:
matrix:
php: ['8.3', '8.4', '8.5']
fail-fast: true
steps:
- *checkout_step
- &lowercase_repo
name: 'Lowercase github.repository'
run: |
echo "IMAGE_NAME=${GITHUB_REPOSITORY@L}" >> ${GITHUB_ENV}
- &login_ghcr
name: 'Login to GitHub Container Registry'
uses: docker/login-action@4907a6ddec9925e35a0a9e82d7399ccc52663121 # v4
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- &setup_buildx
name: 'Set up Docker Buildx'
uses: docker/setup-buildx-action@4d04d5d9486b7bd6fa91e7baf45bbb4f8b9deedd # v4
- name: 'Build Docker Test Image'
uses: docker/build-push-action@bcafcacb16a39f128d818304e6c9c0c18556b85f # v7
with:
build-args: |
PHP_VERSION=${{ matrix.php }}
INSTALL_TRANSLATIONS=false
cache-from: type=registry,ref=ghcr.io/${{ env.IMAGE_NAME }}:cache-ci-${{ matrix.php }}
cache-to: type=registry,ref=ghcr.io/${{ env.IMAGE_NAME }}:cache-ci-${{ matrix.php }},mode=max
context: .
load: true
tags: |
fossbilling:test-${{ matrix.php }}
target: test
- name: 'PHPStan'
run: |
docker run --rm \
--env APP_ENV=test \
fossbilling:test-${{ matrix.php }} \
./src/vendor/bin/phpstan analyse --configuration phpstan.neon --memory-limit=512M
- name: 'PHPUnit'
run: |
docker run --rm \
--env APP_ENV=test \
fossbilling:test-${{ matrix.php }} \
./src/vendor/bin/phpunit --configuration phpunit.xml.dist
- name: 'Run Live Tests'
if: ${{ matrix.php == '8.5' }}
run: bash ./.github/scripts/run-docker-live-tests.sh fossbilling:test-${{ matrix.php }}
preview-build:
name: 'Create Preview Build'
permissions:
contents: read
packages: write
needs: [ php-build-test ]
if: ${{ (github.event_name == 'pull_request' && github.event.action != 'labeled' && github.event.action != 'unlabeled' && github.event.pull_request.base.repo.id != github.event.pull_request.head.repo.id) || (github.event_name == 'push' && github.ref != 'refs/heads/main') }}
runs-on: ubuntu-latest
timeout-minutes: 30
steps:
- *checkout_step
- *lowercase_repo
- *login_ghcr
- *setup_buildx
- name: 'Build Release Artifact'
uses: docker/build-push-action@bcafcacb16a39f128d818304e6c9c0c18556b85f # v7
with:
build-args: |
FOSSBILLING_VERSION=${{ github.sha }}
FOSSBILLING_VERSION_TRUNCATE=7
SENTRY_DSN=${{ vars.SENTRY_DSN }}
cache-from: type=registry,ref=ghcr.io/${{ env.IMAGE_NAME }}:cache-preview
cache-to: type=registry,ref=ghcr.io/${{ env.IMAGE_NAME }}:cache-preview,mode=max
context: .
target: release-artifact
outputs: type=local,dest=./release-tree
- name: 'Create Preview Build Archive'
run: tar -cf /tmp/build.tar -C ./release-tree ./
- name: 'Upload Preview Build Archive'
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7
with:
if-no-files-found: 'error'
name: preview-build
path: /tmp/build.tar
retention-days: 1
preview-docker:
name: 'Build and Push Preview Docker Image'
permissions:
contents: read
packages: write
needs: [ php-build-test ]
if: ${{ github.ref == 'refs/heads/main' && github.event_name == 'push' }}
uses: ./.github/workflows/docker-build.yml
secrets:
DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}
DOCKERHUB_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }}
with:
target: runtime
cache-scope: cache-preview
extract-release-tree: true
release-tree-artifact-name: preview-build
meta-images: fossbilling/fossbilling
meta-tags: |
type=raw,value=preview
meta-flavor: |
latest=false
build-args: |
FOSSBILLING_VERSION=${{ github.sha }}
FOSSBILLING_VERSION_TRUNCATE=7
SENTRY_DSN=${{ vars.SENTRY_DSN }}
upload-preview:
name: 'Upload Preview Release'
needs: [ preview-build, preview-docker ]
if: ${{ !cancelled() && (needs.preview-build.result == 'success' || needs.preview-docker.result == 'success') }}
runs-on: ubuntu-latest
timeout-minutes: 15
steps:
- name: 'Download Preview Release Archive'
uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8
with:
name: preview-build
path: '/tmp'
- name: 'Extract Preview Release Archive'
run: |
mkdir ./src
tar -xf /tmp/build.tar -C ./src
- name: 'Create Downloadable Preview'
run: zip -qr ../FOSSBilling-preview.zip .
working-directory: ./src/
- name: 'Move Downloadable Preview for Upload'
run: |
mkdir ./dist
mv ./FOSSBilling-preview.zip ./dist
- name: 'Upload Downloadable Preview to S3'
if: ${{ github.ref == 'refs/heads/main' }}
uses: jakejarvis/s3-sync-action@7ed8b112447abb09f1da74f3466e4194fc7a6311 # master
with:
args: '--acl public-read --follow-symlinks'
env:
AWS_S3_BUCKET: ${{ secrets.AWS_S3_BUCKET }}
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_S3_ENDPOINT: ${{ secrets.AWS_S3_ENDPOINT }}
AWS_REGION: 'auto'
SOURCE_DIR: './dist'
- name: 'Upload Downloadable Preview to GitHub'
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7
with:
if-no-files-found: 'error'
name: 'FOSSBilling Preview'
path: './src/*'
retention-days: 14