ci(*): more efficient testing #8
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Conditional Testing | |
on: | |
pull_request: | |
branches: ["**"] | |
jobs: | |
check_changes: | |
runs-on: ubuntu-latest | |
outputs: | |
changed_extensions: ${{ steps.set_changed.outputs.changed_extensions }} | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v3 | |
with: | |
fetch-depth: 0 # Fetch all history for all branches and tags | |
ref: ${{ github.head_ref }} # Ensure PR's head is checked out | |
- name: Get changed directories | |
id: set_changed | |
run: | | |
echo "::group::Determining Changed Directories" | |
DIRECTORIES="Firebase auth-mailchimp-sync delete-user-data firestore-bigquery-export firestore-counter firestore-send-email firestore-shorten-urls-bitly firestore-translate-text rtdb-limit-child-nodes storage-resize-images" | |
BASE_SHA=${{ github.event.pull_request.base.sha }} | |
HEAD_SHA=${{ github.event.pull_request.head.sha }} | |
CHANGED_EXTENSIONS="" | |
if ! git diff --name-only $BASE_SHA $HEAD_SHA > /dev/null 2>&1; then | |
echo "Error detecting changes using PR base and head SHAs, falling back to default branch comparison." | |
git fetch --no-tags --depth=1 origin +refs/heads/next:refs/remotes/origin/next | |
BASE_SHA=$(git rev-parse origin/next) | |
fi | |
for dir in $DIRECTORIES; do | |
if git diff --name-only $BASE_SHA $HEAD_SHA | grep -q "^$dir/"; then | |
CHANGED_EXTENSIONS+="$dir " | |
fi | |
done | |
CHANGED_EXTENSIONS_JSON=$(echo "[\"${CHANGED_EXTENSIONS// /\",\"}\"]") | |
echo "changed_extensions=$CHANGED_EXTENSIONS_JSON" >> $GITHUB_OUTPUT | |
echo "::endgroup::" | |
test_extensions: | |
needs: check_changes | |
if: | |
${{ fromJson(needs.check_changes.outputs.changed_extensions).length > 0 }} | |
runs-on: ubuntu-latest | |
strategy: | |
fail-fast: false | |
matrix: | |
extension: | |
${{ fromJson(needs.check_changes.outputs.changed_extensions) }} | |
defaults: | |
run: | |
working-directory: ${{ matrix.extension }}/functions | |
steps: | |
- uses: actions/checkout@v3 | |
- uses: actions/setup-node@v3 | |
with: | |
node-version: "18" | |
cache: "npm" | |
cache-dependency-path: "**/package-lock.json" | |
- run: npm install | |
- run: npm run build | |
- run: npm test |