From 0945489837031c45abc6fc42ad142f54068dca91 Mon Sep 17 00:00:00 2001 From: Yvonne Radsmikham Date: Mon, 16 Mar 2020 14:31:26 -0700 Subject: [PATCH] improved generation pipeline yaml and updated infra guide --- .../templates/infra-generation-pipeline.yml | 27 ++++++++++++++++--- guides/infra/spk-infra-generation-pipeline.md | 11 +++++--- 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/azure-pipelines/templates/infra-generation-pipeline.yml b/azure-pipelines/templates/infra-generation-pipeline.yml index 1a8750217..9a15830dc 100644 --- a/azure-pipelines/templates/infra-generation-pipeline.yml +++ b/azure-pipelines/templates/infra-generation-pipeline.yml @@ -75,21 +75,40 @@ steps: set -e # Retrieve most recent commit hash from HLD repo - commit_hash=$(git rev-parse HEAD) + commit_hash=$(git rev-parse HEAD | cut -c1-7) echo "Commit Hash: $commit_hash" # Clone Generated Repo and copy generated components over echo "Cloning Generated Repo: $GENERATED_REPO" git clone $GENERATED_REPO + repo_url=$GENERATED_REPO # Extract repo name from url - repo_url=$GENERATED_REPO repo=${repo_url##*/} repo_name=${repo%.*} cd "$repo_name" + + git pull + rsync -rv --exclude=.terraform $HOME/$PROJECT_DIRECTORY-generated . + # Check if PR branch already exists: + pr_list=$(git ls-remote origin) + if [[ $pr_list == *"pr-$commit_hash"* ]]; then + echo "PR Branch already exist. Iterating..." + count=$(git ls-remote origin | grep "pr-$commit_hash" | wc -l | tr -d " ") + count=$((count + 1)) + PR_BRANCH_NAME=pr-$commit_hash-$count + echo "PR BRANCH NAME: $PR_BRANCH_NAME" + git checkout -b $PR_BRANCH_NAME + else + echo "PR Branch does not exist. Creating a new PR Branch" + PR_BRANCH_NAME=pr-$commit_hash-1 + echo "PR BRANCH NAME: $PR_BRANCH_NAME" + git checkout -b $PR_BRANCH_NAME + fi + # Set git identity git config user.email "admin@azuredevops.com" git config user.name "Automated Account" @@ -101,7 +120,7 @@ steps: # Format Terraform files terraform fmt - + # Add generated files to repository git status git add . @@ -142,4 +161,4 @@ steps: fi env: ACCESS_TOKEN_SECRET: $(ACCESS_TOKEN_SECRET) - displayName: 'Commit and Push to Generated Repository' \ No newline at end of file + displayName: 'Commit and Push to Generated Repository' diff --git a/guides/infra/spk-infra-generation-pipeline.md b/guides/infra/spk-infra-generation-pipeline.md index bd151fdf3..3d62563d7 100644 --- a/guides/infra/spk-infra-generation-pipeline.md +++ b/guides/infra/spk-infra-generation-pipeline.md @@ -6,7 +6,8 @@ orchestrator for your Infrastructure Generation workflow using `spk infra`. ## Prerequisites 1. _Permissions_: The ability to create Projects in your Azure DevOps - Organization. + Organization and set security permissions to "Contribute" and "Contribute to + Pull Requests" for git repositories. 2. _High Level Definitions_: Your own infrastructure high level definitions for your deployment. You can refer to a sample repo [here](https://github.com/yradsmikham/spk-infra-hld). You should be able to @@ -28,6 +29,10 @@ two flavors: - [Azure DevOps](https://github.com/microsoft/bedrock/blob/master/gitops/azure-devops/ADORepos.md) - [GitHub](https://github.com/microsoft/bedrock/blob/master/gitops/azure-devops/GitHubRepos.md) +**Note**: If your git repos are in different Azure DevOps organizations and/or +pojects, be sure that the generated git repo has "Contribute" and "Contribute to +Pull Requests" permissions configured in the security settings. + ### 2. Add Azure Pipeline Build YAML The SPK repository has a @@ -65,8 +70,8 @@ If using Azure DevOps repos, be sure to include the additional environment variables: ``` -AZDO_ORG_NAME: Azure DevOps organization url (i.e. https://dev.azure.com/org_name/) -AZDO_PROJECT_NAME: The name of the project in your Azure DevOps organization where the repository is hosted +AZDO_ORG_NAME: Azure DevOps organization url where the generated repo is hosted (i.e. https://dev.azure.com/org_name/) +AZDO_PROJECT_NAME: The name of the project in your Azure DevOps organization where the generated repo is hosted ``` You can use `spk` to create the Azure DevOps Variable Groups by executing