From d94c99b70ecd17bd02c088eae297a28f2a5b1954 Mon Sep 17 00:00:00 2001 From: Jacob Hanafin Date: Thu, 9 Jan 2025 10:35:52 -0600 Subject: [PATCH] Test reusable workflows --- .../reusable_integration_testing.yml | 186 ++++++++++++++++++ .github/workflows/rocky.yml | 178 +---------------- 2 files changed, 189 insertions(+), 175 deletions(-) create mode 100644 .github/workflows/reusable_integration_testing.yml diff --git a/.github/workflows/reusable_integration_testing.yml b/.github/workflows/reusable_integration_testing.yml new file mode 100644 index 0000000..8cd73b5 --- /dev/null +++ b/.github/workflows/reusable_integration_testing.yml @@ -0,0 +1,186 @@ +--- +name: Reusable Cluster Deploymet + +on: + workflow_call: + inputs: + os: + required: true + type: string + +jobs: + + cluster-deploy: + runs-on: ubuntu-latest + + steps: + - name: Check out the codebase. + uses: actions/checkout@v2 + + - name: Add Terraform Repository + run: | + wget -O - https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg + echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list + + - name: Set up Python + id: setup_python + uses: actions/setup-python@v2 + with: + python-version: 3.9 + + - name: Install Dependencies + run: | + sudo apt update -y && sudo apt install terraform -y + python -m pip install --upgrade pip + pip3 install ansible pytest-testinfra + + - name: Ansible Version check + run: ansible --version + + - name: Terraform Init + id: init + run: | + cd testing/ + terraform init + + - run: 'echo "$SSH_KEY" > .key' + shell: bash + env: + SSH_KEY: ${{secrets.SSH_PRIVATE_KEY}} + + - run: chmod 400 .key + + - name: Terraform private key + run: cp .key testing/.key + + - name: Terraform Validate + id: validate + run: | + cd testing/ + terraform validate -no-color + + # - name: Terraform Plan + # id: plan + # run: | + # cd testing/ + # terraform plan -var "GITHUB_RUN_ID=$GITHUB_RUN_ID" -var "os=rocky8" -no-color + # continue-on-error: true + # env: + # AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} + # AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + # AWS_DEFAULT_REGION: us-gov-west-1 + + # - name: Terraform Plan Status + # if: steps.plan.outcome == 'failure' + # run: exit 1 + + # - name: Terraform Apply + # run: | + # cd testing/ + # terraform apply -var "GITHUB_RUN_ID=$GITHUB_RUN_ID" -var "os=rocky8" -auto-approve + # env: + # AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} + # AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + # AWS_DEFAULT_REGION: us-gov-west-1 + + # - name: Create inventory + # run: | + # cp -R inventory/rocky8 + + # - name: Create inventory hosts.yml + # run: | + # touch hosts.yml + # echo "rke2_cluster:" > hosts.yml + # echo " children:" >> hosts.yml + # echo " rke2_servers:" >> hosts.yml + # echo " hosts:" >> hosts.yml + # echo " $(aws ec2 describe-instances --filters "Name=instance-state-name,Values=running" "Name=tag:Owner,Values=rke2-ansible-github-actions" "Name=tag:NodeType,Values=Server" "Name=tag:github_run,Values=$GITHUB_RUN_ID" --query "Reservations[*].Instances[*].PublicIpAddress" --output text | head -1):" >> hosts.yml + # echo " rke2_agents:" >> hosts.yml + # echo " hosts:" >> hosts.yml + # echo " $(aws ec2 describe-instances --filters "Name=instance-state-name,Values=running" "Name=tag:Owner,Values=rke2-ansible-github-actions" "Name=tag:NodeType,Values=Agent" "Name=tag:github_run,Values=$GITHUB_RUN_ID" --query "Reservations[*].Instances[*].PublicIpAddress" --output text | head -1):" >> hosts.yml + # echo "all:" >> hosts.yml + # echo " vars:" >> hosts.yml + # echo " rke2_kubernetes_api_server_host: $(aws ec2 describe-instances --filters "Name=instance-state-name,Values=running" "Name=tag:Owner,Values=rke2-ansible-github-actions" "Name=tag:NodeType,Values=Server" "Name=tag:github_run,Values=$GITHUB_RUN_ID" --query "Reservations[*].Instances[*].PrivateIpAddress" --output text | head -1)" >> hosts.yml + # echo "" >> ansible.cfg + # echo "" >> ansible.cfg + # echo "remote_user=centos" >> ansible.cfg + # echo "private_key_file=.key" >> ansible.cfg + # env: + # AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} + # AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + # AWS_DEFAULT_REGION: us-gov-west-1 + + # - name: Check hosts.yml and ansible.cfg + # run: | + # cat hosts.yml + # cat ansible.cfg + + # - name: Run playbook + # run: | + # ANSIBLE_HOST_KEY_CHECKING=False ansible-playbook -i inventory/rocky8/hosts.yml -u centos -vv --private-key .key site.yml + + # - name: Run playbook again for idempotency + # run: | + # ANSIBLE_HOST_KEY_CHECKING=False ansible-playbook -i inventory/rocky8/hosts.yml -u centos -vv --private-key .key site.yml + + # - name: Run Ansible Tests + # run: | + # ANSIBLE_HOST_KEY_CHECKING=False ansible-playbook -i inventory/rocky8/hosts.yml -u centos --verbose --skip-tags "troubleshooting" --private-key .key testing.yml + + # - name: Run Python Tests + # run: | + # export DEFAULT_PRIVATE_KEY_FILE=.key + # pytest --hosts=rke2_servers --ansible-inventory=inventory/rocky8/hosts.yml --force-ansible --connection=ansible --sudo testing/basic_server_tests.py + # pytest --hosts=rke2_agents --ansible-inventory=inventory/rocky8/hosts.yml --force-ansible --connection=ansible --sudo testing/basic_agent_tests.py + + # - name: Create new inventory hosts.yml with added hosts + # run: | + # rm hosts.yml + # touch hosts.yml + # echo "rke2_cluster:" > hosts.yml + # echo " children:" >> hosts.yml + # echo " rke2_servers:" >> hosts.yml + # echo " hosts:" >> hosts.yml + # echo " $(aws ec2 describe-instances --filters "Name=instance-state-name,Values=running" "Name=tag:Owner,Values=rke2-ansible-github-actions" "Name=tag:NodeType,Values=Server" "Name=tag:github_run,Values=$GITHUB_RUN_ID" --query "Reservations[*].Instances[*].PublicIpAddress" --output text | head -1):" >> hosts.yml + # echo " rke2_agents:" >> hosts.yml + # echo " hosts:" >> hosts.yml + # echo " $(aws ec2 describe-instances --filters "Name=instance-state-name,Values=running" "Name=tag:Owner,Values=rke2-ansible-github-actions" "Name=tag:NodeType,Values=Agent" "Name=tag:github_run,Values=$GITHUB_RUN_ID" --query "Reservations[*].Instances[*].PublicIpAddress" --output text | head -1):" >> hosts.yml + # echo " $(aws ec2 describe-instances --filters "Name=instance-state-name,Values=running" "Name=tag:Owner,Values=rke2-ansible-github-actions" "Name=tag:NodeType,Values=ExtraNode" "Name=tag:github_run,Values=$GITHUB_RUN_ID" --query "Reservations[*].Instances[*].PublicIpAddress" --output text | head -1):" >> hosts.yml + # echo "all:" >> hosts.yml + # echo " vars:" >> hosts.yml + # echo " rke2_kubernetes_api_server_host: $(aws ec2 describe-instances --filters "Name=instance-state-name,Values=running" "Name=tag:Owner,Values=rke2-ansible-github-actions" "Name=tag:NodeType,Values=Server" "Name=tag:github_run,Values=$GITHUB_RUN_ID" --query "Reservations[*].Instances[*].PrivateIpAddress" --output text | head -1)" >> hosts.yml + # cp hosts.yml inventory/rocky8/hosts.yml + # env: + # AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} + # AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + # AWS_DEFAULT_REGION: us-gov-west-1 + + # - name: Run playbook again with added hosts + # run: | + # ANSIBLE_HOST_KEY_CHECKING=False ansible-playbook -i inventory/rocky8/hosts.yml -u centos --verbose --private-key .key site.yml + + # - name: Run Ansible Tests with added hosts + # run: | + # ANSIBLE_HOST_KEY_CHECKING=False ansible-playbook -i inventory/rocky8/hosts.yml -u centos --verbose --skip-tags "troubleshooting" --private-key .key testing.yml + + # - name: Run Python Tests with added hosts + # run: | + # export DEFAULT_PRIVATE_KEY_FILE=.key + # pytest --hosts=rke2_servers --ansible-inventory=inventory/rocky8/hosts.yml --force-ansible --connection=ansible --sudo testing/basic_server_tests.py + # pytest --hosts=rke2_agents --ansible-inventory=inventory/rocky8/hosts.yml --force-ansible --connection=ansible --sudo testing/basic_agent_tests.py + + # - name: Run troubleshoot tasks + # if: ${{ failure() }} + # run: | + # ANSIBLE_HOST_KEY_CHECKING=False ansible-playbook -i inventory/rocky8/hosts.yml -u centos -vvv --tags "troubleshooting" --private-key .key testing.yml + + # - name: Delete Stack + # if: ${{ always() }} + # run: | + # cd testing/ + # terraform destroy -var "GITHUB_RUN_ID=$GITHUB_RUN_ID" -auto-approve + # env: + # AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} + # AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + # AWS_DEFAULT_REGION: us-gov-west-1 + \ No newline at end of file diff --git a/.github/workflows/rocky.yml b/.github/workflows/rocky.yml index cb8482f..4b0ec7b 100644 --- a/.github/workflows/rocky.yml +++ b/.github/workflows/rocky.yml @@ -8,178 +8,6 @@ on: jobs: rocky8-test: - name: "8" - runs-on: ubuntu-latest - - steps: - - name: Check out the codebase. - uses: actions/checkout@v2 - - - name: Install Terraform - run: | - wget -O - https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg - echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list - sudo apt update -y && sudo apt install terraform -y - - - name: Terraform Init - id: init - run: | - cd testing/ - terraform init - - - run: 'echo "$SSH_KEY" > .key' - shell: bash - env: - SSH_KEY: ${{secrets.SSH_PRIVATE_KEY}} - - - run: chmod 400 .key - - - name: Terraform private key - run: cp .key testing/.key - - - name: Terraform Validate - id: validate - run: | - cd testing/ - terraform validate -no-color - - - name: Terraform Plan - id: plan - run: | - cd testing/ - terraform plan -var "GITHUB_RUN_ID=$GITHUB_RUN_ID" -var "os=rocky8" -no-color - continue-on-error: true - env: - AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} - AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} - AWS_DEFAULT_REGION: us-gov-west-1 - - - name: Terraform Plan Status - if: steps.plan.outcome == 'failure' - run: exit 1 - - - name: Terraform Apply - run: | - cd testing/ - terraform apply -var "GITHUB_RUN_ID=$GITHUB_RUN_ID" -var "os=rocky8" -auto-approve - env: - AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} - AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} - AWS_DEFAULT_REGION: us-gov-west-1 - - - name: Set up Python - id: setup_python - uses: actions/setup-python@v2 - with: - python-version: 3.9 - - - name: Install dependencies - run: | - python -m pip install --upgrade pip - pip3 install ansible pytest-testinfra - - - name: Ansible Version check - run: ansible --version - - - name: Create inventory hosts.yml - run: | - touch hosts.yml - echo "rke2_cluster:" > hosts.yml - echo " children:" >> hosts.yml - echo " rke2_servers:" >> hosts.yml - echo " hosts:" >> hosts.yml - echo " $(aws ec2 describe-instances --filters "Name=instance-state-name,Values=running" "Name=tag:Owner,Values=rke2-ansible-github-actions" "Name=tag:NodeType,Values=Server" "Name=tag:github_run,Values=$GITHUB_RUN_ID" --query "Reservations[*].Instances[*].PublicIpAddress" --output text | head -1):" >> hosts.yml - echo " rke2_agents:" >> hosts.yml - echo " hosts:" >> hosts.yml - echo " $(aws ec2 describe-instances --filters "Name=instance-state-name,Values=running" "Name=tag:Owner,Values=rke2-ansible-github-actions" "Name=tag:NodeType,Values=Agent" "Name=tag:github_run,Values=$GITHUB_RUN_ID" --query "Reservations[*].Instances[*].PublicIpAddress" --output text | head -1):" >> hosts.yml - echo "all:" >> hosts.yml - echo " vars:" >> hosts.yml - echo " rke2_kubernetes_api_server_host: $(aws ec2 describe-instances --filters "Name=instance-state-name,Values=running" "Name=tag:Owner,Values=rke2-ansible-github-actions" "Name=tag:NodeType,Values=Server" "Name=tag:github_run,Values=$GITHUB_RUN_ID" --query "Reservations[*].Instances[*].PrivateIpAddress" --output text | head -1)" >> hosts.yml - echo "" >> ansible.cfg - echo "" >> ansible.cfg - echo "remote_user=centos" >> ansible.cfg - echo "private_key_file=.key" >> ansible.cfg - env: - AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} - AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} - AWS_DEFAULT_REGION: us-gov-west-1 - - - name: Check hosts.yml and ansible.cfg - run: | - cat hosts.yml - cat ansible.cfg - - - name: Prep inventory - run: | - cp -R inventory/sample inventory/rocky8 - cp hosts.yml inventory/rocky8/hosts.yml - - - name: Run playbook - run: | - ANSIBLE_HOST_KEY_CHECKING=False ansible-playbook -i inventory/rocky8/hosts.yml -u centos -vv --private-key .key site.yml - - - name: Run playbook again for idempotency - run: | - ANSIBLE_HOST_KEY_CHECKING=False ansible-playbook -i inventory/rocky8/hosts.yml -u centos -vv --private-key .key site.yml - - - name: Run Ansible Tests - run: | - ANSIBLE_HOST_KEY_CHECKING=False ansible-playbook -i inventory/rocky8/hosts.yml -u centos --verbose --skip-tags "troubleshooting" --private-key .key testing.yml - - - name: Run Python Tests - run: | - export DEFAULT_PRIVATE_KEY_FILE=.key - pytest --hosts=rke2_servers --ansible-inventory=inventory/rocky8/hosts.yml --force-ansible --connection=ansible --sudo testing/basic_server_tests.py - pytest --hosts=rke2_agents --ansible-inventory=inventory/rocky8/hosts.yml --force-ansible --connection=ansible --sudo testing/basic_agent_tests.py - - - name: Create new inventory hosts.yml with added hosts - run: | - rm hosts.yml - touch hosts.yml - echo "rke2_cluster:" > hosts.yml - echo " children:" >> hosts.yml - echo " rke2_servers:" >> hosts.yml - echo " hosts:" >> hosts.yml - echo " $(aws ec2 describe-instances --filters "Name=instance-state-name,Values=running" "Name=tag:Owner,Values=rke2-ansible-github-actions" "Name=tag:NodeType,Values=Server" "Name=tag:github_run,Values=$GITHUB_RUN_ID" --query "Reservations[*].Instances[*].PublicIpAddress" --output text | head -1):" >> hosts.yml - echo " rke2_agents:" >> hosts.yml - echo " hosts:" >> hosts.yml - echo " $(aws ec2 describe-instances --filters "Name=instance-state-name,Values=running" "Name=tag:Owner,Values=rke2-ansible-github-actions" "Name=tag:NodeType,Values=Agent" "Name=tag:github_run,Values=$GITHUB_RUN_ID" --query "Reservations[*].Instances[*].PublicIpAddress" --output text | head -1):" >> hosts.yml - echo " $(aws ec2 describe-instances --filters "Name=instance-state-name,Values=running" "Name=tag:Owner,Values=rke2-ansible-github-actions" "Name=tag:NodeType,Values=ExtraNode" "Name=tag:github_run,Values=$GITHUB_RUN_ID" --query "Reservations[*].Instances[*].PublicIpAddress" --output text | head -1):" >> hosts.yml - echo "all:" >> hosts.yml - echo " vars:" >> hosts.yml - echo " rke2_kubernetes_api_server_host: $(aws ec2 describe-instances --filters "Name=instance-state-name,Values=running" "Name=tag:Owner,Values=rke2-ansible-github-actions" "Name=tag:NodeType,Values=Server" "Name=tag:github_run,Values=$GITHUB_RUN_ID" --query "Reservations[*].Instances[*].PrivateIpAddress" --output text | head -1)" >> hosts.yml - cp hosts.yml inventory/rocky8/hosts.yml - env: - AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} - AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} - AWS_DEFAULT_REGION: us-gov-west-1 - - - name: Run playbook again with added hosts - run: | - ANSIBLE_HOST_KEY_CHECKING=False ansible-playbook -i inventory/rocky8/hosts.yml -u centos --verbose --private-key .key site.yml - - - name: Run Ansible Tests with added hosts - run: | - ANSIBLE_HOST_KEY_CHECKING=False ansible-playbook -i inventory/rocky8/hosts.yml -u centos --verbose --skip-tags "troubleshooting" --private-key .key testing.yml - - - name: Run Python Tests with added hosts - run: | - export DEFAULT_PRIVATE_KEY_FILE=.key - pytest --hosts=rke2_servers --ansible-inventory=inventory/rocky8/hosts.yml --force-ansible --connection=ansible --sudo testing/basic_server_tests.py - pytest --hosts=rke2_agents --ansible-inventory=inventory/rocky8/hosts.yml --force-ansible --connection=ansible --sudo testing/basic_agent_tests.py - - - name: Run troubleshoot tasks - if: ${{ failure() }} - run: | - ANSIBLE_HOST_KEY_CHECKING=False ansible-playbook -i inventory/rocky8/hosts.yml -u centos -vvv --tags "troubleshooting" --private-key .key testing.yml - - - name: Delete Stack - if: ${{ always() }} - run: | - cd testing/ - terraform destroy -var "GITHUB_RUN_ID=$GITHUB_RUN_ID" -auto-approve - env: - AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} - AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} - AWS_DEFAULT_REGION: us-gov-west-1 - \ No newline at end of file + uses: ./.github/workflows/reusable_integration_testing.yml + with: + os: rocky8 \ No newline at end of file