Skip to content

Commit

Permalink
feat(SYNC-MODULES): Add modules syncronization with le-tf-infra-aws repo
Browse files Browse the repository at this point in the history
  • Loading branch information
renemontilva committed Nov 18, 2024
1 parent 37c554d commit 9c0709e
Show file tree
Hide file tree
Showing 2 changed files with 162 additions and 0 deletions.
97 changes: 97 additions & 0 deletions .github/workflows/sync-modules.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
name: Synchronize Module Versions
on:
workflow_dispatch:
pull_request:
types: [opened, reopened, synchronize, labeled, unlabeled]
jobs:
clone_repositories:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
path: le-tf-infra-aws-template
- name: Checkout le-tf-infra-aws repo
uses: actions/checkout@v4
with:
repository: "binbashar/le-tf-infra-aws"
path: le-tf-infra-aws
- name: Upload Workspace
uses: actions/upload-artifact@v4
with:
path: ${{ github.workspace }}
name: template-${{ github.run_id }}
include-hidden-files: true
management_layer:
runs-on: ubuntu-latest
needs: clone_repositories
strategy:
matrix:
modules: [
["management/global/sso/account_assignments.tf", "management/global/sso/account_assignments.tf"],
["management/us-east-1/base-tf-backend/main.tf", "management/primary_region/base-tf-backend/main.tf"]
]
steps:
- name: Download Workspace
uses: actions/download-artifact@v4
with:
name: template-${{ github.run_id }}
- name: list files
run: |
pwd
ls -la
- name: Sync modules
run: |
cd le-tf-infra-aws-template
chmod +x sync_modules.sh
./sync_modules.sh ../le-tf-infra-aws/${{ matrix.modules[0] }} template/${{ matrix.modules[1] }}
- name: Generate Artifact Name
id: artifact_name
run: |
ARTIFACT=$(echo ${{ matrix.modules[1] }} | sed 's/\//_/g')
echo "ARTIFACT=$ARTIFACT" >> $GITHUB_OUTPUT
- name: Upload Workspace
uses: actions/upload-artifact@v4
with:
path: ${{ github.workspace }}
name: pull-request-${{ steps.artifact_name.outputs.ARTIFACT }}
include-hidden-files: true
pull_request:
runs-on: ubuntu-latest
needs: management_layer
steps:
- name: Download Workspace
uses: actions/download-artifact@v4
with:
path: ${{ github.workspace }}
pattern: pull-request-*
merge-multiple: true
- name: list files
run: |
pwd
ls -la
- name: Git status
run: |
cd le-tf-infra-aws-template
ls -lha
#- name: Create PR
# uses: peter-evans/create-pull-request@v7
# with:
# path: le-tf-infra-aws-template
# commit-message: "Sync ${{ matrix.modules[1] }} module"
# title: "Sync Management Layer modules"
# body: |
# ## What?
# * Syncronize the management layer terraform modules versions with the le-tf-infra-aws repository.
#
# ## Why?
# * To keep the management layer modules up to date with the le-tf-infra-aws repository.
#
# ## References
# * [le-tf-infra-aws]()
# branch: "sync-management-layer-modules"
# branch-suffix: timestamp
# base: "master"
# labels: |
# enhancement
# patch
65 changes: 65 additions & 0 deletions sync_modules.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#! /bin/bash

#################################################################################################################################################
# This script is used to compare module versions and update the template file if the versions do not match #
# Usage: ./sync_modules.sh <infra_file> <template_file> #
# Example: ./sync_modules.sh le-tf-infra-aws/management/global/sso/account_assignments.tf template/management/global/sso/account_assignments.tf #
#################################################################################################################################################

# management/global/sso/account_assignments.tf
# management/primary_region/base-tf-backend/main.tf
# security/primary_region/base-tf-backend/main.tf
# security/primary_region/security-base/account.tf
# shared/primary_region/base-network/network_vpc_flow_logs.tf
# shared/primary_region/base-network/network.tf
# shared/primary_region/base-tf-backend/main.tf

# Check if the number of arguments is correct
if [ "$#" -ne 2 ]; then
echo "Usage: $0 <infra_file> <template_file>"
exit 1
fi

# print message that initiate the comparison
echo -e "Initiating comparison:\nInfra file: $1\nTemplate file: $2\n"

# It should comtains the path of the infra file and the template file
INFRA_FILE=$1
TEMPLATE_FILE=$2

if [ -z $TEMPLATE_FILE ]; then
echo "Template file does not exist"
exit 1
fi

if [ -z $INFRA_FILE ]; then
echo "Infra file does not exist"
exit 1
fi

TEMPLATE_VERSION=$(cat $TEMPLATE_FILE | grep -oP 'ref=\K[^"]*')
INFRA_VERSION=$(cat $INFRA_FILE | grep -oP 'ref=\K[^"]*')

if [ -z $INFRA_VERSION ]; then
echo "Infra Version value taken from the files is empty"
exit 1
fi

if [ -z $TEMPLATE_VERSION ]; then
echo "Template Version value taken from the files is empty"
exit 1
fi

echo "Current Template Version: $TEMPLATE_VERSION"
echo "Current Infra Version: $INFRA_VERSION"

if [ "$TEMPLATE_VERSION" == "$INFRA_VERSION" ]; then
echo "Versions match, no need to update"
exit 0
else
echo "Versions do not match, updating on template repository"
sed -i "s/ref=$TEMPLATE_VERSION/ref=$INFRA_VERSION/g" $TEMPLATE_FILE
git add $TEMPLATE_FILE
echo "Updated the version in the template repository file to $INFRA_VERSION"
exit 0
fi

0 comments on commit 9c0709e

Please sign in to comment.