From 07ac9a0647bf218ac90c6171dd522a9ca1ed8534 Mon Sep 17 00:00:00 2001 From: Andy Wu Date: Mon, 21 Oct 2024 16:34:33 -0700 Subject: [PATCH] [feat] add reusable-check-network workflow --- .github/workflows/reusable-check-network.yml | 70 ++++++++++++++ .../workflows/reusable-fetch-bastion-ips.yml | 94 +++++++++++++++++++ 2 files changed, 164 insertions(+) create mode 100644 .github/workflows/reusable-check-network.yml create mode 100644 .github/workflows/reusable-fetch-bastion-ips.yml diff --git a/.github/workflows/reusable-check-network.yml b/.github/workflows/reusable-check-network.yml new file mode 100644 index 0000000..ca675eb --- /dev/null +++ b/.github/workflows/reusable-check-network.yml @@ -0,0 +1,70 @@ +name: Check Network File Changes + +on: + workflow_call: + secrets: + token: + required: true + outputs: + devnet_changed: + description: 'Whether the devnet network files were changed' + value: ${{ jobs.check-network-changes.outputs.devnet_changed }} + testnet_changed: + description: 'Whether the testnet network files were changed' + value: ${{ jobs.check-network-changes.outputs.testnet_changed }} + +jobs: + check-network-changes: + runs-on: ubuntu-latest + outputs: + devnet_changed: ${{ steps.check_changes.outputs.devnet_changed }} + testnet_changed: ${{ steps.check_changes.outputs.testnet_changed }} + + steps: + - uses: jitterbit/get-changed-files@v1 + id: changed_files + with: + format: space-delimited + token: ${{ secrets.token }} + + - name: Determine if relevant files changed and retrieve network types + id: check_changes + run: | + CHANGED_FILES="${{ steps.changed_files.outputs.modified }}" + echo "Changed files: $CHANGED_FILES" + + # Define the files we care about + AUTHORIZED_KEYS_DEVNET="authorized_keys_odyssey_devnet" + AUTHORIZED_KEYS_TESTNET="authorized_keys_odyssey_testnet" + BASTION_ACCESS_DEVNET="bastion-access-devnet.yml" + BASTION_ACCESS_TESTNET="bastion-access-testnet.yml" + + # Initialize flags for network types + DEVNET_CHANGED=false + TESTNET_CHANGED=false + + # Check if any of the files were modified and set the flags + if echo "$CHANGED_FILES" | grep -q "$AUTHORIZED_KEYS_DEVNET"; then + DEVNET_CHANGED=true + echo "authorized_keys for devnet changed" + fi + if echo "$CHANGED_FILES" | grep -q "$AUTHORIZED_KEYS_TESTNET"; then + TESTNET_CHANGED=true + echo "authorized_keys for testnet changed" + fi + if echo "$CHANGED_FILES" | grep -q "$BASTION_ACCESS_DEVNET"; then + DEVNET_CHANGED=true + echo "bastion access for devnet changed" + fi + if echo "$CHANGED_FILES" | grep -q "$BASTION_ACCESS_TESTNET"; then + TESTNET_CHANGED=true + echo "bastion access for testnet changed" + fi + + # Output the values for use in subsequent steps + echo "devnet_changed=$DEVNET_CHANGED" >> $GITHUB_OUTPUT + echo "testnet_changed=$TESTNET_CHANGED" >> $GITHUB_OUTPUT + + # Print the results for verification + echo "DevNet changed: $DEVNET_CHANGED" + echo "TestNet changed: $TESTNET_CHANGED" diff --git a/.github/workflows/reusable-fetch-bastion-ips.yml b/.github/workflows/reusable-fetch-bastion-ips.yml new file mode 100644 index 0000000..367359b --- /dev/null +++ b/.github/workflows/reusable-fetch-bastion-ips.yml @@ -0,0 +1,94 @@ +name: Fetch Bastion Host IPs + +on: + workflow_call: + inputs: + devnet_changed: + description: "Boolean to check if DevNet files have changed" + required: true + type: boolean + testnet_changed: + description: "Boolean to check if TestNet files have changed" + required: true + type: boolean + instance_name_devnet: + description: "The instance name for Odyssey DevNet Bastion" + required: true + type: string + instance_name_testnet: + description: "The instance name for Odyssey TestNet Bastion" + required: true + type: string + role_bastion: + description: "The role for the Bastion host (default: bastion)" + required: true + type: string + default: "bastion" + instance_region: + description: "The region to search for instances" + required: true + type: string + +jobs: + fetch_bastion_ips: + runs-on: ubuntu-latest + outputs: + instance_ip_odyssey_devnet: ${{ steps.get_instance_ips.outputs.instance_ip_odyssey_devnet }} + instance_ip_odyssey_testnet: ${{ steps.get_instance_ips.outputs.instance_ip_odyssey_testnet }} + + steps: + - name: Fetch Bastion Host IPs + id: get_instance_ips + run: | + DEVNET_CHANGED="${{ inputs.devnet_changed }}" + TESTNET_CHANGED="${{ inputs.testnet_changed }}" + INSTANCE_NAME_ODYSSEY_DEVNET="${{ inputs.instance_name_devnet }}" + INSTANCE_NAME_ODYSSEY_TESTNET="${{ inputs.instance_name_testnet }}" + ROLE_BASTION="${{ inputs.role_bastion }}" + INSTANCE_REGION="${{ inputs.instance_region }}" + + # Fetch DevNet Bastion IP if changes are detected + if [ "$DEVNET_CHANGED" = "true" ]; then + echo "Fetching DevNet Bastion IP" + INSTANCE_IP_ODYSSEY_DEVNET=$(aws ec2 describe-instances \ + --region $INSTANCE_REGION \ + --filters "Name=tag:Network,Values=$INSTANCE_NAME_ODYSSEY_DEVNET" "Name=tag:Role,Values=$ROLE_BASTION" \ + --query "Reservations[].Instances[].PublicIpAddress" \ + --output text) + + if [ -n "$INSTANCE_IP_ODYSSEY_DEVNET" ]; then + echo "Odyssey DevNet Instance IP: $INSTANCE_IP_ODYSSEY_DEVNET" + echo "instance_ip_odyssey_devnet=$INSTANCE_IP_ODYSSEY_DEVNET" >> $GITHUB_OUTPUT + else + echo "Failed to fetch DevNet IP or no instance found" + fi + else + echo "DevNet unchanged, skipping IP fetch" + fi + + # Fetch TestNet Bastion IP if changes are detected + if [ "$TESTNET_CHANGED" = "true" ]; then + echo "Fetching TestNet Bastion IP" + INSTANCE_IP_ODYSSEY_TESTNET=$(aws ec2 describe-instances \ + --region $INSTANCE_REGION \ + --filters "Name=tag:Network,Values=$INSTANCE_NAME_ODYSSEY_TESTNET" "Name=tag:Role,Values=$ROLE_BASTION" \ + --query "Reservations[].Instances[].PublicIpAddress" \ + --output text) + + if [ -n "$INSTANCE_IP_ODYSSEY_TESTNET" ]; then + echo "Odyssey TestNet Instance IP: $INSTANCE_IP_ODYSSEY_TESTNET" + echo "instance_ip_odyssey_testnet=$INSTANCE_IP_ODYSSEY_TESTNET" >> $GITHUB_OUTPUT + else + echo "Failed to fetch TestNet IP or no instance found" + fi + else + echo "TestNet unchanged, skipping IP fetch" + fi + + # Display the fetched IPs (if any) + if [ -n "$INSTANCE_IP_ODYSSEY_DEVNET" ]; then + echo "Odyssey DevNet Instance IP: $INSTANCE_IP_ODYSSEY_DEVNET" + fi + if [ -n "$INSTANCE_IP_ODYSSEY_TESTNET" ]; then + echo "Odyssey TestNet Instance IP: $INSTANCE_IP_ODYSSEY_TESTNET" + fi