diff options
| author | 2020-02-27 20:53:13 -0800 | |
|---|---|---|
| committer | 2020-02-27 20:53:13 -0800 | |
| commit | 184da24f7dd94b90a5251ca8a54ee87c069f940c (patch) | |
| tree | cac8d7d53e3e32e3f20125662ceef4604ab0303c /scripts/check_dockerfiles.sh | |
| parent | Merge pull request #62 from python-discord/bug/ci/61/python-symlink-not-resol... (diff) | |
| parent | CI: fix can_pull causing script to exit with code 1 (diff) | |
Merge pull request #54 from python-discord/ci-improvements
CI Improvements
Diffstat (limited to 'scripts/check_dockerfiles.sh')
| -rwxr-xr-x | scripts/check_dockerfiles.sh | 65 | 
1 files changed, 53 insertions, 12 deletions
| diff --git a/scripts/check_dockerfiles.sh b/scripts/check_dockerfiles.sh index c84c61f..88cb7cc 100755 --- a/scripts/check_dockerfiles.sh +++ b/scripts/check_dockerfiles.sh @@ -1,6 +1,7 @@  #!/usr/bin/env bash  set -euo pipefail +shopt -s inherit_errexit  exec 3>&1 # New file descriptor to stdout  BASE_URL="https://dev.azure.com/\ @@ -12,10 +13,18 @@ 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 +declare -A build_cache +get_build() {      local branch="${1:?"get_build: argument 1 'branch' is unset"}" + +    # Attempt to use cached value +    if [[ -v build_cache["${branch}"] ]]; then +        printf '%s\n' "Retrieving build for ${branch} from cache." >&3 +        printf '%s' "${build_cache[$branch]}" +        return 0 +    fi +      local url="${BASE_URL}&branchName=${branch}"      printf '%s\n' "Retrieving the latest successful build using ${url}" >&3 @@ -29,10 +38,37 @@ get_build() {      then          return 1      else +        # Cache the response +        build_cache["${branch}"]="${response}"          printf '%s' "${response}"      fi  } +can_pull() { +    local image="${1:?"can_pull: argument 1 'image' is unset"}" + +    local master_commit +    if master_commit="$( +            get_build "refs/heads/master" \ +            | jq -re '.value[0].sourceVersion' +        )" \ +        && git diff --quiet "${master_commit}" -- "${@:2}" +    then +        printf \ +            '%s\n' \ +            "Can pull ${image} image from Docker Hub; no changes since master." + +        printf '%s\n' "##vso[task.setvariable variable=${image^^}_PULL]True" +    else +        printf \ +            '%s\n' \ +            "Cannot pull ${image} image from Docker Hub due to detected " \ +            "changes; the ${image} image will be built." + +        return 1 +    fi +} +  # Get the previous commit  if [[ "${BUILD_REASON}" = "PullRequest" ]]; then      if ! prev_commit="$( @@ -65,22 +101,27 @@ 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" +    echo "##vso[task.setvariable variable=BASE_CHANGED]False"  else      # Always rebuild the venv if the base changes. +    echo "Changes detected in docker/base.Dockerfile; all images will be built."      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 "##vso[task.setvariable variable=VENV_CHANGED]False" + +    if ! can_pull venv docker/venv.Dockerfile Pipfile*; then +        # Venv image can't be pulled so it needs to be built. +        # Therefore, the base image is needed too. +        can_pull base docker/base.Dockerfile || true +    fi +else +    echo \ +        "Changes detected in docker/venv.Dockerfile or the Pipfiles;" \ +        "the venv image will be built." +      # Though base image hasn't changed, it's still needed to build the venv. -    echo "Can pull base image from Docker Hub; no changes made since master." -    echo "##vso[task.setvariable variable=BASE_PULL;isOutput=true]True" +    can_pull base docker/base.Dockerfile || true  fi | 
