diff --git a/.github/workflows/test-playwright.yml b/.github/workflows/test-playwright.yml new file mode 100644 index 00000000..f11f3c8f --- /dev/null +++ b/.github/workflows/test-playwright.yml @@ -0,0 +1,156 @@ +name: Playwright test + +on: + workflow_call: + inputs: + ARTIFACT_INCLUDE_HIDDEN_FILES: + description: Whether to include hidden files in the artifact. + type: boolean + default: false + required: false + ARTIFACT_NAME: + description: Name for the artifact. + type: string + default: 'artifact' + required: false + ARTIFACT_OVERWRITE: + description: Determine if an artifact with a matching name will be deleted before a new one is uploaded or not. + type: boolean + default: false + required: false + ARTIFACT_PATH: + description: A file, directory or wildcard pattern that describes what to upload. + type: string + required: true + ARTIFACT_RETENTION_DAYS: + description: Duration after which artifact will expire in day. + type: number + default: 30 + required: false + COMPOSER_DEPS_INSTALL: + description: Whether to install Composer dependencies. + type: boolean + default: false + required: false + NODE_VERSION: + description: Node version with which the node script will be executed. + default: 18 + required: false + type: string + NPM_REGISTRY_DOMAIN: + description: Domain of the private npm registry. + default: https://npm.pkg.github.com/ + required: false + type: string + PHP_VERSION: + description: PHP version with which the dependencies are installed. + default: '8.2' + required: false + type: string + PLAYWRIGHT_BROWSER_ARGS: + description: Set of arguments passed to `npx playwright install`. + default: '--with-deps' + required: false + type: string + SCRIPT_NAME: + description: The name of a custom script to run the tests. + required: true + type: string + secrets: + ENV_FILE_DATA: + description: Additional environment variables for the tests. + required: false + GITHUB_USER_EMAIL: + description: Email address for the GitHub user configuration. + required: false + GITHUB_USER_NAME: + description: Username for the GitHub user configuration. + required: false + GITHUB_USER_SSH_KEY: + description: Private SSH key associated with the GitHub user passed as `GITHUB_USER_NAME`. + required: false + NPM_REGISTRY_TOKEN: + description: Authentication for the private npm registry. + required: false + COMPOSER_AUTH_JSON: + description: Authentication for privately hosted packages and repositories as a JSON formatted object. + required: false + +jobs: + run-playwright-test: + timeout-minutes: 60 + runs-on: ubuntu-latest + env: + PHP_CHECK: false + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Set up SSH + env: + GITHUB_USER_SSH_KEY: ${{ secrets.GITHUB_USER_SSH_KEY }} + if: ${{ env.GITHUB_USER_SSH_KEY != '' }} + uses: webfactory/ssh-agent@v0.9.0 + with: + ssh-private-key: ${{ env.GITHUB_USER_SSH_KEY }} + + - name: Set up Git + env: + GITHUB_USER_EMAIL: ${{ secrets.GITHUB_USER_EMAIL }} + GITHUB_USER_NAME: ${{ secrets.GITHUB_USER_NAME }} + if: ${{ env.GITHUB_USER_EMAIL != '' && env.GITHUB_USER_NAME != '' }} + run: | + git config --global user.email "${{ env.GITHUB_USER_EMAIL }}" + git config --global user.name "${{ env.GITHUB_USER_NAME }}" + + - name: Set up PHP + if: ${{ inputs.COMPOSER_DEPS_INSTALL }} + uses: shivammathur/setup-php@v2 + env: + COMPOSER_AUTH: '${{ secrets.COMPOSER_AUTH_JSON }}' + with: + php-version: ${{ inputs.PHP_VERSION }} + tools: composer + coverage: none + + - name: Install Composer dependencies + if: ${{ inputs.COMPOSER_DEPS_INSTALL }} + uses: ramsey/composer-install@v3 + with: + composer-options: '--prefer-dist' + + - name: Set up node + uses: actions/setup-node@v4 + env: + NODE_AUTH_TOKEN: ${{ secrets.NPM_REGISTRY_TOKEN }} + with: + node-version: ${{ inputs.NODE_VERSION }} + registry-url: ${{ inputs.NPM_REGISTRY_DOMAIN }} + cache: 'npm' + + - name: Install dependencies + run: npm ci + + - name: Install Playwright dependencies + run: | + npx playwright install ${{ inputs.PLAYWRIGHT_BROWSER_ARGS }} + + - name: Run script for test + continue-on-error: true + run: | + touch .env.ci + echo "${{ secrets.ENV_FILE_DATA }}" >> .env.ci + # Ensure .env.ci is deleted on exit + trap 'rm -f .env.ci' EXIT + + npm run ${{ inputs.SCRIPT_NAME }} + + - name: Upload artifact + uses: actions/upload-artifact@v4 + with: + name: ${{ inputs.ARTIFACT_NAME }} + path: | + ${{ inputs.ARTIFACT_PATH }} + overwrite: ${{ inputs.ARTIFACT_OVERWRITE }} + include-hidden-files: ${{ inputs.ARTIFACT_INCLUDE_HIDDEN_FILES }} + retention-days: ${{ inputs.ARTIFACT_RETENTION_DAYS }} diff --git a/docs/test-playwright.md b/docs/test-playwright.md new file mode 100644 index 00000000..fd329ad4 --- /dev/null +++ b/docs/test-playwright.md @@ -0,0 +1,95 @@ +# Playwright test + +This workflow executes Playwright-based tests in a controlled and isolated environment via GitHub Actions. + +The workflow can: + +- execute a building step, both for node and PHP environments (if the PHP version is provided and a `composer.json` file is present) +- create an environment variables file named `.env.ci` dedicated to the test step; load this file using `dotenv-ci` directly in your test script, e.g., `./node_modules/.bin/dotenv -e .env.ci -- npm run e2e` +- execute the tests using Playwright +- upload the artifacts + +**Simplest possible example:** + +```yml +name: E2E Testing + +on: + workflow_dispatch: +jobs: + e2e-playwright: + uses: inpsyde/reusable-workflows/.github/workflows/test-playwright.yml@main + with: + ARTIFACT_PATH: './artifacts' + SCRIPT_NAME: 'ci-test-e2e' +``` + +## Configuration parameters + +### Inputs + +| Name | Default | Description | +|---------------------------------|---------------------------------|---------------------------------------------------------------------------------------------------| +| `ARTIFACT_INCLUDE_HIDDEN_FILES` | `false` | Whether to include hidden files in the artifact | +| `ARTIFACT_NAME` | `'artifact'` | Name for the artifact | +| `ARTIFACT_OVERWRITE` | `false` | Determine if an artifact with a matching name will be deleted before a new one is uploaded or not | +| `ARTIFACT_PATH` | | A file, directory or wildcard pattern that describes what to upload | +| `ARTIFACT_RETENTION_DAYS` | `30` | Duration after which artifact will expire in day | +| `COMPOSER_DEPS_INSTALL` | `false` | Whether to install Composer dependencies | +| `NODE_VERSION` | `18` | Node version with which the node script will be executed | +| `NPM_REGISTRY_DOMAIN` | `'https://npm.pkg.github.com/'` | Domain of the private npm registry | +| `PHP_VERSION` | `'8.2'` | PHP version with which the dependencies are installed | +| `PLAYWRIGHT_BROWSER_ARGS` | `'--with-deps'` | Set of arguments passed to `npx playwright install` | +| `SCRIPT_NAME` | | The name of a custom script to run the tests | + +### Secrets + +| Name | Description | +|-----------------------|------------------------------------------------------------------------------------------| +| `ENV_FILE_DATA` | Additional environment variables for the tests | +| `COMPOSER_AUTH_JSON` | Authentication for privately hosted packages and repositories as a JSON formatted object | +| `NPM_REGISTRY_TOKEN` | Authentication for the private npm registry | +| `GITHUB_USER_EMAIL` | Email address for the GitHub user configuration | +| `GITHUB_USER_NAME` | Username for the GitHub user configuration | +| `GITHUB_USER_SSH_KEY` | Private SSH key associated with the GitHub user passed as `GITHUB_USER_NAME` | + +**Example with configuration parameters:** + +```yml +name: E2E Testing + +on: + workflow_dispatch: +jobs: + e2e-playwright: + uses: inpsyde/reusable-workflows/.github/workflows/test-playwright.yml@main + strategy: + matrix: + php: [ '8.2', '8.3' ] + with: + ARTIFACT_PATH: | + artifacts/* + playwright-report/ + ARTIFACT_INCLUDE_HIDDEN_FILES: true + SCRIPT_NAME: 'ci-test-e2e' + COMPOSER_DEPS_INSTALL: true + PHP_VERSION: ${{ matrix.php }} + NODE_VERSION: 20 + PLAYWRIGHT_BROWSER_ARGS: 'chromium --with-deps' + secrets: + ENV_FILE_DATA: ${{ secrets.ENV_FILE_DATA }} + COMPOSER_AUTH_JSON: '${{ secrets.PACKAGIST_AUTH_JSON }}' + GITHUB_USER_EMAIL: ${{ secrets.DEPLOYBOT_EMAIL }} + GITHUB_USER_NAME: ${{ secrets.DEPLOYBOT_USER }} + GITHUB_USER_SSH_KEY: ${{ secrets.DEPLOYBOT_SSH_PRIVATE_KEY }} + NPM_REGISTRY_TOKEN: ${{ secrets.DEPLOYBOT_PACKAGES_READ_ACCESS_TOKEN}} +``` + +**Example of secrets:** + +For `ENV_FILE_DATA`: + +```SHELL +TEST_EXEC_KEY=YOUR-KEY +WP_BASE_URL=https://example.com +```