Skip to content

Create Environment

Create Environment #1

name: Create environment
on:
issues:
types: [ opened ]
workflow_dispatch:
permissions:
issues: write
contents: write
actions: write
jobs:
opened:
name: Create environment
runs-on: ubuntu-latest
container:
image: ghcr.io/xpiritbv/azure-sap-automation:github-workflow
if: contains(github.event.issue.labels.*.name, 'create-environment')
steps:
- name: Get app token
id: get_workflow_token
uses: peter-murray/workflow-application-token-action@v3
with:
application_id: ${{ secrets.APPLICATION_ID }}
application_private_key: ${{ secrets.APPLICATION_PRIVATE_KEY }}
#organization: ${{ github.repository_owner }}
- name: Checkout
uses: actions/checkout@v4
with:
token: ${{ steps.get_workflow_token.outputs.token }}
- 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'
env:
GH_TOKEN: ${{ steps.get_workflow_token.outputs.token }}
run: |
#!/usr/bin/env bash
set -euo pipefail
json_input='${{ steps.parse.outputs.payload }}'
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 | tr -d '"')
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: $region
deployer_name=${environment}-${region_map}-${deployer_vnet}-INFRASTRUCTURE
library_name=${environment}-${region_map}-SAP_LIBRARY
url_to_call=/repos/${{ github.repository }}/environments/${deployer_name^^}
_=$(gh api \
-X PUT \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
${url_to_call})
mkdir -p ${GITHUB_WORKSPACE}/WORKSPACES/DEPLOYER/${deployer_name^^}
mkdir -p ${GITHUB_WORKSPACE}/WORKSPACES/LIBRARY/${library_name^^}
cat .cfg_template/deployer.tfvars \
| sed "s|@@ENV@@|${environment}|g" \
| sed "s|@@REGION@@|${region}|g" \
| sed "s|@@VNET@@|${deployer_vnet}|g" \
| sed "s|@@REGION_DISPLAY_NAME@@|${region}|g" \
> ${GITHUB_WORKSPACE}/WORKSPACES/DEPLOYER/${deployer_name^^}/${deployer_name^^}.tfvars
cat .cfg_template/library.tfvars \
| sed "s|@@ENV@@|${environment}|g" \
| sed "s|@@REGION@@|${region}|g" \
> ${GITHUB_WORKSPACE}/WORKSPACES/LIBRARY/${library_name^^}/${library_name^^}.tfvars
# Update the environment in the issue-closed workflow
workspace=$(ls ${GITHUB_WORKSPACE}/WORKSPACES|tail -n 1)
yq -i '.jobs.link-azure.environment = "'${deployer_name^^}'"' .github/workflows/issue-closed.yaml
git config --global --add safe.directory ${GITHUB_WORKSPACE}
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
# Now update the deployment workflow with the deployer and library
${GITHUB_WORKSPACE}/.github/workflows/scripts/update-workflow-on-workspaces-changes.sh
- name: 'Close issue'
run: |
new_issue=$(cat .github/workflows/templates/link-azure.tpl | sed "s|@@REPO@@|${{ github.server_url }}/${{ github.repository }}|g" | \
gh issue create -t "Link Azure to GitHub" -F - -l link-azure)
gh issue close ${{ github.event.issue.number }}
gh issue comment ${{ github.event.issue.number }} --body "Environment created. The only thing left is add the information required to connect to Azure.\n\nI created a [new issue for you to fill in the required information](${new_issue})."
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}