Skip to content

Commit

Permalink
Make ECS generation GHA work with multiple modules at once
Browse files Browse the repository at this point in the history
  • Loading branch information
QU3B1M committed Dec 13, 2024
1 parent bad34a6 commit 883db15
Showing 1 changed file with 42 additions and 25 deletions.
67 changes: 42 additions & 25 deletions .github/workflows/generate-ecs-mappings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,32 @@ jobs:
- name: Set up Docker Compose
run: sudo apt-get install docker-compose

- name: Extract ECS Module and Run ECS Generator
- name: Extract ECS Modules and Run ECS Generator
id: run-ecs-generator
run: |
# Fetch base branch
git fetch origin +refs/heads/master:refs/remotes/origin/master
# Extract the ECS module name from the modified files
# Extract the ECS module names from the modified files
modified_files=$(git diff --name-only origin/master)
ecs_module=""
updated_modules=()
for file in $modified_files; do
if [[ $file == ecs/* ]]; then
ecs_module=$(echo $file | cut -d'/' -f2)
break
if [[ ! " ${updated_modules[*]} " =~ " ${ecs_module} " ]]; then
updated_modules+=("$ecs_module")
fi
fi
done
if [[ -n "$ecs_module" ]]; then
# Run the ECS generator script
bash docker/ecs/mapping-generator.sh run "$ecs_module"
echo "ecs_module=$ecs_module" >> $GITHUB_ENV
if [[ ${#updated_modules[@]} -gt 0 ]]; then
export REPO_PATH=$(pwd)
for ecs_module in "${updated_modules[@]}"; do
# Run the ECS generator script for each module
bash docker/ecs/mapping-generator.sh run "$ecs_module"
echo "Processed ECS module: $ecs_module"
done
echo "updated_modules=${updated_modules[*]}" >> $GITHUB_ENV
else
echo "No modifications detected in ecs/ directory."
exit 0
Expand All @@ -55,7 +61,7 @@ jobs:
uses: actions/upload-artifact@v4
with:
name: ecs-template
path: ecs/${{ env.ecs_module }}/mappings/v8.11.0/generated/elasticsearch/legacy/template.json
path: ecs/**/mappings/v8.11.0/generated/elasticsearch/legacy/template.json

- name: Checkout target repository
uses: actions/checkout@v4
Expand All @@ -64,9 +70,9 @@ jobs:
token: ${{ secrets.GITHUB_TOKEN }}
path: wazuh-indexer-plugins

- name: Copy generated file to target repository
- name: Copy generated files to target repository
run: |
# Map ECS module to target JSON filename
# Map ECS modules to target JSON filenames
declare -A module_to_file=(
[agent]="index-template-agent.json"
[alerts]="index-template-alerts.json"
Expand All @@ -83,31 +89,42 @@ jobs:
[vulnerabilities]="index-template-vulnerabilities.json"
)
target_file=${module_to_file[${{ env.ecs_module }}]}
if [[ -z "$target_file" ]]; then
echo "No corresponding file for module ${{ env.ecs_module }}"
exit 1
fi
for ecs_module in ${updated_modules[@]}; do
target_file=${module_to_file[$ecs_module]}
if [[ -z "$target_file" ]]; then
echo "No corresponding file for module $ecs_module"
exit 1
fi
mkdir -p wazuh-indexer-plugins/plugins/setup/src/main/resources/
cp ecs/${{ env.ecs_module }}/mappings/v8.11.0/generated/elasticsearch/legacy/template.json wazuh-indexer-plugins/plugins/setup/src/main/resources/$target_file
mkdir -p wazuh-indexer-plugins/plugins/setup/src/main/resources/
cp ecs/$ecs_module/mappings/v8.11.0/generated/elasticsearch/legacy/template.json wazuh-indexer-plugins/plugins/setup/src/main/resources/$target_file
done
- name: Commit and push changes
run: |
cd wazuh-indexer-plugins
git config --global user.email "[email protected]"
git config --global user.name "GitHub Actions"
git checkout -b update-ecs-template-${{ env.ecs_module }}
branch_name="update-ecs-templates"
# Check if branch exists
if git ls-remote --heads origin $branch_name | grep $branch_name; then
git checkout $branch_name
else
git checkout -b $branch_name
fi
git add .
git commit -m "Update ECS template for module ${{ env.ecs_module }}"
git push origin update-ecs-template-${{ env.ecs_module }}
git commit -m "Update ECS templates for modified modules: $updated_modules"
git push origin $branch_name
- name: Create Pull Request
uses: peter-evans/create-pull-request@v4
with:
token: ${{ secrets.GITHUB_TOKEN }}
commit-message: "Update ECS template for module ${{ env.ecs_module }}"
branch: update-ecs-template-${{ env.ecs_module }}
title: "Update ECS template for module ${{ env.ecs_module }}"
body: "This PR updates the ECS template for the ${{ env.ecs_module }} module."
commit-message: "Update ECS templates for modified modules: $updated_modules"
branch: update-ecs-templates
title: "Update ECS templates for modified modules: $updated_modules"
body: "This PR updates the ECS templates for the following modules: $updated_modules."
base: master

0 comments on commit 883db15

Please sign in to comment.