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

Converted set-cicd-flag to a YAML template with inline script #325

Merged
merged 2 commits into from
Sep 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,10 @@ jobs:

steps:
- ${{ each config in parameters.configurationMatrix }}:
- task: Bash@3
name: ${{ config.jobName }}
displayName: Determine if ${{ config.jobName }} needs CI
inputs:
filePath: './$(PIPELINE_ROOT_DIR)/$(SCRIPTS_DIR)/set-cicd-flag.sh'
workingDirectory: $(Build.SourcesDirectory)
env:
TERRAFORM_TEMPLATE_PATH: '${{ config.terraformTemplatePath }}'
- template: scripts/set-cicd-flag.yml
parameters:
terraformTemplatePath: ${{ config.terraformTemplatePath }}
jobName: ${{ config.jobName }}

- ${{ each config in parameters.configurationMatrix }}:
- job: Build_${{ config.jobName }}_${{ parameters.environment }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,10 @@ jobs:
condition: not(coalesce(variables.FORCE_RUN, ${{ parameters.forceRun }}))
steps:
- ${{ each config in parameters.configurationMatrix }}:
- task: Bash@3
name: ${{ config.jobName }}
displayName: Determine if ${{ config.jobName }} needs CD
inputs:
filePath: './$(PIPELINE_ROOT_DIR)/$(SCRIPTS_DIR)/set-cicd-flag.sh'
workingDirectory: $(Build.SourcesDirectory)
env:
TERRAFORM_TEMPLATE_PATH: '${{ config.terraformTemplatePath }}'
- template: scripts/set-cicd-flag.yml
parameters:
terraformTemplatePath: ${{ config.terraformTemplatePath }}
jobName: ${{ config.jobName }}

- ${{ each config in parameters.configurationMatrix }}:
- deployment: Provision_${{ config.jobName }}_${{ parameters.environment }}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
parameters:
terraformTemplatePath: ''
jobName: ''

steps:
- task: Bash@3
name: ${{ parameters.jobName }}
displayName: Determine if ${{ parameters.jobName }} needs CI/CD
env:
TERRAFORM_TEMPLATE_PATH: ${{ parameters.terraformTemplatePath }}
inputs:
targetType: 'inline'
script: |
#!/usr/bin/env bash

# Note: Omitting `set -euo pipefail` as it makes using grep to filter for changes a nightmare!

declare readonly GIT_DIFF_EXTENSION_WHITE_LIST="*.go|*.tf|*.sh|Dockerfile*|*.tfvars|*.yaml|*.yml"

function setCICDFlag() {
echo "Template $TERRAFORM_TEMPLATE_PATH needs CI/CD"
echo "##vso[task.setvariable variable=needs_cicd;isOutput=true]true"
}

MASTER="remotes/origin/master"
GIT_DIFF_SOURCEBRANCH="HEAD"

# we should always use master as a comparison, except in the case that this is
# a build for master. In this case we can use HEAD~ (1 commit behind master)
# because all merges will be squash merges
if [[ $(git diff "$MASTER") ]]; then
GIT_DIFF_UPSTREAMBRANCH="$MASTER"
else
GIT_DIFF_UPSTREAMBRANCH="$MASTER~"
fi

echo "GIT_DIFF_UPSTREAMBRANCH: $GIT_DIFF_UPSTREAMBRANCH"
echo "GIT_DIFF_SOURCEBRANCH: $GIT_DIFF_SOURCEBRANCH"

FILE_CHANGE_SET=$(git diff "$GIT_DIFF_SOURCEBRANCH" "$GIT_DIFF_UPSTREAMBRANCH" --name-only)
echo "Files changed since last commit..."
echo "$FILE_CHANGE_SET"

FILTERED_FILE_CHANGE_SET=$(grep -E "$GIT_DIFF_EXTENSION_WHITE_LIST" <<< "$FILE_CHANGE_SET" || true)
echo "Files changed since last commit, filtered for build-relevant files..."
echo "$FILTERED_FILE_CHANGE_SET"

TEST_HARNESS_CHANGES=$(grep "$TEST_HARNESS_DIR" <<< "$FILTERED_FILE_CHANGE_SET" || true)
TEMPLATE_CHANGES=$(grep "$TERRAFORM_TEMPLATE_PATH" <<< "$FILTERED_FILE_CHANGE_SET" || true)
PIPELINE_CHANGES=$(grep "$PIPELINE_ROOT_DIR" <<< "$FILTERED_FILE_CHANGE_SET" || true)
MODULE_CHANGES=$(grep "$TF_ROOT_DIR/modules" <<< "$FILTERED_FILE_CHANGE_SET" || true)

# if relevant files have been changed, CICD for this template needs to run
[ ! -z "${TEST_HARNESS_CHANGES}" ] && setCICDFlag
[ ! -z "${TEMPLATE_CHANGES}" ] && setCICDFlag
[ ! -z "${PIPELINE_CHANGES}" ] && setCICDFlag
[ ! -z "${MODULE_CHANGES}" ] && setCICDFlag

exit 0