Last active
October 19, 2018 06:16
-
-
Save jeremysells/021c062d7f0e146c4703558b36cb2f23 to your computer and use it in GitHub Desktop.
bin/composer.sh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -e | |
# MIT licensed | |
# This script is AS-IS where is (no warranty, liability etc - see the license for full details) | |
# https://opensource.org/licenses/MIT and/or https://en.wikipedia.org/wiki/MIT_License | |
# Notes: | |
# * This script is meant to be loaded from the root of the git repo, which can contain a .env file (with project settings) | |
# * There is security concerns with loading ssh keys into a container!!! Use at your own risk! | |
# * This script is for demo only, use at your own risk. | |
# * Docker for Mac probably will not work with this (sorry I am a Linux user) | |
# * If you are calling this from Jenkins which is running in a Docker container, good luck - but it is possible! | |
# * * Hint: The volume mounts for Jenkins in Docker are relative to the host! (A symlink will fix this) | |
# * * Hint: Add a user to the host that's got a fixed ID and also has the same UID and GID as Jenkins in the container | |
# * * Hint: The user on the host needs to have the same ssh keys as the user in the container | |
# * * This is a little tricky to get your head around, but not too hard to implement | |
# * This also works for development machines (yay consistency!) | |
# * * I have briefly tested setting IntelliJ IDEA at this as the composer binary. An update worked - which is awesome! | |
# Make sure this script is run from the root dir | |
if [ ! -d ".git" ]; then | |
echo "ERROR: Please run from the project root" | |
exit 1 | |
fi | |
# Load project setting | |
# Note: I use a .env file, as this also works with docker-compose (for development) | |
if [ -f ".env" ]; then | |
source .env | |
fi | |
# Make sure we have the first argument | |
if [[ $# -eq 0 ]] ; then | |
echo "ERROR: No arguments supplied" | |
exit 1 | |
fi | |
# Work out the container and version, and pull it | |
# Note: Suppressing output of pulling the image here - this is to make this script more like a local binary | |
COMPOSER_VERSION=${DEV_COMPOSER_VERSION:-"latest"} | |
COMPOSER_IMAGE="composer:${COMPOSER_VERSION}" | |
docker pull ${COMPOSER_IMAGE} > /dev/null | |
# Work out the command | |
COMMAND=$@ | |
if [ $1 == "update" ] || [ $1 == "install" ]; then | |
COMMAND="${COMMAND} --ignore-platform-reqs --no-scripts" | |
fi | |
# Run composer in a container | |
# --tty \ | |
# --interactive \ | |
docker run \ | |
--rm \ | |
--user $(id -u):$(id -g) \ | |
--env SSH_AUTH_SOCK=/ssh-auth.sock \ | |
--env HOME=$HOME \ | |
--volume $(pwd):/app \ | |
--volume $SSH_AUTH_SOCK:/ssh-auth.sock \ | |
--volume $HOME/.ssh:$HOME/.ssh:ro \ | |
--volume /etc/passwd:/etc/passwd:ro \ | |
--volume /etc/group:/etc/group:ro \ | |
${COMPOSER_IMAGE} ${COMMAND} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment