Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update create-pipelines.bat onboarding script to auto-provision environment #178

Merged
merged 10 commits into from
Feb 23, 2022
40 changes: 33 additions & 7 deletions scripts/onboarding/create-pipelines.bat
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ REM // -------------------------------------------------------------------------
echo.
echo Creating Azure DevOps pipelines in the context of:
echo.
echo DevOps Organization: %DEVOPS_ORG%
echo DevOps Project: %DEVOPS_PROJECT_NAME%
echo Repository Name/URL: %DEVOPS_REPO_NAME_OR_URL%
echo Repository Type: %DEVOPS_REPO_TYPE%
echo Repository Branch: %DEVOPS_REPO_BRANCH%
echo Azure Pipeline Suffix: %DEVOPS_PIPELINE_NAME_SUFFIX%
echo DevOps Organization: %DEVOPS_ORG%
echo DevOps Project: %DEVOPS_PROJECT_NAME%
echo Repository Name/URL: %DEVOPS_REPO_NAME_OR_URL%
echo Repository Type: %DEVOPS_REPO_TYPE%
echo Repository Branch ^& Environment: %DEVOPS_REPO_BRANCH%
echo Azure Pipeline Suffix: %DEVOPS_PIPELINE_NAME_SUFFIX%
echo.
choice /C YN /M "Do you want to proceed?"
if errorlevel 2 exit /b 0
Expand All @@ -26,7 +26,7 @@ for %%N in (management-groups roles platform-logging policy platform-connectivit

REM Check for pipeline existence
set FOUND=
for /f usebackq %%F in (`call az pipelines list -o tsv --query="[?name=='%%N-%DEVOPS_PIPELINE_NAME_SUFFIX%'].name | [0]"`) do set FOUND=true
for /f usebackq %%F in (`call az pipelines list -o tsv --query="[?name=='%%N%DEVOPS_PIPELINE_NAME_SUFFIX%'].name | [0]"`) do set FOUND=true

REM Only create Azure DevOps pipeline if it does *not* already exist
if not defined FOUND (
Expand All @@ -36,3 +36,29 @@ for %%N in (management-groups roles platform-logging policy platform-connectivit
echo Pipeline [%%N%DEVOPS_PIPELINE_NAME_SUFFIX%] already exists. Skipping creation.
)
)

REM Get environments in the project
echo.
echo Retrieving list of environments for project [%DEVOPS_PROJECT_NAME%]..
call az devops invoke --organization "%DEVOPS_ORG%" --route-parameters project="%DEVOPS_PROJECT_NAME%" --http-method GET --api-version 6.0 --area distributedtask --resource environments -o json >%DEVOPS_OUTPUT_DIR%\environment.json

REM Check if environment matching repository branch name exists
set ENVIRONMENT=
echo Checking project for existing environment [%DEVOPS_REPO_BRANCH%]...
for /f "usebackq delims=" %%E in (`jq ".value[] | select(.name == \"%DEVOPS_REPO_BRANCH%\") | .name" %DEVOPS_OUTPUT_DIR%\environment.json`) do set ENVIRONMENT=%%~E

REM Create environment if it doesn't already exist
if not defined ENVIRONMENT (
echo Creating environment [%DEVOPS_REPO_BRANCH%]...
echo { "name": "%DEVOPS_REPO_BRANCH%" } >%DEVOPS_OUTPUT_DIR%\environment-body.json
call az devops invoke --organization "%DEVOPS_ORG%" --route-parameters project="%DEVOPS_PROJECT_NAME%" --http-method POST --api-version 6.0 --area distributedtask --resource environments --in-file %DEVOPS_OUTPUT_DIR%\environment-body.json >nul
) else (
echo Environment [%DEVOPS_REPO_BRANCH%] already exists. Skipping creation.
)

echo.
echo Now that an environment exists for the repository branch [%DEVOPS_REPO_BRANCH%],
echo learn more about configuring approvals and checks for deployments associated with this
echo environment by reviewing the following documentation:
echo * https://docs.microsoft.com/azure/devops/pipelines/process/approvals
echo.