diff --git a/.github/workflows/sync_modules.yml b/.github/workflows/sync_modules.yml new file mode 100644 index 0000000..d7a268b --- /dev/null +++ b/.github/workflows/sync_modules.yml @@ -0,0 +1,40 @@ +name: Synchorize Modules Version +on: + workflow_dispatch: + pull_request: + types: [opened, reopened, synchronize, labeled, unlabeled] +jobs: + management_layer: + runs-on: ubuntu-latest + 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: Checkout + uses: actions/checkout@v2 + - name: Create branch + run: git checkout -b sync-management-layer-modules + #- name: Clone le-tf-infra-aws repo + # run: | + # cd ../ + # git clone git@github.com:binbashar/le-tf-infra-aws.git + #- name: Sync modules + # run: | + # cd le-tf-infra-aws-template + # chmod +x sync_modules.sh + # ./sync_modules.sh ${{ matrix.modules[0] }} ${{ matrix.modules[1] }} + # git add ${{ matrix.modules[1] }} + # git commit -m "Sync ${{ matrix.modules[1] }} module" + - name: Create PR + uses: peter-evans/create-pull-request@v7 + with: + token: ${{ secrets.GITHUB_TOKEN }} + commit-message: "Sync ${{ matrix.modules[1] }} module" + title: "Sync Management Layer modules" + body: "Sync ${{ matrix.modules[0] }} and ${{ matrix.modules[1] }} modules" + branch: "sync-management-layer-modules" + base: "master" + labels: "enhancement" diff --git a/sync_modules.sh b/sync_modules.sh new file mode 100755 index 0000000..6c627e6 --- /dev/null +++ b/sync_modules.sh @@ -0,0 +1,64 @@ +#! /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 # +# 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 " + 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" le-tf-infra-aws-template/template/management/global/sso/account_assignments.tf + echo "Updated the version in the template repository file to $INFRA_VERSION" + exit 0 +fi