diff options
| author | 2019-06-26 17:41:50 -0700 | |
|---|---|---|
| committer | 2019-06-29 20:05:39 -0700 | |
| commit | 4db74c604befd2dc41d1b660bdea08187c8877b0 (patch) | |
| tree | e6cfbd90769928142ac8ec63f6bc8b764b542ecd | |
| parent | Check Bash version (diff) | |
Write script to check dockerfiles
| -rw-r--r-- | azure-pipelines.yml | 45 | ||||
| -rwxr-xr-x | scripts/check_dockerfiles.sh | 84 | ||||
| -rwxr-xr-x | scripts/test.sh | 3 | 
3 files changed, 88 insertions, 44 deletions
diff --git a/azure-pipelines.yml b/azure-pipelines.yml index a05fc3b..f9a187a 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -8,47 +8,10 @@ jobs:      vmImage: 'Ubuntu-16.04'    steps: -    - script: | -        set -euo pipefail - -        REQUEST_URL="https://dev.azure.com/\ -        python-discord/${SYSTEM_TEAMPROJECTID}/_apis/build/builds?\ -        queryOrder=finishTimeDescending&\ -        resultFilter=succeeded&\ -        \$top=1&\ -        repositoryType=${BUILD_REPOSITORY_PROVIDER}&\ -        repositoryId=${BUILD_REPOSITORY_NAME}&\ -        branchName=${BUILD_SOURCEBRANCH}&\ -        api-version=5.0" - -        printf '%s\n' "Retrieving latest successful build using ${REQUEST_URL}" -        response="$(curl -sSL "${REQUEST_URL}")" - -        if [[ "$(printf '%s' "${response}" | jq -re '.count')" = 0 ]]; then -            (>&2 echo \ -                "No previous build was found." \ -                "Either the previous build is too old and was deleted" \ -                "or the branch was empty before this build." -            ) -            exit 0 -        fi - -        prev_build="$(printf '%s' "${response}" | jq -re '.value[0].id')" -        printf '%s\n' "Most recent successful build: ${prev_build}" -        printf '%s\n' "##vso[task.setvariable variable=PREV_BUILD]${prev_build}" - -        if [[ "${BUILD_REASON}" = "PullRequest" ]]; then -            key='triggerInfo."pr.sourceSha"' -        else -            key='sourceVersion' -        fi - -        prev_commit="$(printf '%s' "${response}" | jq -re ".value[0].${key}")" -        printf '%s\n' "Previous commit: ${prev_commit}" -        printf '%s\n' "##vso[task.setvariable variable=PREV_COMMIT]${prev_commit}" -      displayName: 'Get Previous Successful Build' - -    - script: echo "${BASH_VERSION}" +    - task: ShellScript@2 +      inputs: +        scriptPath: scripts/check_dockerfiles.sh +      displayName: 'Check If Images Need to Be Built'      - task: DownloadPipelineArtifact@2        inputs: diff --git a/scripts/check_dockerfiles.sh b/scripts/check_dockerfiles.sh new file mode 100755 index 0000000..ddc7084 --- /dev/null +++ b/scripts/check_dockerfiles.sh @@ -0,0 +1,84 @@ +#!/usr/bin/env bash + +set -euo pipefail +exec 3>&1 # New file descriptor to stdout + +BASE_URL="https://dev.azure.com/\ +python-discord/${SYSTEM_TEAMPROJECTID}/_apis/build/builds?\ +queryOrder=finishTimeDescending&\ +resultFilter=succeeded&\ +\$top=1&\ +repositoryType=${BUILD_REPOSITORY_PROVIDER}&\ +repositoryId=${BUILD_REPOSITORY_NAME}&\ +api-version=5.0" + +get_build() { +    set -e # Poor Ubuntu LTS doesn't have Bash 4.4's inherit_errexit + +    local branch="${1:?"get_build: argument 1 'branch' is unset"}" +    local url="${BASE_URL}&branchName=${branch}" + +    printf '%s\n' "Retrieving the latest successful build using ${url}" >&3 + +    local response +    response="$(curl -sSL "${url}")" + +    if [[ -z "${response}" ]] \ +        || ! printf '%s' "${response}" | jq -re '.count' +    then +        return 1 +    else +        printf '%s' "${response}" +    fi +} + +# Get the previous commit +if [[ "${BUILD_REASON}" = "PullRequest" ]]; then +    if ! prev_commit="$( +            get_build "${BUILD_SOURCEBRANCH}" \ +            | jq -re '.value[0].triggerInfo."pr.sourceSha"' +        )" +    then +        echo \ +            "Could not retrieve the previous build's commit." \ +            "Falling back to the head of the target branch." + +        prev_commit="origin/${SYSTEM_PULLREQUEST_TARGETBRANCH}" +    fi +elif ! prev_commit="$( +        get_build "${BUILD_SOURCEBRANCH}" \ +        | jq -re '.value[0].sourceVersion' +    )" +then +    echo \ +        "No previous build was found." \ +        "Either the previous build is too old and was deleted" \ +        "or the branch was empty before this build." \ +        "All images will be built." +    exit 0 +fi + +# Compare diffs +head="$(git rev-parse HEAD)" +printf '%s\n' "Comparing HEAD (${head}) against ${prev_commit}." + +if git diff --quiet "${prev_commit}" -- docker/base.Dockerfile; then +    echo "No changes detected in docker/base.Dockerfile." +    echo "##vso[task.setvariable variable=BASE_CHANGED;isOutput=true]false" +else +    # Always rebuild the venv if the base changes. +    exit 0 +fi + +if git diff --quiet "${prev_commit}" -- docker/venv.Dockerfile Pipfile*; then +    echo "No changes detected in docker/venv.Dockerfile or the Pipfiles." +    echo "##vso[task.setvariable variable=VENV_CHANGED;isOutput=true]false" +elif master_commit="$( +        get_build "refs/heads/master" \ +        | jq -re '.value[0].sourceVersion' +    )" \ +    && git diff --quiet "${master_commit}" -- docker/base.Dockerfile +then +    echo "Can pull base image from Docker Hub; no changes made since master." +    echo "##vso[task.setvariable variable=BASE_PULL;isOutput=true]true" +fi diff --git a/scripts/test.sh b/scripts/test.sh deleted file mode 100755 index a267324..0000000 --- a/scripts/test.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/usr/bin/env bash - -printf '%s\n' "$1"  |