Skip to content

Commit

Permalink
build: reuse workflows (#477)
Browse files Browse the repository at this point in the history
  • Loading branch information
asbiin authored Dec 29, 2023
1 parent 44f5d67 commit 17ca1c7
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 367 deletions.
192 changes: 11 additions & 181 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,195 +16,25 @@ concurrency:
group: Build ${{ github.ref }}
cancel-in-progress: true


jobs:
#############
# Run tests
#############
tests:
runs-on: ubuntu-latest
name: Testsuite ${{ matrix.testsuite }} with PHP ${{ matrix.php-version }} + ${{ matrix.connection }}
uses: monicahq/workflows/.github/workflows/laravel.yml@main
with:
php-version: ${{ matrix.php-version }}
connection: ${{ matrix.connection }}
coverage: ${{ matrix.connection == 'sqlite' && matrix.php-version == '8.2' }}
database_name: customers

strategy:
fail-fast: false
matrix:
php-version: ['8.2']
connection: [sqlite, mysql, pgsql]
testsuite: [Unit]
coverage: [true] # run test with coverage, if 'coverage-with' match with the connection

steps:
- name: Checkout sources
uses: actions/checkout@v4

- name: Setup PHP ${{ matrix.php-version }}
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-version }}
extensions: mbstring, dom, fileinfo, ${{ matrix.connection }}
coverage: pcov
ini-values: pcov.directory=., pcov.exclude="~vendor~"
- name: Check PHP Version
run: php -v
- name: Check Composer Version
run: composer -V
- name: Check PHP Extensions
run: php -m

# Composer
- name: Validate composer.json and composer.lock
run: composer validate

- name: Get Composer Cache Directory
id: composer-cache
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT
- name: Cache composer files
uses: actions/cache@v3
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ matrix.php-version }}-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-composer-${{ matrix.php-version }}-${{ hashFiles('**/composer.lock') }}
${{ runner.os }}-composer-${{ matrix.php-version }}
${{ runner.os }}-composer-
- name: Install composer dependencies
run: composer install --no-progress --no-interaction --prefer-dist --optimize-autoloader

# Prepare
- name: Prepare environment
run: |
cp tests/.env.ci-${{ matrix.connection }} .env
touch config/.version config/.release
mkdir -p public/build/assets
{\
echo "{"; \
for f in app.js app.css; do \
[[ $first == 1 ]] && echo -n "," || first=1; \
k=${f##*.}/$f; \
echo "\"resources/$k\": {\"file\": \"assets/$f\", \"src\": \"resources/$k\"}"; \
echo '' > public/build/assets/$f; \
done; \
echo "}"; \
} | tee public/build/manifest.json
- name: Create sqlite database
if: matrix.connection == 'sqlite'
run: touch database/database.sqlite
- name: Create mysql database
if: matrix.connection == 'mysql'
run: |
sudo systemctl start mysql.service
mysql --protocol=tcp -u root -proot -e "CREATE DATABASE IF NOT EXISTS customers CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"
- name: Create pgsql database
if: matrix.connection == 'pgsql'
run: |
sudo systemctl start postgresql.service
sudo -u postgres psql --command="CREATE USER customers PASSWORD 'secret'" --command="\du"
sudo -u postgres createdb --owner=customers customers
PGPASSWORD=secret psql --username=customers --host=localhost --list customers
- name: Generate key
run: php artisan key:generate

- name: Run migrations
run: php artisan migrate --no-interaction -vvv
- name: Run seeds
run: php artisan db:seed --no-interaction -vvv

# Test
- name: Run tests suite with coverage
if: matrix.connection == env.coverage-with && matrix.php-version == env.default-php-version && matrix.coverage
run: vendor/bin/phpunit -c phpunit.xml --testsuite ${{ matrix.testsuite }} --log-junit ./results/${{ matrix.connection }}/junit/results${{ matrix.testsuite }}.xml --coverage-clover ./results/${{ matrix.connection }}/coverage/coverage${{ matrix.testsuite }}.xml
env:
DB_CONNECTION: ${{ matrix.connection }}
- name: Run tests
if: matrix.connection != env.coverage-with || matrix.php-version != env.default-php-version || ! matrix.coverage
run: vendor/bin/phpunit -c phpunit.xml --testsuite ${{ matrix.testsuite }} --log-junit ./results/${{ matrix.connection }}/junit/results${{ matrix.testsuite }}.xml
env:
DB_CONNECTION: ${{ matrix.connection }}

- name: Fix results files
run: sed -i -e "s%$GITHUB_WORKSPACE/%%g" **/*.xml
working-directory: results/${{ matrix.connection }}
if: success() || failure()

- name: Store results
if: matrix.php-version == env.default-php-version && (success() || failure())
uses: actions/upload-artifact@v4
with:
name: results-${{ matrix.php-version }}-${{ matrix.connection }}
path: results


###########################
# Reporting to sonarcloud
###########################
sonarcloud:
needs: tests
runs-on: ubuntu-latest
name: Sonarcloud
if: ${{ ! startsWith(github.ref, 'dependabot/') && (success() || failure()) }}

steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis

- name: Download results
uses: actions/download-artifact@v4
with:
pattern: results-*
path: results
merge-multiple: true

- name: Merge junit files
run: |
yarn dlx junit-merge --recursive --dir results/${{ env.coverage-with }}/junit --out results/results.xml
- name: Set version parameter
id: version
run: |
version=$(git tag --points-at HEAD)
test -z "$version" && version="main"
echo "value=$version" >> $GITHUB_OUTPUT
- name: Set coverage list
id: coverage
run: |
SONAR_COVERAGE=$(ls -m --format=comma results/${{ env.coverage-with }}/coverage/coverage*.xml | sed -e ':a;N;$!ba;s/\n//g; s/ //g;')
echo "list=$SONAR_COVERAGE" >> $GITHUB_OUTPUT
- name: SonarCloud Scan
if: env.SONAR_TOKEN != ''
uses: SonarSource/sonarcloud-github-action@v2.1.1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
with:
args: |
-Dsonar.projectVersion=${{ steps.version.outputs.value }}
-Dsonar.php.tests.reportPath=./results/results.xml
-Dsonar.php.coverage.reportPaths=${{ steps.coverage.outputs.list }}
###########################
# JUnit Test Report
###########################
reporting:
uses: monicahq/workflows/.github/workflows/reporting.yml@main
with:
project: monicahq_customers
secrets:
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
needs: tests
runs-on: ubuntu-latest
name: Reporting
if: success() || failure()

steps:
- name: Download results
uses: actions/download-artifact@v4
with:
pattern: results-*
path: results
merge-multiple: true

- name: Publish Test Report
uses: mikepenz/action-junit-report@v4
with:
report_paths: results/${{ env.coverage-with }}/junit/*.xml
124 changes: 6 additions & 118 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,136 +3,23 @@ name: Deploy
on:
push:
branches: [main]
pull_request:
types: [opened, synchronize, reopened]
release:
types: [created]

workflow_dispatch:

env:
php-version: '8.2'
node-version: 18

concurrency:
group: Deploy ${{ github.ref }}
cancel-in-progress: true


jobs:
#############
# Build
#############
build:
runs-on: ubuntu-latest
name: Build assets
outputs:
version: ${{ steps.version.outputs.version }}
release: ${{ steps.version.outputs.release }}

strategy:
fail-fast: false

steps:
- name: Checkout sources
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Setup PHP ${{ env.php-version }}
uses: shivammathur/setup-php@v2
with:
php-version: ${{ env.php-version }}
extensions: mbstring, dom, fileinfo
coverage: none
- name: Check PHP Version
run: php -v
- name: Check Composer Version
run: composer -V
- name: Check PHP Extensions
run: php -m

# Composer
- name: Validate composer.json and composer.lock
run: composer validate

- name: Get Composer Cache Directory
id: composer-cache
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT
- name: Cache composer files
uses: actions/cache@v3
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ env.php-version }}-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-composer-${{ env.php-version }}-${{ hashFiles('**/composer.lock') }}
${{ runner.os }}-composer-${{ env.php-version }}
${{ runner.os }}-composer-
- name: Install composer dependencies
run: composer install --no-progress --no-interaction --prefer-dist --optimize-autoloader

# Yarn
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: ${{ env.node-version }}
- name: Cache yarn files
uses: actions/cache@v3
with:
path: .yarn/cache
key: ${{ runner.os }}-yarn-v2-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-yarn-v2-${{ hashFiles('**/yarn.lock') }}
${{ runner.os }}-yarn-v2-
- name: Install yarn dependencies
run: yarn install --immutable
- name: Lint files
run: yarn run lint

- name: Get version
id: version
run: |
echo "version=$(git describe --abbrev=0 --tags 2>/dev/null || echo '0.0.0' | sed 's/^v//')" >> $GITHUB_OUTPUT
echo "release=$(git describe --abbrev=0 --tags --exact-match $GITHUB_SHA 2>/dev/null || git log --pretty="%h" -n1 $GITHUB_SHA)" >> $GITHUB_OUTPUT
- name: Prepare environment
run: |
{ \
echo "VITE_PROD_SOURCE_MAPS=true"; \
echo "VITE_SENTRY_RELEASE=${{ steps.version.outputs.version }}"; \
} | tee .env
- name: Build assets
run: yarn run build

- name: Check if there is any file update needed
id: check
run: |
status=$(git status --porcelain)
if [ -z "$status" ]; then
echo "Nothing to push, already up to date."
else
echo -e "Waiting modifications:\n$status"
echo "::error::Resources are not up to date. Please fix it with: 'yarn run lint:fix'."
exit -1
fi
- name: Store assets
uses: actions/upload-artifact@v4
with:
name: assets
path: |
public/build/**/*
!public/build/**/*.map
- name: Store source maps
uses: actions/upload-artifact@v4
with:
name: sourcemaps
path: |
public/build/**/*.map
uses: monicahq/workflows/.github/workflows/build_assets.yml@main
with:
node-version: 18
store: true

######################
# Deploy on fortrabbit
Expand Down Expand Up @@ -188,7 +75,8 @@ jobs:
- name: Deploy
run: |
git remote add deploy $REPO_URL
git push deploy main:master --force
branch=$(git rev-parse --abbrev-ref HEAD)
git push deploy $branch:master --force
env:
# This avoids a failure when the client does not know the SSH Host already
GIT_SSH_COMMAND: "ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no"
Expand Down
Loading

0 comments on commit 17ca1c7

Please sign in to comment.