Tamarin/package-project.sh

87 lines
1.9 KiB
Bash
Raw Normal View History

#!/usr/bin/env bash
set -e
DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
source "$DIR/lib/util.sh"
function show_usage {
echo
echo "Usage: $0 <src> <dist> <image>"
echo
echo "Paramètres: "
echo
echo " - <src> Chemin vers le répertoire des sources du projet à empaqueter"
echo " - <dist> Chemin vers le répertoire de destination du paquet à créer"
echo " - <image> Nom de l'image Docker à utiliser comme base pourl'environnement de build. Exemple: debian:jessie"
echo
}
# Create a build container based on the $BASE_IMAGE argument
function create_container {
# Escape image name
escaped_basename=$(echo "$BASE_IMAGE" | sed 's/[^a-z0-9\-\_\.]/\_/gi')
# Generate container tag
container_tag="tamarin:${escaped_basename}_$(date +%s)"
# Create temporary dir for the Dockerfile
temp_dir="$(mktemp -d)"
# Link lib folder
ln -s $(readlink -f "$DIR/lib") "$temp_dir/lib"
# Create Dockerfile
cat << EOF > "$temp_dir/Dockerfile"
FROM $BASE_IMAGE
ENV DEBIAN_FRONTEND noninteractive
RUN apt-get update &&\
apt-get install jq
ADD ./lib /root/.tamarin
RUN chmod +x /root/.tamarin/build.sh
VOLUME /src
VOLUME /dist
CMD /root/.tamarin/build.sh
EOF
# Build image
tar -C "$temp_dir" -czh . | docker build -t "$container_tag" - 1>&2
# Delete temporary folder
rm -rf "$temp_dir"
# Return newly created container's tag
echo $container_tag
}
# Main function
function main {
info "Building container from $BASE_IMAGE..."
container_tag="$(create_container)"
info "Launching container..."
docker run -e "DISTRIB=$BASE_IMAGE" --rm -v="$PROJECT_PATH:/src" -v="$PROJECT_DIST:/dist" "$container_tag"
info "Cleaning container data..."
info "Done"
}
# Test for arguments
if [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ]; then
show_usage
exit 1
fi
PROJECT_PATH="$(readlink -f $1)"
PROJECT_DIST="$(readlink -f $2)"
BASE_IMAGE="$3"
main