Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudcosmonaut committed Mar 15, 2024
1 parent 9b28089 commit 3bb4378
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 31 deletions.
1 change: 0 additions & 1 deletion .github/workflows/create-environment.tpl

This file was deleted.

84 changes: 54 additions & 30 deletions .github/workflows/create-environment.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
name: Create environment

on:
issues:
types: [ opened, closed ]
workflow_dispatch:

permissions:
issues: write
pull-requests: write
contents: read
contents: write
actions: write

jobs:
Expand Down Expand Up @@ -40,7 +42,10 @@ jobs:
exit 1
}
if [ -z "${{ secrets.AZURE_CLIENT_ID }}" || -z "${{ secrets.AZURE_CLIENT_SECRET }}" || -z "${{ secrets.AZURE_TENANT_ID }}" || -z "${{ secrets.AZURE_SUBSCRIPTION_ID }}" ]; then
if [[ -z "${{ secrets.AZURE_CLIENT_ID }}" ]] \
|| [[ -z "${{ secrets.AZURE_CLIENT_SECRET }}" ]] \
|| [[ -z "${{ secrets.AZURE_TENANT_ID }}" ]] \
|| [[ -z "${{ secrets.AZURE_SUBSCRIPTION_ID }}" ]]; then
missing_secret
fi
Expand All @@ -60,48 +65,65 @@ jobs:
missing_secret
fi
- name: Run Issue form parser
id: parse
uses: peter-murray/issue-forms-body-parser@v4
with:
issue_id: ${{ github.event.issue.number }}
separator: '###'
label_marker_start: '' # U+200B - Zero Width Space; to make sure the UI stays clean
label_marker_end: '' # U+200B
# - name: Run Issue form parser
# id: parse
# uses: peter-murray/issue-forms-body-parser@v4
# with:
# issue_id: ${{ github.event.issue.number }}
# separator: '###'
# label_marker_start: '​' # U+200B - Zero Width Space; to make sure the UI stays clean
# label_marker_end: '​' # U+200B

- name: 'Create GitHub Environment'
run: |
#!/usr/bin/env bash
set -euo pipefail
environment=$(echo "${{ steps.parse.outputs.payload }}" | jq -r '."Environment"')
region=$(echo "${{ steps.parse.outputs.payload }}" | jq -r '."Region"')
deployer_vnet=$(echo "${{ steps.parse.outputs.payload }}" | jq -r '."Deployer Vnet"')
# json_input=$(echo "${{ steps.parse.outputs.payload }}"')
json_input='{"Environment":"ACC","Region":"westeurope","Deployer Vnet":"DEP01"}'
region_map=$(pushd /source/deploy/terraform/terraform-units/modules/sap_namegenerator; echo var.region_mapping.${region} | terraform console; popd)
environment=$(echo ${json_input} | jq -r '."Environment"')
region=$(echo ${json_input} | jq -r '."Region"')
deployer_vnet=$(echo ${json_input} | jq -r '."Deployer Vnet"')
pushd /source/deploy/terraform/terraform-units/modules/sap_namegenerator
region_map=$(echo var.region_mapping.${region} | terraform console)
popd
region_display_name=$(az account list-locations -o json| jq --arg REGION $region '.[] | select(.name==$REGION) | .displayName' -r)
echo region_map: $region_map
echo region_display_name: $region_display_name
exit 1
deployer_name=${environment}-${region_map}-${deployer_vnet}-INFRASTRUCTURE
library_name=${environment}-${region_map}-SAP_LIBRARY
gh auth login --with-token ${{ steps.get_workflow_token.outputs.token }}
echo ${{ secrets.GITHUB_TOKEN }} | gh auth login --with-token
# make sure the environment does not exist
_=$(gh api \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
/repos/xpiritbv/azure-sap-automation-deployer/environments/${environment})
url_to_call=${{ github.api_url }}/repos/${{ github.repository }}/environments
echo "Checking if environment ${environment} already exists"
echo "URL: ${url_to_call}"
if [ $? -eq 0 ]; then
echo "Environment already exists"
exit 1
fi
# # make sure the environment does not exist
# set +e
# _=$(gh api \
# -H "Accept: application/vnd.github+json" \
# -H "X-GitHub-Api-Version: 2022-11-28" \
# ${url_to_call} | jq --arg ENVIRONMENT ${environment} '.environments[] | select(.name == $ENVIRONMENT)')
# set -e
# if [ $? -eq 0 ]; then
# echo "Environment ${environment} already exists"
# exit 1
# fi
gh api -X PUT \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
/repos/xpiritbv/azure-sap-automation-deployer/environments/${environment}
# curl -Ssf -X PUT \
# -H "Authorization: Bearer ${{ steps.get_workflow_token.outputs.token }}" \
# -H "Accept: application/vnd.github+json" \
# -H "X-GitHub-Api-Version: 2022-11-28" \
# ${{ github.api_url }}/repos/${{ github.repository }}/environments/${environment}
mkdir -p ${{ github.workspace }}/WORKSPACES/DEPLOYER/${deployer_name^^}
mkdir -p ${{ github.workspace }}/WORKSPACES/LIBRARY/${library_name^^}
Expand All @@ -110,7 +132,6 @@ jobs:
| sed "s|@@ENV@@|${environment}|g" \
| sed "s|@@REGION@@|${region}|g" \
| sed "s|@@VNET@@|${deployer_vnet}|g" \
| sed "s|@@USE_WEBAPP@@|${use_webapp}|g" \
| sed "s|@@REGION_DISPLAY_NAME@@|${region_display_name}|g" \
> ${{ github.workspace }}/WORKSPACES/DEPLOYER/${deployer_name^^}/${deployer_name^^}.tfvars
Expand All @@ -120,4 +141,7 @@ jobs:
> ${{ github.workspace }}/WORKSPACES/LIBRARY/${library_name^^}/${library_name^^}.tfvars
git add ${{ github.workspace }}/WORKSPACES
git config --global user.name "GitHub Actions"
git config --global user.email "[email protected]"
git commit -m "Add configuration for ${environment} in ${region}"
git push

0 comments on commit 3bb4378

Please sign in to comment.