Skip to content
Open
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
7 changes: 7 additions & 0 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,13 @@ jobs:
- name: Run Jest
run: "npm run test"

shell-testing:
runs-on: "ubuntu-latest"
steps:
- uses: "actions/checkout@v6"
- name: Run shell tests
run: bash scripts/entrypoint.spec.sh

package-lock-up-to-date:
runs-on: "ubuntu-latest"
steps:
Expand Down
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,30 @@ jobs:

Generally, you will use this as a dependency of a job that uses [laminas/laminas-continuous-integration-action](https://github.com/laminas/laminas-continuous-integration-action), as demonstrated in the above configuration.

### Monorepo Support

For monorepo setups where packages are located in subdirectories, use the `working-directory` input:

```yaml
jobs:
matrix:
name: Generate job matrix
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.matrix.outputs.matrix }}
steps:
- name: Gather CI configuration
id: matrix
uses: laminas/laminas-ci-matrix-action@v1
with:
working-directory: packages/my-package
```

When `working-directory` is specified:
- The action looks for `composer.json`, `.laminas-ci.json`, and other configuration files in that directory
- For pull requests, only files changed within the working directory are considered for selective checks
- File paths in the diff are relative to the working directory

> ### actions/checkout not required
>
> An actions/checkout step prior to this action is not required, as it will perform a checkout into the WORKDIR on its own if none has been performed previously.
Expand Down
5 changes: 5 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ description: 'Creates a continuous integration matrix for use with laminas-conti
branding:
icon: 'share-2'
color: 'blue'
inputs:
working-directory:
description: 'Working directory for monorepo support. The action will look for composer.json and other configuration files in this directory.'
required: false
default: '.'
outputs:
config:
description: >-
Expand Down
10 changes: 9 additions & 1 deletion entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,18 @@ git config --global --add safe.directory /github/workspace
checkout

DIFF=
WORKING_DIR="${INPUT_WORKING_DIRECTORY:-.}"

# Change to working directory
if [[ "$WORKING_DIR" != "." ]];then
echo "Changing to working directory: ${WORKING_DIR}"
cd "${WORKING_DIR}" || { echo "Failed to change to working directory: ${WORKING_DIR}"; exit 1; }
fi

if [[ "$GITHUB_EVENT_NAME" == "pull_request" ]];then
echo "Preparing file diff"
DIFF=$(git diff --name-only "$GITHUB_BASE_REF"...HEAD)
# Use --relative to get paths relative to current working directory
DIFF=$(git diff --relative --name-only "$GITHUB_BASE_REF"...HEAD)
fi

if [[ "$DIFF" != "" ]];then
Expand Down
102 changes: 102 additions & 0 deletions scripts/entrypoint.spec.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#!/bin/bash
# Test script for entrypoint.sh working-directory functionality
# This tests the git diff --relative behavior with working directories

set -e

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m' # No Color

TESTS_PASSED=0
TESTS_FAILED=0

# Helper function to run a test
run_test() {
local test_name="$1"
local expected="$2"
local actual="$3"

if [[ "$expected" == "$actual" ]]; then
echo -e "${GREEN}PASS${NC}: $test_name"
TESTS_PASSED=$((TESTS_PASSED + 1))
else
echo -e "${RED}FAIL${NC}: $test_name"
echo " Expected: $expected"
echo " Actual: $actual"
TESTS_FAILED=$((TESTS_FAILED + 1))
fi
}

# Create a temporary directory for testing
TEST_DIR=$(mktemp -d)
trap "rm -rf $TEST_DIR" EXIT

cd "$TEST_DIR"

# Initialize a git repo with a monorepo structure
git init -q
git config user.email "test@test.com"
git config user.name "Test User"
git config commit.gpgsign false

# Create monorepo structure
mkdir -p packages/package-a/src
mkdir -p packages/package-b/src
echo '{"require":{"php":"^8.1"}}' > packages/package-a/composer.json
echo '{"require":{"php":"^8.2"}}' > packages/package-b/composer.json
echo "<?php echo 'A';" > packages/package-a/src/ClassA.php
echo "<?php echo 'B';" > packages/package-b/src/ClassB.php
echo "Root file" > root.txt

# Initial commit
git add .
git commit -q -m "Initial commit"

# Create a branch and make changes
git checkout -q -b feature-branch

# Modify files in both packages and root
echo "<?php echo 'A modified';" > packages/package-a/src/ClassA.php
echo "<?php echo 'B modified';" > packages/package-b/src/ClassB.php
echo "Root modified" > root.txt
git add .
git commit -q -m "Modify files"

# Test 1: git diff --relative from root shows all files
DIFF_ROOT=$(git diff --relative --name-only main...HEAD 2>/dev/null || git diff --relative --name-only master...HEAD)
run_test "git diff from root shows all files" "3" "$(echo "$DIFF_ROOT" | wc -l | tr -d ' ')"

# Test 2: git diff --relative from package-a shows only package-a files
cd packages/package-a
DIFF_PKG_A=$(git diff --relative --name-only main...HEAD 2>/dev/null || git diff --relative --name-only master...HEAD)
run_test "git diff --relative from package-a shows only package-a files" "src/ClassA.php" "$DIFF_PKG_A"
cd "$TEST_DIR"

# Test 3: git diff --relative from package-b shows only package-b files
cd packages/package-b
DIFF_PKG_B=$(git diff --relative --name-only main...HEAD 2>/dev/null || git diff --relative --name-only master...HEAD)
run_test "git diff --relative from package-b shows only package-b files" "src/ClassB.php" "$DIFF_PKG_B"
cd "$TEST_DIR"

# Test 4: Verify INPUT_WORKING_DIRECTORY default behavior
INPUT_WORKING_DIRECTORY=""
WORKING_DIR="${INPUT_WORKING_DIRECTORY:-.}"
run_test "Default working directory is '.'" "." "$WORKING_DIR"

# Test 5: Verify INPUT_WORKING_DIRECTORY custom value
INPUT_WORKING_DIRECTORY="packages/package-a"
WORKING_DIR="${INPUT_WORKING_DIRECTORY:-.}"
run_test "Custom working directory is set correctly" "packages/package-a" "$WORKING_DIR"

# Print summary
echo ""
echo "================================"
echo "Tests passed: $TESTS_PASSED"
echo "Tests failed: $TESTS_FAILED"
echo "================================"

if [[ $TESTS_FAILED -gt 0 ]]; then
exit 1
fi
Loading