Skip to content

Commit

Permalink
Update GitHub Workflow & Cleanup bin shells (#108)
Browse files Browse the repository at this point in the history
* Add reporting to PHPCS.

* Updates to local bin shell files.

* Wrap exposed features into funtions.

* Update to latest install shell.

* Update Workflow dependencies to latest version to avoid deprecations in Actions.

- Add Changed Files action to get from step.

* Add new composite Action.

* Update default workflow to use new composite action to be more dynamic.

* Update formatting of array for bash.

* Remove env.

* Update composite to accept a token from the input value.

* Misspelled matrix.

* Add at least one extension.
  • Loading branch information
thefrosty authored Jan 18, 2023
1 parent d25261b commit 7d467db
Show file tree
Hide file tree
Showing 8 changed files with 323 additions and 203 deletions.
70 changes: 70 additions & 0 deletions .github/actions/ci-setup/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
name: CI Setup
description: "Sets up the environment for jobs during CI workflow"
# https://alejandrocelaya.blog/2022/08/19/how-to-reduce-duplication-in-your-github-actions-workflows/

inputs:
extensions:
default: 'curl, mysql, zip'
description: 'shivammathur/setup-php extensions'
required: false
extensions-cache-key:
description: 'The key used to cache PHP extensions'
required: true
php-version:
description: 'The PHP version to be setup'
required: true
token:
description: 'A GitHub PAT'
required: true
tools:
default: 'composer'
description: 'shivammathur/setup-php tools'
required: false
outputs:
files:
description: 'All changed files'
value: ${{ steps.files.outputs.all }}

runs:
using: composite
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Setup cache extensions
id: extcache
uses: shivammathur/cache-extensions@v1
with:
php-version: ${{ inputs.php-version }}
extensions: ${{ inputs.extensions }}
key: ${{ inputs.extensions-cache-key }}

- name: Cache extensions
uses: actions/cache@v3
with:
path: ${{ steps.extcache.outputs.dir }}
key: ${{ steps.extcache.outputs.key }}
restore-keys: ${{ steps.extcache.outputs.key }}

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ inputs.php-version }}
coverage: xdebug
extensions: ${{ inputs.extensions }}
tools: ${{ inputs.tools }}

- name: Composer config
run: |
composer config github-oauth.github.com "${{ env.GITHUB_TOKEN }}"
env:
GITHUB_TOKEN: ${{ inputs.token }}
shell: bash

- name: Composer update
run: composer update --no-interaction --optimize-autoloader
shell: bash

- name: Get Changed Files
id: files
uses: masesgroup/retrieve-changed-files@v2
81 changes: 51 additions & 30 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -1,61 +1,82 @@
name: Workflow
name: CI Tests

on:
push:
branches:
- develop
pull_request:

env:
WP_VERSION: 6.1.1

# Cancels all previous workflow runs for the same branch that have not yet completed.
concurrency:
# The concurrency group contains the workflow name and the branch name.
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
Build:
runs-on: ubuntu-latest
run-phpcs:
name: Run PHPCS
runs-on: ubuntu-20.04
if: ${{ github.event_name == 'pull_request' }}
strategy:
matrix:
php-versions: [ 7.4, 8.0, 8.1 ]
php-versions: [ 8.0, 8.1, 8.2 ]
coverage: [ true ]

steps:
- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 0 # No shallow clone, we need all history!
uses: actions/checkout@v3

- name: Setup PHP
uses: shivammathur/setup-php@v2
- name: Setup
id: ci-setup
uses: ./.github/actions/ci-setup
with:
extensions: 'curl'
extensions-cache-key: run-phpcs
php-version: ${{ matrix.php-versions }}
coverage: xdebug
tools: composer, cs2pr, phpunit
token: ${{ secrets.GITHUB_TOKEN }}
tools: 'composer, cs2pr, phpcs'

- name: Get composer cache directory
id: composercache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
- name: Run PHPCS
continue-on-error: false
id: phpcs
run: composer phpcs
env:
CHANGED_FILES: ${{ steps.ci-setup.outputs.files }}

- name: Cache dependencies
uses: actions/cache@v2
with:
path: ${{ steps.composercache.outputs.dir }}
key: php-${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: ${{ runner.os }}-composer-
- name: Show PHPCS results in PR
if: ${{ always() && steps.phpcs.outcome == 'failure' }}
run: cs2pr ./phpcs-report.xml

- name: Install dependencies
run: composer update --prefer-dist --no-interaction
run-phpunit:
name: Run PHPUnit
runs-on: ubuntu-20.04
if: ${{ github.event_name == 'pull_request' }}
strategy:
matrix:
php-versions: [ 8.0, 8.1, 8.2 ]
coverage: [ true ]
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Create all branches
run: source ./bin/create-all-branches.sh
- name: Setup
id: ci-setup
uses: ./.github/actions/ci-setup
with:
extensions: 'curl, mysql, mysqli, tar, zip'
extensions-cache-key: run-phpunit
php-version: ${{ matrix.php-versions }}
token: ${{ secrets.GITHUB_TOKEN }}
tools: 'composer, phpunit'

- name: Run composer tests
- name: Run PHPUnit
continue-on-error: false
run: composer tests

- name: Show PHPCS results in PR
run: cs2pr ./phpcs-report.xml
id: phpunit
run: composer phpunit
env:
CHANGED_FILES: ${{ steps.ci-setup.outputs.files }}

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
Expand Down
6 changes: 4 additions & 2 deletions bin/eslint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

set -e

echo 'Checking ESLint'
source "$(dirname "$0")/functions.sh"
echo 'Checking ESLint'

jsFiles=""
jsFilesCount=0
Expand All @@ -21,6 +21,8 @@ if [[ ${jsFilesCount} == 0 ]]; then
exit 0
fi

jsFiles=$(echo "${jsFiles}" | xargs)
echo "Checking files: $jsFiles"

npx standard "${jsFiles}"
# shellcheck disable=SC2086
npx standard ${jsFiles}
103 changes: 75 additions & 28 deletions bin/functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,82 @@ OTHER_ARGUMENTS=()

# Loop through arguments and process them
# @ref https://pretzelhands.com/posts/command-line-flags/
for arg in "$@"; do
case $arg in
--default-branch=*)
DEFAULT_BRANCH="${arg#*=}"
shift # Remove --default-branch= from processing
;;
--test-version=*)
TEST_VERSION="${arg#*=}"
shift # Remove --test-version= from processing
;;
*)
OTHER_ARGUMENTS+=("$1")
shift # Remove generic argument from processing
;;
esac
done
function get_arguments() {
for arg in "$@"; do
case $arg in
--default-branch=*)
DEFAULT_BRANCH="${arg#*=}"
shift # Remove --default-branch= from processing
;;
--test-version=*)
TEST_VERSION="${arg#*=}"
shift # Remove --test-version= from processing
;;
*)
OTHER_ARGUMENTS+=("$1")
shift # Remove generic argument from processing
;;
esac
done
}

# Based off: https://gist.github.com/Hounddog/3891872
if [[ $(git rev-parse --verify HEAD) ]]; then
against='HEAD'
elif [[ $(git rev-parse --verify develop) ]]; then
against='develop'
elif [[ $(git rev-parse --verify main) ]]; then
against='main'
elif [[ $(git rev-parse --verify master) ]]; then
against='master'
# Composer 2.2.x https://getcomposer.org/doc/articles/vendor-binaries.md#finding-the-composer-bin-dir-from-a-binary
if [[ -z "$COMPOSER_BIN_DIR" ]]; then
BIN_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
else
echo "git can't verify HEAD, develop, master, or main."
exit 1
BIN_DIR="$COMPOSER_BIN_DIR"
fi

commitFiles=$(git diff --name-only "$(git merge-base ${DEFAULT_BRANCH:-develop} ${against})")
export BIN_DIR

function get_branch() {
echo "${GITHUB_BASE_REF:=develop}"
}

# Based off: https://gist.github.com/Hounddog/3891872
# Branch to check current commit against
function get_commit_against() {
if [[ $(git rev-parse --verify HEAD) ]]; then
echo 'HEAD'
elif [[ $(git rev-parse --verify develop) ]]; then
echo 'develop'
elif [[ $(git rev-parse --verify main) ]]; then
echo 'main'
elif [[ $(git rev-parse --verify master) ]]; then
echo 'master'
elif [[ $(git rev-parse --verify "$1") ]]; then
echo "$1"
else
echo "git can't verify HEAD, develop, main, or master."
exit 1
fi
}

# Helper function to call a bash file with arguments
# $1: The file to call
# $2: The first arguments to pass to the file
# $3: The second arguments to pass to the file
function source_bin_file() {
if [[ ! "${1+x}" ]]; then
echo "Error: missing file" && exit 1
fi

FILE=./vendor/bin/"$1"
if [[ -f "$FILE" ]]; then
# shellcheck disable=SC2086
"$FILE" "${@:2}"
else
# shellcheck disable=SC2086
"$BIN_DIR"/"$1" "${@:2}"
fi
}

against=$(get_commit_against)
commit=$(get_branch)
echo "git merge-base commit: ${commit} against: ${against}"
if [[ -z ${CHANGED_FILES+x} ]]; then
#commitFiles=$(git diff --name-only "$(git merge-base "${commit}" "${against}")")
commitFiles=$(git diff --name-only "$(git merge-base ${DEFAULT_BRANCH:-develop} ${against})")
else
commitFiles="${CHANGED_FILES}"
fi
Loading

0 comments on commit 7d467db

Please sign in to comment.