From 997776a7f6734520455274428915885bdb2bfa3e Mon Sep 17 00:00:00 2001 From: Kelly Dwan Date: Tue, 8 Dec 2020 12:19:07 -0500 Subject: [PATCH] Tools: Move from Travis CI to GitHub Actions for PR tests (#674) The WordPress project is moving away from Travis CI, so this migrates our CI checks to GitHub Actions. This only applies to automated linters and unit tests run on github PRs, nothing should change for local development. --- .github/bin/phpcs-branch.php | 10 ++-- .github/workflows/linter.yml | 50 +++++++++++++++++++ .github/workflows/unit-tests.yml | 86 ++++++++++++++++++++++++++++++++ .nvmrc | 2 +- .travis.yml | 52 ------------------- 5 files changed, 144 insertions(+), 56 deletions(-) create mode 100644 .github/workflows/linter.yml create mode 100644 .github/workflows/unit-tests.yml delete mode 100644 .travis.yml diff --git a/.github/bin/phpcs-branch.php b/.github/bin/phpcs-branch.php index b5beba6f36..45ff9dacdf 100644 --- a/.github/bin/phpcs-branch.php +++ b/.github/bin/phpcs-branch.php @@ -19,7 +19,7 @@ function run_phpcs( $file, $bin_dir ) { function run_phpcs_changed( $file, $git, $base_branch, $bin_dir ) { $name = basename( $file ); - exec( "$git diff $base_branch HEAD $file > $name.diff" ); + exec( "$git diff $base_branch $file > $name.diff" ); exec( "$git show $base_branch:$file | $bin_dir/phpcs --standard=./phpcs.xml.dist --report=json -nq > $name.orig.phpcs" ); exec( "cat $file | $bin_dir/phpcs --standard=./phpcs.xml.dist --report=json -nq > $name.phpcs" ); @@ -33,7 +33,7 @@ function run_phpcs_changed( $file, $git, $base_branch, $bin_dir ) { } function main() { - $base_branch = getenv( 'TRAVIS_BRANCH' ); + $base_branch = 'remotes/origin/' . getenv( 'BASE_REF' ); $git_dir = dirname( dirname( __DIR__ ) ); $bin_dir = dirname( dirname( __DIR__ ) ) . '/public_html/wp-content/mu-plugins/vendor/bin'; $git = "git -C $git_dir"; @@ -42,10 +42,14 @@ function main() { echo "\nScanning changed files...\n"; $status = 0; - $affected_files = shell_exec( "$git diff $base_branch...HEAD --name-status --diff-filter=AM 2>&1 | grep .php$" ); + $affected_files = shell_exec( "$git diff $base_branch --name-status --diff-filter=AM 2>&1 | grep .php$" ); $affected_files = explode( "\n", trim( $affected_files ) ); foreach ( $affected_files as $record ) { + if ( ! $record ) { + continue; + } + list( $change, $file ) = explode( "\t", trim( $record ) ); $cmd_status = 0; diff --git a/.github/workflows/linter.yml b/.github/workflows/linter.yml new file mode 100644 index 0000000000..e131487506 --- /dev/null +++ b/.github/workflows/linter.yml @@ -0,0 +1,50 @@ +name: Static Analysis (Linting) + +# Run on any changes in public_html +on: + pull_request: + paths: + - public_html/** + push: + branches: [production] + paths: + - public_html/** + +jobs: + check: + name: All + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + with: + fetch-depth: 0 + + - uses: actions/cache@v2 + with: + path: '**/node_modules' + key: ${{ runner.os }}-modules-${{ hashFiles('**/yarn.lock') }} + + - name: Use Node.js 12.x + uses: actions/setup-node@v2-beta + with: + node-version: 12.x + + - name: composer install + run: | + composer install + + - name: yarn install and build + run: | + yarn + yarn workspaces run build + + - name: Lint JavaScript and Styles + run: | + yarn workspaces run lint:js + yarn workspaces run lint:css + + - name: Lint PHP + run: | + BASE_REF=${{ github.base_ref }} php .github/bin/phpcs-branch.php diff --git a/.github/workflows/unit-tests.yml b/.github/workflows/unit-tests.yml new file mode 100644 index 0000000000..4ea6d5ba70 --- /dev/null +++ b/.github/workflows/unit-tests.yml @@ -0,0 +1,86 @@ +name: Unit Tests + +# Run on any changes in public_html +on: + pull_request: + paths: + - public_html/** + push: + branches: [production] + paths: + - public_html/** + +jobs: + unit-js: + name: JavaScript + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + + - uses: actions/cache@v2 + with: + path: '**/node_modules' + key: ${{ runner.os }}-modules-${{ hashFiles('**/yarn.lock') }} + + - name: Use Node.js 12.x + uses: actions/setup-node@v2-beta + with: + node-version: 12.x + + - name: Install dependencies + # We don't need to build, since jest can interpret the source files. + run: yarn + + # Run the Blocks unit tests. If/when other workspaces get tests, those would be added here. + - name: Running the tests + run: yarn workspace wordcamp-blocks run test + + unit-php: + name: PHP Tests + + runs-on: ubuntu-latest + + strategy: + fail-fast: false + matrix: + php-version: ['7.4'] # 8.0 -- Add back to matrix when 8.0 is supported. + + services: + mysql: + image: mysql:5.7 + env: + MYSQL_ALLOW_EMPTY_PASSWORD: false + MYSQL_ROOT_PASSWORD: root + MYSQL_DATABASE: wcorg_test + ports: + - 3306/tcp + options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3 + + steps: + - uses: actions/checkout@v2 + + - name: Set PHP version + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php-version }} + coverage: none + tools: composer:v1 + + - name: Start mysql service + run: sudo /etc/init.d/mysql start + + - name: Install dependencies + run: composer install + + - name: Install WordPress + run: | + bash .docker/bin/install-wp-tests.sh wcorg_test root root 127.0.0.1 trunk + + - name: Install required plugins + run: | + svn export https://plugins.svn.wordpress.org/jetpack/trunk public_html/wp-content/plugins/jetpack + + - name: Running unit tests + run: ./public_html/wp-content/mu-plugins/vendor/bin/phpunit -c phpunit.xml.dist diff --git a/.nvmrc b/.nvmrc index b009dfb9d9..48082f72f0 100644 --- a/.nvmrc +++ b/.nvmrc @@ -1 +1 @@ -lts/* +12 diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 19bec588b9..0000000000 --- a/.travis.yml +++ /dev/null @@ -1,52 +0,0 @@ -# Travis CI configuration file. -# @link https://travis-ci.org/ - -# Declare project language and PHP versions to test against. -# @link http://about.travis-ci.org/docs/user/languages/php/ -language: php -php: 7.2 -env: WP_VERSION=latest WP_MULTISITE=1 -services: - - mysql - -before_script: - # Print system info for debugging. - - node --version - - yarn --version - - timedatectl - -jobs: - include: - # To keep things clean, we set up separate jobs for Travis per project. - - name: "yarn scripts" - install: - - nvm install && nvm use - - yarn - script: - # Run eslint & stylelint over all JS/SCSS files - - yarn workspaces run lint:js - - yarn workspaces run lint:css - # Make sure every build completes. - - yarn workspaces run build - # Run the Blocks unit tests. If/when other workspaces get tests, those would be added here. - - yarn workspace wordcamp-blocks run test - - - name: "PHP Unit Tests" - install: - - bash .docker/bin/install-wp-tests.sh wcorg_test root '' localhost $WP_VERSION - - composer install - # We need Jetpack installed - - svn export https://plugins.svn.wordpress.org/jetpack/trunk public_html/wp-content/plugins/jetpack - script: - - ./public_html/wp-content/mu-plugins/vendor/bin/phpunit -c phpunit.xml.dist - - - name: "PHP Code Standards" - # Only run if we're on the "pull request build", otherwise the diffs are empty. - if: branch = production - install: - - composer install - script: - # Report on phpcs violations that are introduced in the current branch. - # Runs `phpcs` as normal on new files, and `phpcs-changed` on modified files. `phpcs-changed` will only - # report on changed lines in each modified file. - - php .github/bin/phpcs-branch.php