Skip to content
This repository has been archived by the owner on Jul 28, 2023. It is now read-only.
/ container Public archive
generated from spaceonfire/skeleton

Commit

Permalink
feat(ci): configure ci pipeline with github actions
Browse files Browse the repository at this point in the history
  • Loading branch information
tntrex committed Sep 28, 2020
1 parent 1220815 commit 388a936
Show file tree
Hide file tree
Showing 6 changed files with 246 additions and 55 deletions.
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@

# Ignore all test and documentation with "export-ignore".
/.github export-ignore
/bin/coverage-badge export-ignore
/docs export-ignore
/tests export-ignore
/.editorconfig export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/.gitlab-ci.yml export-ignore
/CODE_OF_CONDUCT.md export-ignore
/CONTRIBUTING.md export-ignore
/docker-compose.yml export-ignore
Expand Down
112 changes: 112 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
name: Build Pipeline

on:
push:
branches:
- master
pull_request:
branches:
- master

jobs:
composer:
runs-on: ubuntu-latest
container: spaceonfire/nginx-php-fpm:latest-7.2
steps:
- name: Checkout
uses: actions/checkout@v2

- name: Validate composer.json
run: composer validate

codestyle:
runs-on: ubuntu-latest
container: spaceonfire/nginx-php-fpm:latest-7.2
steps:
- name: Checkout
uses: actions/checkout@v2

- name: Cache Composer packages
id: composer-cache
uses: actions/cache@v2
env:
COMPOSER_CACHE_KEY: 'composer-7.2'
with:
path: vendor
key: ${{ env.COMPOSER_CACHE_KEY }}
restore-keys: ${{ env.COMPOSER_CACHE_KEY }}

- name: Install dependencies
if: steps.composer-cache.outputs.cache-hit != 'true'
run: composer install --prefer-dist --no-progress --no-suggest

- name: Check coding standard
run: vendor/bin/ecs check --no-progress-bar --no-interaction

phpstan:
runs-on: ubuntu-latest
container: spaceonfire/nginx-php-fpm:latest-7.2
steps:
- name: Checkout
uses: actions/checkout@v2

- name: Cache Composer packages
id: composer-cache
uses: actions/cache@v2
env:
COMPOSER_CACHE_KEY: 'composer-7.2'
with:
path: vendor
key: ${{ env.COMPOSER_CACHE_KEY }}
restore-keys: ${{ env.COMPOSER_CACHE_KEY }}

- name: Install dependencies
if: steps.composer-cache.outputs.cache-hit != 'true'
run: composer install --prefer-dist --no-progress --no-suggest

- name: PHPStan
run: vendor/bin/phpstan analyse --no-progress --no-interaction

phpunit:
runs-on: ubuntu-latest
strategy:
matrix:
php-version:
- '7.2'
- '7.3'
- '7.4'
container: spaceonfire/nginx-php-fpm:latest-${{ matrix.php-version }}
steps:
- name: Checkout
uses: actions/checkout@v2

- name: Cache Composer packages
id: composer-cache
uses: actions/cache@v2
env:
COMPOSER_CACHE_KEY: 'composer-${{ matrix.php-version }}'
with:
path: vendor
key: ${{ env.COMPOSER_CACHE_KEY }}
restore-keys: ${{ env.COMPOSER_CACHE_KEY }}

- name: Install dependencies
if: steps.composer-cache.outputs.cache-hit != 'true'
run: composer install --prefer-dist --no-progress --no-suggest

- name: PHPUnit
run: |
apk update
docker-php-ext-enable xdebug
vendor/bin/phpunit --no-interaction
cat build/coverage.txt
- name: PHPUnit Artifacts
uses: actions/upload-artifact@v2
with:
name: phpunit-${{ matrix.php-version }}
path: build/

- name: Generate coverage badge
if: matrix.php-version == '7.2' && github.event_name == 'push' && github.ref == 'refs/heads/master'
run: php bin/coverage-badge ${{ github.repository }}.json ${{ secrets.COVERAGE_GIST_ID }} ${{ secrets.COVERAGE_GIST_TOKEN }}
53 changes: 0 additions & 53 deletions .gitlab-ci.yml

This file was deleted.

3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ The MIT License (MIT). Please see [License File](LICENSE.md) for more informatio
[ico-version]: https://img.shields.io/packagist/v/spaceonfire/container.svg?style=flat-square
[ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square
[ico-downloads]: https://img.shields.io/packagist/dt/spaceonfire/container.svg?style=flat-square
[ico-coverage]: https://img.shields.io/endpoint?style=flat-square&url=https%3A%2F%2Fgist.githubusercontent.com%2Fhustlahusky%2Fd62607c1a2e4707959b0142e0ea876cd%2Fraw%2Fspaceonfire-container.json
[ico-build-status]: https://github.com/spaceonfire/container/workflows/Build%20Pipeline/badge.svg
[link-packagist]: https://packagist.org/packages/spaceonfire/container
[link-downloads]: https://packagist.org/packages/spaceonfire/container
[link-author]: https://github.com/hustlahusky
[link-contributors]: ../../contributors
[link-actions]: ../../actions
129 changes: 129 additions & 0 deletions bin/coverage-badge
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
#!/usr/bin/env php
<?php

/*
* php bin/coverage-badge <filename> <gist-id> <gist-token>
*/

try {
$arguments = $argv;
array_shift($arguments);

[$filename, $gistId, $gistToken] = $arguments + [null, null, null];

if ($filename === null) {
throw new InvalidArgumentException('Badge filename not specified');
}
if ($gistId === null) {
throw new InvalidArgumentException('Gist id not specified');
}
if ($gistToken === null) {
throw new InvalidArgumentException('Gist token not specified');
}

$filename = str_replace(['/', '_'], '-', $filename);

$coverageFile = dirname(__DIR__) . '/build/coverage.txt';

if (!file_exists($coverageFile)) {
throw new RuntimeException(sprintf('Text coverage report not found at %s', $coverageFile));
}

$coverageFileContents = file_get_contents($coverageFile);

preg_match('/^\s+Lines:\s+(\d+\.\d+)\%/m', $coverageFileContents, $matches);

if (isset($matches[1]) && is_numeric($matches[1])) {
$coverage = (float)$matches[1];

/** @noinspection TypeUnsafeComparisonInspection */
$message = sprintf(
'%s%%',
(string)$coverage == (int)$coverage
? (string)(int)$coverage
: number_format($coverage, 1)
);

$color = 'red';

if ($coverage >= 30) {
$color = 'orange';
}
if ($coverage >= 50) {
$color = 'yellow';
}
if ($coverage >= 65) {
$color = 'yellowgreen';
}
if ($coverage >= 80) {
$color = 'green';
}
if ($coverage >= 95) {
$color = 'brightgreen';
}
} else {
$message = 'N/A';
$color = 'lightgrey';
}

$badgeInfo = [
'schemaVersion' => 1,
'label' => 'coverage',
'message' => $message,
'color' => $color,
];

$postFields = json_encode([
'files' => [
$filename => [
'content' => json_encode($badgeInfo),
],
],
]);

$curl = curl_init();

curl_setopt_array($curl, [
CURLOPT_URL => sprintf('https://api.github.com/gists/%s', $gistId),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'PATCH',
CURLOPT_POSTFIELDS => $postFields,
CURLOPT_HTTPHEADER => [
'accept: application/vnd.github.v3+json',
sprintf('authorization: Bearer %s', $gistToken),
'content-type: application/json',
'user-agent: curl',
],
]);

$response = curl_exec($curl);
$json = json_decode($response, true);
$err = curl_error($curl);
$httpCode = curl_getinfo($curl, CURLINFO_RESPONSE_CODE);

curl_close($curl);

if ($err) {
throw new RuntimeException(sprintf('Curl error: %s', $err));
}

if ($httpCode > 399) {
$exceptionMessage = sprintf('Request error: %s', $json['message'] ?? $response);

if (isset($json['errors'])) {
$exceptionMessage .= PHP_EOL . PHP_EOL . 'Errors: ' . json_encode($json['errors'], JSON_PRETTY_PRINT);
}

throw new RuntimeException($exceptionMessage);
}

echo 'Coverage badge info uploaded' . PHP_EOL;
exit(0);
} catch (Throwable $e) {
fwrite(STDERR, $e->getMessage() . PHP_EOL);
exit(1);
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"phpunit/phpunit": "^8.5",
"phpstan/phpstan": "^0.12",
"roave/security-advisories": "dev-master",
"symplify/easy-coding-standard-prefixed": "^8.0"
"symplify/easy-coding-standard-prefixed": "^8.3"
},
"provide": {
"psr/container-implementation": "^1.0"
Expand Down

0 comments on commit 388a936

Please sign in to comment.