githubofkrishnadhas creating vnet with name ARCHITECTS-UEAST-AKS-VNET in centralindia #25
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: azure-virtual-network-creation | |
permissions: | |
contents: write | |
on: | |
workflow_dispatch: | |
inputs: | |
application_name: | |
required: true | |
type: string | |
description: "Azure Application Name" | |
environment: | |
required: true | |
type: string | |
description: "Azure Environment" | |
temporary: | |
required: true | |
type: choice | |
options: | |
- "TRUE" | |
- "FALSE" | |
description: "Azure temporary tag" | |
location: | |
required: true | |
type: string | |
description: "Azure location" | |
resource_group_name: | |
required: true | |
type: string | |
description: "Azure Vnet resource group Name" | |
vnet_name: | |
required: true | |
type: string | |
description: "Azure Vnet Name" | |
vnet_address_space: | |
required: true | |
type: string | |
description: "Azure Vnet Address Space" | |
subnet_cidrs: | |
required: true | |
type: string | |
description: "Azure Subnet Address Space. if more than one subnet needed, seperate the CIDR range by comma" | |
action: | |
description: 'Choose action: apply or destroy' | |
required: true | |
default: 'apply' | |
type: choice | |
options: | |
- apply | |
- destroy | |
run-name: ${{ github.actor }} creating vnet with name ${{ inputs.vnet_name }} in ${{ inputs.location }} | |
jobs: | |
create-vnet: | |
runs-on: ubuntu-latest | |
env: | |
ARM_CLIENT_ID: ${{ secrets.AZURE_CLIENT_ID }} | |
ARM_CLIENT_SECRET: ${{ secrets.AZURE_CLIENT_SECRET }} | |
ARM_TENANT_ID: ${{ secrets.AZURE_TENANT_ID }} | |
ARM_SUBSCRIPTION_ID: ${{ secrets.AZURE_SUBSCRIPTION_ID }} | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v4 | |
- name: Construct terraform configuration files | |
uses: actions/setup-python@v5 | |
with: | |
python-version: '3.11' | |
- name: Install pipenv | |
run: | | |
pip install pipenv | |
pipenv install --skip-lock | |
- name: Create terraform configuration files from template | |
run: | | |
pipenv run python3 render_and_create_config_files.py --application_name "${{ inputs.application_name }}" \ | |
--environment "${{ inputs.environment }}" --temporary "${{ inputs.temporary }}" \ | |
--resource_group_name "${{ inputs.resource_group_name }}" \ | |
--location "${{ inputs.location }}" \ | |
--vnet_name "${{ inputs.vnet_name }}" \ | |
--vnet_address_space "${{ inputs.vnet_address_space }}" \ | |
--subnet_cidrs "${{ inputs.subnet_cidrs }}" | |
- name: Token generator | |
uses: githubofkrishnadhas/github-access-using-githubapp@v2 | |
id: app-token | |
with: | |
github_app_id: ${{ secrets.TOKEN_GENERATOR_APPID }} | |
github_app_private_key: ${{ secrets.TOKEN_GENERATOR_PRIVATE_KEY }} | |
owner: 'devwithkrishna' | |
repositories: 'azure-virtual-network-terraform-module' | |
- name: Set up git authentication for terraform modules | |
run: | | |
git config --local --remove-section http."https://github.com/" | |
git config --global url."https://githubofkrishnadhas:${{ steps.app-token.outputs.token }}@github.com/devwithkrishna".insteadOf "https://github.com/devwithkrishna" | |
- name: Terraform setup | |
uses: hashicorp/setup-terraform@v3 | |
with: | |
terraform_version: "1.5.7" | |
- name: terraform init | |
run: terraform -chdir=${{ inputs.resource_group_name }}/${{ inputs.vnet_name }} init | |
- name: Terraform Plan | |
run: terraform -chdir=${{ inputs.resource_group_name }}/${{ inputs.vnet_name }} plan -out=planfile.out ${{ github.event.inputs.action == 'destroy' && '-destroy' || '' }} | |
- name: Terraform Apply | |
id: execute | |
if: ${{ github.event.inputs.action == 'apply' }} | |
run: terraform -chdir=${{ inputs.resource_group_name }}/${{ inputs.vnet_name }} apply -auto-approve planfile.out | |
- name: Terraform Destroy | |
if: ${{ github.event.inputs.action == 'destroy' }} | |
run: terraform -chdir=${{ inputs.resource_group_name }}/${{ inputs.vnet_name }} apply -auto-approve planfile.out | |
- name: List files | |
run: ls -lat | |
- name: Check Execute Status | |
id: check_execute_status | |
run: | | |
if [[ "${{ steps.execute.outcome }}" == "success" ]]; then | |
echo "execute success" | |
echo "::set-output name=success::true" | |
else | |
echo "execute failed" | |
echo "::set-output name=success::false" | |
fi | |
- name: Token generator | |
uses: githubofkrishnadhas/github-access-using-githubapp@v2 | |
id: token | |
with: | |
github_app_id: ${{ secrets.TOKEN_GENERATOR_APPID }} | |
github_app_private_key: ${{ secrets.TOKEN_GENERATOR_PRIVATE_KEY }} | |
owner: 'devwithkrishna' | |
repositories: 'azure-virtual-network-creation-repo' | |
- name: Commit and push the configuration file | |
if: ${{ github.event.inputs.action == 'apply' }} | |
env: | |
GITHUB_TOKEN: ${{ steps.token.outputs.token }} | |
run: | | |
cd ${{ inputs.resource_group_name }}/${{ inputs.vnet_name }} | |
git config user.name 'github-actions' | |
git config user.email 'actions@github.com' | |
git add . | |
git commit -m "Update configuration file for ${{ inputs.resource_group_name }}/${{ inputs.vnet_name }}" | |
git remote set-url origin https://x-access-token:${GITHUB_TOKEN}@github.com/devwithkrishna/azure-virtual-network-creation-repo.git | |
git push | |
verify-vnet: | |
runs-on: ubuntu-latest | |
if: ${{ github.event.inputs.action == 'apply' }} | |
needs: create-vnet | |
steps: | |
- name: Proceed with Job 2 | |
run: echo "create-vnet 'execute' step was successful, proceeding with verify-vnet." | |
- uses: azure/login@v2 | |
with: | |
creds: '{"clientId":"${{ secrets.AZURE_CLIENT_ID }}","clientSecret":"${{ secrets.AZURE_CLIENT_SECRET }}","subscriptionId":"${{ secrets.AZURE_SUBSCRIPTION_ID }}","tenantId":"${{ secrets.AZURE_TENANT_ID }}"}' | |
- name: Install jq | |
run: | | |
sudo apt update | |
sudo apt-get install jq -y | |
- name: install azure cli | |
run: | | |
sudo apt-get update | |
sudo apt-get install apt-transport-https ca-certificates curl gnupg lsb-release -y | |
sudo mkdir -p /etc/apt/keyrings | |
curl -sLS https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor | sudo tee /etc/apt/keyrings/microsoft.gpg > /dev/null | |
sudo chmod go+r /etc/apt/keyrings/microsoft.gpg | |
AZ_DIST=$(lsb_release -cs) | |
echo "Types: deb | |
URIs: https://packages.microsoft.com/repos/azure-cli/ | |
Suites: ${AZ_DIST} | |
Components: main | |
Architectures: $(dpkg --print-architecture) | |
Signed-by: /etc/apt/keyrings/microsoft.gpg" | sudo tee /etc/apt/sources.list.d/azure-cli.sources | |
sudo apt-get update | |
sudo apt-get install azure-cli -y | |
az --version | |
- name: Azure CLI script | |
run: | | |
az account show | |
# Check if VNet exists | |
echo "Checking if VNet exists..." | |
output=$(az network vnet show -g "${{ inputs.resource_group_name }}" -n "${{ inputs.vnet_name }}" 2>/dev/null) | |
if echo "$output" | jq empty > /dev/null 2>&1; then | |
echo "VNet exists. Command succeeded." | |
else | |
echo "VNet does not exist or the command failed." | |
exit 1 | |
fi | |
- name: Execute only if VNet exists | |
if: ${{ success() }} | |
run: | | |
echo "The VNet exists. Proceeding with the next step." | |
echo "Preparing Helpdesk email...." | |
- uses: actions/setup-python@v5 | |
with: | |
python-version: '3.12' | |
- name: Install pipenv | |
run: | | |
pip install pipenv | |
pipenv install --skip-lock | |
- name: Get Actor Email | |
run: | | |
curl -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ | |
-H "Accept: application/vnd.github+json" \ | |
https://api.github.com/users/${{ github.actor }} > user.json | |
USER_EMAIL=$(jq -r '.email // ""' user.json) | |
if [ -z "$USER_EMAIL" ]; then | |
echo "The user email is not publicly available." | |
USER_EMAIL="no-reply@example.com" # Fallback email if not available | |
fi | |
echo "USER_EMAIL=$USER_EMAIL" >> $GITHUB_ENV | |
- name: prepare email for Helpdesk team | |
run: | | |
pipenv run python3 send_email.py --application_name "${{ inputs.application_name }}" \ | |
--environment "${{ inputs.environment }}" \ | |
--region "${{ inputs.location }}" \ | |
--vnet_name "${{ inputs.vnet_name }}" \ | |
--address_space "${{ inputs.vnet_address_space }}" \ | |
--subscription_id "${{ secrets.AZURE_SUBSCRIPTION_ID }}" |