2019-05-02 17:28:20 +02:00
|
|
|
#!/bin/bash
|
2019-07-06 16:17:03 +02:00
|
|
|
set -eoux pipefail
|
2019-05-02 17:28:20 +02:00
|
|
|
|
2020-05-18 03:56:55 +02:00
|
|
|
if [[ -z ${1:-} && -n ${CI:-} ]]; then
|
2019-10-18 11:15:36 +02:00
|
|
|
echo 'Usage: ./build.sh VERSION_SHORT'
|
2019-05-18 13:26:04 +02:00
|
|
|
exit 1
|
2020-05-18 03:56:55 +02:00
|
|
|
elif [[ ${CI:-} == true || -n ${1:-} ]]; then
|
2019-05-16 19:23:07 +02:00
|
|
|
VERSION_SHORT="$1"
|
2019-05-18 13:26:04 +02:00
|
|
|
else
|
2019-07-06 16:46:56 +02:00
|
|
|
VERSION_SHORT=$(find . -maxdepth 1 -type d | sort | tail -1 | grep -o "[[0-9]].[[0-9]]*")
|
2019-05-18 13:26:04 +02:00
|
|
|
EXTRA_TAG=latest
|
2019-05-02 17:28:20 +02:00
|
|
|
fi
|
|
|
|
|
2019-07-06 16:46:56 +02:00
|
|
|
cd "$VERSION_SHORT" || exit 1
|
2019-05-02 17:28:20 +02:00
|
|
|
|
2019-05-18 13:26:04 +02:00
|
|
|
VERSION=$(grep -oP '[0-9]+\.[0-9]+\.[0-9]+' Dockerfile | head -1)
|
|
|
|
DOCKER_REPO=factoriotools/factorio
|
|
|
|
|
2021-01-28 00:55:45 +02:00
|
|
|
BRANCH=${GITHUB_REF#refs/*/}
|
2021-01-26 20:43:14 +02:00
|
|
|
|
|
|
|
if [[ -n ${GITHUB_BASE_REF:-} ]]; then
|
2021-02-05 23:55:04 +02:00
|
|
|
TAGS="-t $DOCKER_REPO:$GITHUB_BASE_REF"
|
2019-05-16 19:23:07 +02:00
|
|
|
else
|
2019-10-18 11:37:43 +02:00
|
|
|
if [[ -n ${CI:-} ]]; then
|
|
|
|
# we are either on master or on a tag build
|
2021-01-26 20:43:14 +02:00
|
|
|
if [[ ${BRANCH:-} == master || ${BRANCH:-} == "$VERSION" ]]; then
|
2019-10-18 11:56:07 +02:00
|
|
|
TAGS="-t $DOCKER_REPO:$VERSION -t $DOCKER_REPO:$VERSION_SHORT"
|
2019-10-18 11:37:43 +02:00
|
|
|
# we are on an incremental build of a tag
|
2021-01-26 20:43:14 +02:00
|
|
|
elif [[ $VERSION == "${BRANCH%-*}" ]]; then
|
|
|
|
TAGS="-t $DOCKER_REPO:$BRANCH -t $DOCKER_REPO:$VERSION -t $DOCKER_REPO:$VERSION_SHORT"
|
2020-05-18 03:56:55 +02:00
|
|
|
# we build a other branch than master and exclude dependabot branches from tags cause the / is not supported by docker
|
2021-01-26 20:43:14 +02:00
|
|
|
elif [[ -n ${BRANCH:-} && ! $BRANCH =~ "/" ]]; then
|
|
|
|
TAGS="-t $DOCKER_REPO:$BRANCH"
|
2019-10-18 11:37:43 +02:00
|
|
|
fi
|
|
|
|
else
|
|
|
|
# we are not in CI and tag version and version short
|
2019-10-18 11:52:25 +02:00
|
|
|
TAGS="-t $DOCKER_REPO:$VERSION -t $DOCKER_REPO:$VERSION_SHORT"
|
2019-05-16 19:23:07 +02:00
|
|
|
fi
|
|
|
|
|
2019-07-06 16:56:59 +02:00
|
|
|
if [[ -n ${EXTRA_TAG:-} ]]; then
|
2019-10-18 10:50:25 +02:00
|
|
|
IFS=","
|
|
|
|
for TAG in $EXTRA_TAG; do
|
2019-11-01 11:15:23 +02:00
|
|
|
TAGS+=" -t $DOCKER_REPO:$TAG"
|
2019-10-18 10:50:25 +02:00
|
|
|
done
|
2019-05-16 19:23:07 +02:00
|
|
|
fi
|
2019-11-07 01:21:39 +02:00
|
|
|
|
|
|
|
if [[ ${STABLE:-} == "$VERSION" ]]; then
|
2019-11-20 23:53:23 +02:00
|
|
|
TAGS+=" -t $DOCKER_REPO:stable"
|
2019-11-07 01:21:39 +02:00
|
|
|
fi
|
2019-05-16 19:23:07 +02:00
|
|
|
fi
|
|
|
|
|
2021-01-15 11:39:24 +02:00
|
|
|
# Travis gets rate limited by Docker HUB.
|
2021-01-26 20:57:15 +02:00
|
|
|
if [[ ${CI:-} == true && -n ${DOCKER_PASSWORD:-} && -n ${DOCKER_USERNAME:-} ]]; then
|
2021-01-15 11:39:24 +02:00
|
|
|
echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin
|
|
|
|
fi
|
|
|
|
|
2019-10-18 11:21:15 +02:00
|
|
|
# shellcheck disable=SC2068
|
2020-06-19 18:39:59 +02:00
|
|
|
eval docker build . ${TAGS[@]:-}
|
2019-05-16 19:23:07 +02:00
|
|
|
docker images
|
|
|
|
|
2020-05-18 03:04:12 +02:00
|
|
|
# remove -1 from incremental tag
|
|
|
|
# eg before: 0.18.24-1, after 0.18.24
|
2021-01-26 20:43:14 +02:00
|
|
|
if [[ ${BRANCH:-} ]]; then
|
|
|
|
BRANCH_VERSION=${BRANCH%-*}
|
2019-10-18 11:37:43 +02:00
|
|
|
fi
|
|
|
|
|
2019-07-06 16:46:56 +02:00
|
|
|
# only push when:
|
2020-05-18 03:07:19 +02:00
|
|
|
# or we build a tag and we don't build a PR
|
2021-01-26 20:43:14 +02:00
|
|
|
if [[ $VERSION == "${BRANCH_VERSION:-}" && ${GITHUB_BASE_REF:-} == "" ]] ||
|
2020-05-18 03:07:19 +02:00
|
|
|
# or we are not in CI
|
2019-10-18 11:37:43 +02:00
|
|
|
[[ -z ${CI:-} ]]; then
|
2019-05-18 13:40:25 +02:00
|
|
|
|
2019-10-18 11:37:43 +02:00
|
|
|
if [[ ${CI:-} == true ]]; then
|
2019-05-18 13:26:04 +02:00
|
|
|
echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin
|
2019-05-16 19:23:07 +02:00
|
|
|
fi
|
|
|
|
|
2020-05-18 03:56:55 +02:00
|
|
|
# push a tag on a branch other than master except dependabot branches cause docker does not support /
|
2021-01-26 20:43:14 +02:00
|
|
|
if [[ -n ${BRANCH:-} && $VERSION != "${BRANCH_VERSION:-}" && ${BRANCH:-} != "master" && ! ${BRANCH:-} =~ "/" ]]; then
|
|
|
|
docker push "$DOCKER_REPO:$BRANCH"
|
2020-05-18 03:56:55 +02:00
|
|
|
fi
|
2019-07-06 16:46:56 +02:00
|
|
|
|
|
|
|
# push an incremental tag
|
2020-05-18 03:04:12 +02:00
|
|
|
# eg 0.18.24-1
|
2021-01-26 20:43:14 +02:00
|
|
|
if [[ $VERSION == "${BRANCH_VERSION:-}" ]]; then
|
|
|
|
docker push "$DOCKER_REPO:$BRANCH"
|
2019-07-06 16:46:56 +02:00
|
|
|
fi
|
|
|
|
|
2020-05-18 03:04:12 +02:00
|
|
|
# only push on tags or when manually running the script
|
2021-01-28 01:19:46 +02:00
|
|
|
if [[ -n ${BRANCH_VERSION:-} || -z ${CI:-} ]]; then
|
2019-05-16 19:23:07 +02:00
|
|
|
docker push "$DOCKER_REPO:$VERSION"
|
|
|
|
docker push "$DOCKER_REPO:$VERSION_SHORT"
|
|
|
|
fi
|
2019-07-06 16:46:56 +02:00
|
|
|
|
2019-07-06 16:56:59 +02:00
|
|
|
if [[ -n ${EXTRA_TAG:-} ]]; then
|
2019-10-18 10:50:25 +02:00
|
|
|
IFS=","
|
|
|
|
for TAG in $EXTRA_TAG; do
|
|
|
|
docker push "$DOCKER_REPO:$TAG"
|
|
|
|
done
|
2019-05-18 13:26:04 +02:00
|
|
|
fi
|
2019-05-16 19:23:07 +02:00
|
|
|
|
2019-11-07 01:21:39 +02:00
|
|
|
if [[ ${STABLE:-} == "$VERSION" ]]; then
|
|
|
|
docker push "$DOCKER_REPO:stable"
|
|
|
|
fi
|
2019-05-16 19:23:07 +02:00
|
|
|
fi
|