Skip to content

Commit

Permalink
feat(setup-magento): create unified setup action
Browse files Browse the repository at this point in the history
Adds a unified setup action for stores and extensions, making the CI
environment more consistent.
  • Loading branch information
damienwebdev committed Oct 30, 2022
1 parent f5d43a5 commit f1abf7e
Show file tree
Hide file tree
Showing 3 changed files with 304 additions and 0 deletions.
125 changes: 125 additions & 0 deletions .github/workflows/_internal-setup-magento.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
name: Setup Magento Test

on:
workflow_dispatch: {}
push:
branches:
- main
paths:
- "setup-magento/**"
- ".github/workflows/_internal-setup-magento.yaml"
- "supported-version/**"
- "!(**/*.md)"
pull_request:
branches:
- main
paths:
- "setup-magento/**"
- ".github/workflows/_internal-setup-magento.yaml"
- "supported-version/**"
- "!(**/*.md)"

env:
PSEUDO_REPO_FOLDER: ../magento_repo
magento_folder: ../magento2

jobs:
compute_matrix:
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.supported-version.outputs.matrix }}
steps:
- uses: actions/checkout@v3
- uses: ./supported-version
with:
kind: currently-supported
id: supported-version
- run: echo ${{ steps.supported-version.outputs.matrix }}

setup-magento-store:
needs: compute_matrix
strategy:
matrix: ${{ fromJSON(needs.compute_matrix.outputs.matrix) }}
fail-fast: false
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- run: |
PSEUDO_STORE_FULL_PATH=$(realpath "${{ env.PSEUDO_REPO_FOLDER }}")
echo "PSEUDO_STORE_FULL_PATH=$PSEUDO_STORE_FULL_PATH" >> $GITHUB_ENV
name: Generate Full Pseudo Store Path
shell: bash
- name: Set PHP Version
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
tools: composer:v${{ matrix.composer }}

- uses: actions/cache@v3
id: setup-magento-store-cache
with:
key: setup-magento-ci | ${{ runner.os }} | ${{ matrix.magento }}
path: ${{ env.PSEUDO_STORE_FULL_PATH }}

- run: composer create-project --repository-url="https://mirror.mage-os.org" "${{ matrix.magento }}" "${{ env.PSEUDO_REPO_FOLDER }}" --no-install
name: Create Store to simulate a real Magento store in a real repo.
if: steps.setup-magento-store-cache.outputs.cache-hit != 'true'

- uses: ./fix-magento-install
name: Fix Magento Out of Box Install Issues
with:
magento_directory: ${{ env.PSEUDO_REPO_FOLDER }}
if: steps.setup-magento-store-cache.outputs.cache-hit != 'true'

- run: composer install
shell: bash
working-directory: "${{ env.PSEUDO_REPO_FOLDER }}"
if: steps.setup-magento-store-cache.outputs.cache-hit != 'true'

- run: git init && git config user.email "you@example.com" && git config user.name "Your Name" && git add . && git commit -m "init" && git clean -fdx
working-directory: "${{ env.PSEUDO_REPO_FOLDER }}"
if: steps.setup-magento-store-cache.outputs.cache-hit != 'true'

- run: cp -R ${{ env.PSEUDO_REPO_FOLDER }} ${{ env.magento_folder }}
shell: bash

- uses: ./setup-magento
with:
php-version: ${{ matrix.php }}
tools: composer:v${{ matrix.composer }}
mode: store
working-directory: ${{ env.magento_folder }}

- uses: graycoreio/github-actions-magento2/cache-magento@cache_magento
with:
mode: 'store'
composer_cache_key: '${{ matrix.magento }}'

- run: composer install
name: Composer install
shell: bash
working-directory: ${{ steps.setup-magento.outputs.path }}

setup-magento-extension:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- uses: ./setup-magento
with:
php-version: 8.1
tools: composer:v2
mode: extension
magento_version: 2.4.5-p1

- uses: graycoreio/github-actions-magento2/cache-magento@cache_magento
with:
mode: 'extension'
composer_cache_key: 'magento-2.4.5-p1'

- run: composer install
name: Composer install
shell: bash
working-directory: ${{ steps.setup-magento.outputs.path }}
79 changes: 79 additions & 0 deletions setup-magento/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# Magento 2 Package Installation Test Action

A Github Action that sets Magento up to the point of composer install.

## Inputs

See the [action.yml](./action.yml)

## Usage

### Stores

```yml
name: Setup Magento Store

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
setup-magento-store:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: ./setup-magento
with:
php-version: 8.1
tools: composer:v2
mode: store
working-directory: $GITHUB_WORKSPACE

- run: composer install
name: Composer install
shell: bash
working-directory: ${{ steps.setup-magento.outputs.path }}
```

### Extensions

```yml
name: Setup Magento Store

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
setup-magento-extension:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- uses: ./setup-magento
with:
php-version: 8.1
tools: composer:v2
mode: extension
magento_version: 2.4.5-p1

- run: composer config repositories.local path $GITHUB_WORKSPACE
name: Add Github Repo for Testing
working-directory: ${{ steps.setup-magento.outputs.path }}
shell: bash

- run: composer require my/package "@dev"
name: Attempt install
working-directory: ${{ steps.setup-magento.outputs.path }}
shell: bash
env:
COMPOSER_AUTH: ${{ secrets.composer_auth }}
```
100 changes: 100 additions & 0 deletions setup-magento/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
name: "Setup Magento"
author: "Graycore"
description: "This action sets up a Magento instance for further actions like running tests, etc."

inputs:
php-version:
description: "Setup PHP version."
default: "8.1"
required: true

tools:
description: "Setup popular tools globally."
required: false

extensions:
description: "Setup PHP extensions."
required: false

coverage:
description: "Setup code coverage driver."
required: false

magento_repository:
required: false
default: "https://mirror.mage-os.org/"
description: "Where to install Magento from"

magento_version:
required: false
default: '~2.4.5'
description: "The version of Magento to use. This is only relevant if you are testing an extension."

apply_fixes:
required: false
default: 'false'
description: "Whether or not to apply fixes during setup."

mode:
required: true
default: 'extension'
description: "The mode for setup, one of: `extension` or `store`."

working-directory:
required: false
default: "."
description: "The working directory to run the action in."

outputs:
path:
description: "The absolute path to where Magento was set up."
value: ${{ steps.setup-magento-get-magento-path.outputs.path }}

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

- run: |
MAGENTO_DIRECTORY=""
if [ "${{ inputs.mode }}" = 'extension' ]; then
MAGENTO_DIRECTORY="../magento2"
else
MAGENTO_DIRECTORY="${{ inputs.working_directory }}"
fi
echo "MAGENTO_DIRECTORY=$MAGENTO_DIRECTORY" >> $GITHUB_OUTPUT
id: setup-magento-compute-directory
shell: bash
- run: |
mkdir -p ${{ steps.setup-magento-compute-directory.outputs.MAGENTO_DIRECTORY }}
name: Make a directory that may not exist.
shell: bash
if: inputs.mode == 'extension'
- run: composer create-project --repository-url="${{ inputs.magento_repository }}" "${{ inputs.magento_version }}" ${{ steps.setup-magento-compute-directory.outputs.MAGENTO_DIRECTORY }} --no-install
working-directory: ${{ inputs.working-directory }}
shell: bash
name: Create Magento ${{ inputs.magento_version }} Project
if: inputs.mode == 'extension'

- uses: graycoreio/github-actions-magento2/fix-magento-install@fix_magento_action
name: Fix Magento Out of Box Install Issues
with:
magento_directory: ${{ steps.setup-magento-compute-directory.outputs.MAGENTO_DIRECTORY }}
if: inputs.mode == 'extension' || inputs.apply_fixes == 'true'

- run: |
echo "path=$(realpath ${{ steps.setup-magento-compute-directory.outputs.MAGENTO_DIRECTORY }})" >> $GITHUB_OUTPUT
shell: bash
id: setup-magento-get-magento-path
branding:
icon: "code"
color: "green"

0 comments on commit f1abf7e

Please sign in to comment.