Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate Travis CI tasks to GitHub Actions #1040

Merged
merged 13 commits into from
Oct 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions .github/actions/prepare-node/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: Prepare Node
description: Load npm cache, install Node dependencies

inputs:
install-deps:
description: Whether to run `npm ci`. Set "maybe" to install deps if cache is missing. "yes" by default.
required: false
default: "yes"

ignore-scripts:
description: Whether to run `npm ci` with --ignore-scripts. "yes" by default.
required: false
default: "yes"

runs:
using: composite
steps:
# Log debug information
- shell: sh -e {0}
run: |
node --version
npm --version

# Get npm cache directory
- uses: actions/cache@v2
id: npm-cache
with:
path: ~/.npm
key: ${{ runner.os }}-node-${{ hashFiles('package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-

# Install node dependencies
- shell: sh -e {0}
# Update npm cache directory if package-lock has changed
run: |
if [ "${{ inputs.install-deps }}" = "yes" ] || [ "${{ steps.npm-cache.outputs.cache-hit }}" != "true" ]; then
npm ci `if [ "${{ inputs.ignore-scripts }}" = "yes" ]; then printf %s "--ignore-scripts"; fi`
else
echo "Skip npm ci"
fi
60 changes: 60 additions & 0 deletions .github/actions/prepare-php/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
name: Prepare PHP
description: Set up PHP, load Composer cache, install Composer dependencies

inputs:
php-version:
description: Specify PHP version. "7.4" by default.
required: false
default: "7.4"

tools:
description: Specify the tools parameter of shivammathur/setup-php@v2. null by default.
required: false
default: null

install-deps:
description: Whether to run `composer install`. Set "maybe" to install deps if cache is missing. "yes" by default.
required: false
default: "yes"

runs:
using: composite
steps:
# Set up PHP
- name:
uses: shivammathur/setup-php@v2
with:
php-version: ${{ inputs.php-version }}
coverage: none
tools: ${{ inputs.tools }}

# Log debug information
- shell: sh -e {0}
run: |
php --version
composer --version

# Get Composer cache directory
- shell: sh -e {0}
id: composer-cache-config
run: echo "::set-output name=dir::$(composer config cache-files-dir)"

# Set up Composer caching
- uses: actions/cache@v2
id: composer-cache
with:
path: ${{ steps.composer-cache-config.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('composer.lock') }}
restore-keys: |
${{ runner.os }}-composer-

# Install Composer dependencies
- shell: sh -e {0}
# Update Composer cache directory if composer.lock has changed
run: |
if [ "${{ inputs.install-deps }}" = "yes" ] || [ "${{ steps.composer-cache.outputs.cache-hit }}" != "true" ]; then
composer install --prefer-dist --no-suggest --no-progress --no-ansi --no-interaction
echo "${PWD}/vendor/bin" >> $GITHUB_PATH
else
echo "Skip composer install"
fi
58 changes: 58 additions & 0 deletions .github/workflows/bundle-size.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
name: Bundle Size

on:
push:
branches:
- trunk
- develop
pull_request:

jobs:
BundleSize:
name: Bundle size
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2

- name: Prepare PHP
uses: ./.github/actions/prepare-php
with:
install-deps: "maybe"

- name: Prepare node
uses: ./.github/actions/prepare-node
with:
ignore-scripts: "no"

- name: Build production bundle
run: |
echo "::group::Build log"
tomalec marked this conversation as resolved.
Show resolved Hide resolved
npm run build
echo "::endgroup::"

# Since BundleWatch is having issues when getting some environment variables from GitHub Actions,
# it needs some workarounds to specify `CI_COMMIT_SHA`, `CI_BRANCH`, and `CI_BRANCH_BASE` here.
# References:
# - https://github.com/bundlewatch/bundlewatch/blob/v0.3.2/src/app/config/getCIVars.js#L8-L10
# - https://github.com/bundlewatch/bundlewatch/issues/423
# - https://github.com/bundlewatch/bundlewatch/issues/220
- name: Prepare BundleWatch env values - push
if: ${{ github.event_name == 'push' }}
run: |
BRANCH=$(echo '${{ github.event.ref }}' | sed 's/^refs\/heads\///')
echo "CI_BRANCH=$BRANCH" >> $GITHUB_ENV
echo "CI_BRANCH_BASE=$BRANCH" >> $GITHUB_ENV
echo "CI_COMMIT_SHA=${{ github.sha }}" >> $GITHUB_ENV

- name: Prepare BundleWatch env values - pull request
if: ${{ github.event_name == 'pull_request' }}
run: |
echo "CI_BRANCH=${{ github.head_ref }}" >> $GITHUB_ENV
echo "CI_BRANCH_BASE=${{ github.base_ref }}" >> $GITHUB_ENV
echo "CI_COMMIT_SHA=${{ github.event.pull_request.head.sha }}" >> $GITHUB_ENV

- name: Run BundleWatch
env:
BUNDLEWATCH_GITHUB_TOKEN: ${{ secrets.BUNDLEWATCH_GITHUB_TOKEN }}
run: node ./node_modules/bundlewatch/lib/bin/index.js
63 changes: 63 additions & 0 deletions .github/workflows/js-css-linting.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
name: JavaScript and CSS Linting

on:
push:
branches:
- trunk
- develop
pull_request:

jobs:
Setup:
name: Setup for jobs
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2

- name: Prepare node
uses: ./.github/actions/prepare-node
with:
install-deps: "maybe"

JSLintingCheck:
name: Lint JavaScript
needs: Setup
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2

- name: Prepare node
uses: ./.github/actions/prepare-node

- name: Save code linting report JSON
run: npm run lint:js -- --quiet --output-file eslint_report.json --format json
# Continue to the next step even if this fails
continue-on-error: true

- name: Annotate code linting results
uses: ataylorme/[email protected]
with:
repo-token: "${{ secrets.GITHUB_TOKEN }}"
report-json: "eslint_report.json"

- name: Upload ESLint report
uses: actions/upload-artifact@v2
with:
name: eslint_report.json
path: eslint_report.json

CSSLintingCheck:
name: Lint CSS
needs: Setup
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2

- name: Prepare node
uses: ./.github/actions/prepare-node

- name: Lint CSS
run: npm run lint:css
22 changes: 22 additions & 0 deletions .github/workflows/js-unit-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: JavaScript Unit Tests

on:
push:
branches:
- trunk
- develop
pull_request:

jobs:
UnitTests:
name: JavaScript unit tests
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2

- name: Prepare node
uses: ./.github/actions/prepare-node

- name: Run JavaScript unit tests
run: npm run test-unit
27 changes: 27 additions & 0 deletions .github/workflows/php-coding-standards.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: PHP Coding Standards

on:
push:
branches:
- trunk
- develop
pull_request:

jobs:
phpcs:
name: PHP coding standards
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2

- name: Prepare PHP
uses: ./.github/actions/prepare-php
with:
tools: cs2pr

- name: Log PHPCS debug information
run: vendor/bin/phpcs -i

- name: Run PHPCS on all files
run: vendor/bin/phpcs ./src -q -n --report=checkstyle | cs2pr
68 changes: 68 additions & 0 deletions .github/workflows/php-unit-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
name: PHP Unit Tests

on:
push:
branches:
- trunk
- develop
pull_request:

jobs:
Setup:
name: Setup for jobs
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2

- name: Prepare node
uses: ./.github/actions/prepare-node
with:
install-deps: "maybe"

- name: Prepare PHP
uses: ./.github/actions/prepare-php
with:
install-deps: "maybe"

UnitTests:
name: PHP unit tests - PHP ${{ matrix.php }}, WP ${{ matrix.wp-version }}
needs: Setup
runs-on: ubuntu-latest
env:
WP_CORE_DIR: "/tmp/wordpress/src"
WP_TESTS_DIR: "/tmp/wordpress/tests/phpunit"
strategy:
matrix:
php: [7.3, 7.4]
wp-version: [5.6, 5.7, 5.8]

steps:
- name: Checkout repository
uses: actions/checkout@v2

- name: Prepare node
uses: ./.github/actions/prepare-node

- name: Prepare PHP
uses: ./.github/actions/prepare-php
with:
php-version: "${{ matrix.php }}"

- name: Set up MySQL
# MySQL 8.0 uses the `caching_sha2_password` authentication method by default.
# So here alter password with `mysql_native_password` authentication method
# to make older PHP (7.3.x) mysql client be able to create database connections.
run: |
sudo systemctl start mysql.service
mysql -u root -proot -e "ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'root';"

- name: Build client bundles
run: npm run dev

- name: Install WP tests
shell: bash
run: ./bin/install-wp-tests.sh wordpress_test root root localhost ${{ matrix.wp-version }}

- name: Run PHP unit tests
run: composer test-unit
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
# Google Listings & Ads [![Build Status](https://travis-ci.com/woocommerce/google-listings-and-ads.svg?branch=trunk)](https://travis-ci.com/woocommerce/google-listings-and-ads)
# Google Listings & Ads

[![PHP Unit Tests](https://github.com/woocommerce/google-listings-and-ads/actions/workflows/php-unit-tests.yml/badge.svg)](https://github.com/woocommerce/google-listings-and-ads/actions/workflows/php-unit-tests.yml)
[![JavaScript Unit Tests](https://github.com/woocommerce/google-listings-and-ads/actions/workflows/js-unit-tests.yml/badge.svg)](https://github.com/woocommerce/google-listings-and-ads/actions/workflows/js-unit-tests.yml)
[![PHP Coding Standards](https://github.com/woocommerce/google-listings-and-ads/actions/workflows/php-coding-standards.yml/badge.svg)](https://github.com/woocommerce/google-listings-and-ads/actions/workflows/php-coding-standards.yml)
[![JavaScript and CSS Linting](https://github.com/woocommerce/google-listings-and-ads/actions/workflows/js-css-linting.yml/badge.svg)](https://github.com/woocommerce/google-listings-and-ads/actions/workflows/js-css-linting.yml)
[![Bundle Size](https://github.com/woocommerce/google-listings-and-ads/actions/workflows/bundle-size.yml/badge.svg)](https://github.com/woocommerce/google-listings-and-ads/actions/workflows/bundle-size.yml)

A native integration with Google offering free listings and Smart Shopping ads to WooCommerce merchants.

Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"dealerdirect/phpcodesniffer-composer-installer": "^v0.7",
"phpunit/phpunit": "^7.5",
"wp-cli/i18n-command": "^2.2",
"wp-coding-standards/wpcs": "^2.3"
"wp-coding-standards/wpcs": "^2.3",
"yoast/phpunit-polyfills": "^1.0"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


Is this a dependency needed for GH Actions, or something that was missing before?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's the problem stuck me for the longest before. I got error when running composer test-unit on my local and GHA. Not sure why it can run successfully on Travis CI.

> ./vendor/bin/phpunit
Error: The PHPUnit Polyfills library is a requirement for running the WP test suite.
If you are trying to run plugin/theme integration tests, make sure the PHPUnit Polyfills library (https://github.com/Yoast/PHPUnit-Polyfills) is available and either load the autoload file of this library in your own test bootstrap before calling the WP Core test bootstrap file; or set the absolute path to the PHPUnit Polyfills library in a "WP_TESTS_PHPUNIT_POLYFILLS_PATH" constant to allow the WP Core bootstrap to load the Polyfills.

If you are trying to run the WP Core tests, make sure to set the "WP_RUN_CORE_TESTS" constant to 1 and run `composer install` before running the tests.
Once the dependencies are installed, you can run the tests using the Composer-installed version of PHPUnit or using a PHPUnit phar file, but the dependencies do need to be installed whichever way the tests are run.
Script ./vendor/bin/phpunit handling the test-unit event returned with error code 1

Finally, I ended up this problem by adding the yoast/phpunit-polyfills dependency.

},
"replace": {
"symfony/polyfill-mbstring": "*"
Expand Down
Loading