Refactor script for building, testing, publishing Docker containers (#4354)

* Rename build-and-publish-docker to build-docker.sh

* Refactor build-docker.sh to separate test and publish steps

* Update scripts/build-docker.sh

Co-Authored-By: Paul Stack <public@paulstack.co.uk>

* Sanitize CLI version if needed

Co-authored-by: Paul Stack <public@paulstack.co.uk>
This commit is contained in:
Chris Smith 2020-04-21 10:39:26 -07:00 committed by GitHub
parent 807cf44cd7
commit e96662a48e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 114 additions and 68 deletions

View file

@ -1,67 +0,0 @@
#!/bin/bash
# Usage build-and-publish-docker [cli-version]
set -o nounset
set -o errexit
set -o pipefail
readonly SCRIPT_DIR="$( cd "$( dirname "${0}" )" && pwd )"
readonly ROOT=${SCRIPT_DIR}/..
if [ -z "${1:-}" ]; then
>&2 echo "error: missing version to publish"
exit 1
fi
if [ -z "${DOCKER_HUB_USER:-}" ]; then
>&2 echo "error: 'DOCKER_HUB_USER' should be defined"
exit 1
fi
if [ -z "${DOCKER_HUB_PASSWORD:-}" ]; then
>&2 echo "error: 'DOCKER_HUB_PASSWORD' should be defined"
exit 1
fi
CLI_VERSION="${1}"
# We only want to push docker images for stable versions of Pulumi. So if there is a -alpha
# pre-release tag, skip publishing.
if [[ "${CLI_VERSION}" == *-alpha* ]]; then
>&2 echo "Skipping docker publishing for ${CLI_VERSION} since it is a pre-release"
exit 0
fi
docker login -u "${DOCKER_HUB_USER}" -p "${DOCKER_HUB_PASSWORD}"
echo "Building containers..."
for container in pulumi actions; do
echo "- pulumi/${container}"
docker build --build-arg PULUMI_VERSION="${CLI_VERSION}" \
-t "pulumi/${container}:${CLI_VERSION}" \
-t "pulumi/${container}:latest" \
"${SCRIPT_DIR}/../dist/${container}"
done
echo "Running container runtime tests..."
pushd ${ROOT}/tests
GOOS=linux go test -c -o /tmp/pulumi-test-containers ${ROOT}/tests/containers/...
popd
docker run -e RUN_CONTAINER_TESTS=true \
-e PULUMI_ACCESS_TOKEN=${PULUMI_ACCESS_TOKEN} \
--volume /tmp:/src \
--entrypoint /bin/bash \
pulumi/pulumi:latest \
-c "pip install pipenv && /src/pulumi-test-containers -test.parallel=1 -test.v -test.run TestPulumiDockerImage"
# Disabled due to https://github.com/pulumi/pulumi/issues/4136
# echo "Running container entrypoint tests..."
# RUN_CONTAINER_TESTS=true go test ${ROOT}/tests/containers/... -test.run TestPulumiActionsImage -test.v
echo "Publishing containers..."
for container in pulumi actions; do
echo "- pulumi/${container}"
docker push "pulumi/${container}:${CLI_VERSION}"
docker push "pulumi/${container}:latest"
done
docker logout

111
scripts/build-docker.sh Executable file
View file

@ -0,0 +1,111 @@
# Clean up the CLI version for dev builds, since they aren't valid tag names.
# #!/bin/bash
#
# Builds the Pulumi docker containers locally. Optionally running tests or
# publishing to a container registry.
#
# Usage: build-docker cli-version [--test] [--publish]
set -o nounset
set -o errexit
set -o pipefail
readonly SCRIPT_DIR="$( cd "$( dirname "${0}" )" && pwd )"
readonly ROOT=${SCRIPT_DIR}/..
if [ -z "${1:-}" ]; then
>&2 echo "error: missing version to publish"
exit 1
fi
# Sanitize the name of the version, e.g.
# "v1.14.0-alpha.1586190504+gf4e9f7e2" -> "v1.14.0-alpha.1586190504".
readonly CLI_VERSION="$(echo "${1}" | sed 's/\+.*//g')"
# The Docker containers built/tested/published from this repository.
readonly PULUMI_CONTAINERS=("pulumi" "actions")
echo_header() {
echo -e "\n\033[0;35m${1}\033[0m"
}
test_containers() {
# Run tests _within_ the "pulumi" container, ensuring that the CLI is installed
# and working correctly.
echo_header "Executing container runtime tests"
docker run -e RUN_CONTAINER_TESTS=true \
-e PULUMI_ACCESS_TOKEN=${PULUMI_ACCESS_TOKEN} \
--volume /tmp:/src \
--entrypoint /bin/bash \
pulumi/pulumi:latest \
-c "pip install pipenv && /src/pulumi-test-containers -test.parallel=1 -test.v -test.run TestPulumiDockerImage"
# The actions container should fetch program dependencies from NPM, PIP, etc. before
# executing. These tests just shell out to docker run to confirm that.
# Disabled due to https://github.com/pulumi/pulumi/issues/4136
# echo_header "Executing container entrypoint tests"
# RUN_CONTAINER_TESTS=true go test ${ROOT}/tests/containers/... -test.run TestPulumiActionsImage -test.v
# In case there are any other unit tests defined in the module, run those as well.
pushd ${ROOT}/tests
GOOS=linux go test -c -o /tmp/pulumi-test-containers ${ROOT}/tests/containers/...
popd
}
# Publishes the built containers to Docker Hub.
publish_containers() {
echo_header "Publishing containers"
# Required environment variables.
if [ -z "${DOCKER_HUB_USER:-}" ]; then
>&2 echo "error: 'DOCKER_HUB_USER' should be defined"
exit 1
fi
if [ -z "${DOCKER_HUB_PASSWORD:-}" ]; then
>&2 echo "error: 'DOCKER_HUB_PASSWORD' should be defined"
exit 1
fi
# We only want to push docker images for stable versions of Pulumi. So if there is a -alpha
# pre-release tag, skip publishing.
if [[ "${CLI_VERSION}" == *-alpha* ]]; then
>&2 echo "Skipping docker publishing for ${CLI_VERSION} since it is a pre-release"
exit 0
fi
docker login -u "${DOCKER_HUB_USER}" -p "${DOCKER_HUB_PASSWORD}"
for container in ${PULUMI_CONTAINERS[@]}; do
echo "- pulumi/${container}"
docker push "pulumi/${container}:${CLI_VERSION}"
docker push "pulumi/${container}:latest"
done
docker logout
}
echo_header "Building Pulumi containers (${CLI_VERSION})"
for container in ${PULUMI_CONTAINERS[@]}; do
echo "- Building pulumi/${container}"
docker build --build-arg PULUMI_VERSION="${CLI_VERSION}" \
-t "pulumi/${container}:${CLI_VERSION}" \
-t "pulumi/${container}:latest" \
"${SCRIPT_DIR}/../dist/${container}"
done
# Loop through the remaining args, running them in order.
for script_arg in "${@:2}"; do
case ${script_arg} in
"--test")
test_containers
;;
"--publish")
echo "Publishing..."
publish_containers
;;
*)
echo "Error: Unrecognized argument '${script_arg}'"
break
;;
esac
done

View file

@ -64,8 +64,10 @@ if [[ "${TRAVIS_PUBLISH_PACKAGES:-}" == "true" ]]; then
find /opt/pulumi/nuget -name 'Pulumi*.nupkg' \
-exec dotnet nuget push -k ${NUGET_PUBLISH_KEY} -s https://api.nuget.org/v3/index.json {} ';'
"${ROOT}/scripts/build-and-publish-docker" "${NPM_VERSION}"
echo "Publishing Docker containers to hub.docker.com:"
"${ROOT}/scripts/build-docker.sh" "${NPM_VERSION}" --publish --test
echo "Building package docs:"
"$(go env GOPATH)/src/github.com/pulumi/scripts/ci/build-package-docs.sh" pulumi
fi