Skip to content

Commit

Permalink
chore: test matrix idea
Browse files Browse the repository at this point in the history
  • Loading branch information
devops-chris committed Dec 28, 2023
1 parent 7d1f133 commit 9674752
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
63 changes: 63 additions & 0 deletions .github/actions/test_matrix/action.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
name: 'Build Matrix'

description: 'Builds components based on matrix configuration'

inputs:
event: # Event type (e.g., push, pull_request)
required: true

runs:
using: 'composite'
steps:
- name: Check Event Type
id: check-event
run: |
if [ "${{ inputs.event }}" != "push" ] && [ "${{ inputs.event }}" != "pull_request" ]; then
echo "Invalid event type. Only 'push' and 'pull_request' are supported."
exit 1
fi
- name: Set Matrix Outputs
id: set-matrix
run: |
components=()
paths=()
docker_files=()
working_dirs=()
image_names=()
# Check if it's a pull request and get changed files
if [ "${{ inputs.event }}" == "pull_request" ]; then
files=$(git diff --name-only ${{ github.event.before }} ${{ github.sha }})
else
files=$(git diff --name-only HEAD^ HEAD)
fi
# Function to add a component
add_component() {
components+=("$1")
paths+=("$2")
docker_files+=("$3")
working_dirs+=("$4")
image_names+=("$5")
}
# Check if paths of each component are in the changed files
if echo "$files" | grep -qE '^backend/'; then
add_component "Backend" "backend/models/** backend/ops_api/** backend/Dockerfile.ops-api" "Dockerfile.ops-api" "backend" "ops-backend"
fi
if echo "$files" | grep -qE '^backend/data-tools/'; then
add_component "Data-Tools" "backend/data-tools/** backend/Dockerfile.data-tools" "Dockerfile.data-tools" "backend" "ops-data-tools"
fi
if echo "$files" | grep -qE '^frontend/'; then
add_component "Frontend" "frontend/** frontend/Dockerfile.azure" "Dockerfile.azure" "frontend" "ops-frontend"
fi
# Set matrix outputs
echo "::set-output name=matrix::[${components[@]}]"
echo "::set-output name=paths::[${paths[@]}]"
echo "::set-output name=docker_file::[${docker_files[@]}]"
echo "::set-output name=working_dir::[${working_dirs[@]}]"
echo "::set-output name=image_name::[${image_names[@]}]"
35 changes: 35 additions & 0 deletions .github/workflows/test_matrix.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: Build Components

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

jobs:
build:
runs-on: ubuntu-latest

strategy:
matrix:
event: ["push", "pull_request"]

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

- name: Set Matrix and Build
id: set-matrix
uses: ./.github/actions/test_matrix
with:
event: ${{ matrix.event }}

- name: Use Matrix in Another Step
run: |
echo "Component: ${{ matrix.component }}"
echo "Paths: ${{ matrix.paths }}"
echo "Dockerfile: ${{ matrix.docker_file }}"
echo "Working Directory: ${{ matrix.working_dir }}"
echo "Image Name: ${{ matrix.image_name }}"

0 comments on commit 9674752

Please sign in to comment.