Skip to content

Instantly share code, notes, and snippets.

@nikunjkotecha
Created March 15, 2017 06:41
Show Gist options
  • Save nikunjkotecha/c82d0ec58bcaad6315a59bfbf5b4b62f to your computer and use it in GitHub Desktop.
Save nikunjkotecha/c82d0ec58bcaad6315a59bfbf5b4b62f to your computer and use it in GitHub Desktop.
Push the code from GITLAB to Platform
#!/usr/bin/env bash
set -e
# This script can be configured by specifying different environment variables in
# your .gitlab-ci.yml file's invocation of the script. If those are omitted, as
# in this example, the defaults below and throughout the script should be used.
# Check basic requirements from Config.
if [ -z "$PLATFORM_PROJECT_ID" ]; then
echo "PLATFORM_PROJECT_ID is required, please contact support if you don't know how to do it."
exit 1
fi
if [ -z "$PLATFORM_TOKEN" ]; then
echo "PLATFORM_TOKEN is required, please contact support if you don't know how to do it."
exit 1
fi
# By default we use master as the Platform parent env.
PF_PARENT_ENV=${PF_PARENT_ENV:-master}
# By default we don't allow master to be deployed.
ALLOW_MASTER=${ALLOW_MASTER:-0}
# Prepare the variables.
PF_BRANCH=${PF_DEST_BRANCH:-$CI_BUILD_REF_NAME}
# Platform command path.
CLI_CMD=${CLI_CMD:-"~/.platformsh/bin/platform"}
if [ -z "$PF_BRANCH" ]; then
echo "Source branch (CI_BUILD_REF_NAME or PF_DEST_BRANCH) not defined."
exit 1
fi
# This script is not for production deployments.
if [ "$PF_BRANCH" = "master" ] && [ "$ALLOW_MASTER" != 1 ]; then
echo "Not pushing master branch."
exit
fi
# Start: Setup access to platform.sh.
# Create platformsh directory in home directory.
# Platform cli checks for API token only in home directory as of now.
mkdir -p ~/.platformsh
# Remove the old config yaml file.
rm -f ~/.platformsh/config.yaml
# Create the config file.
touch ~/.platformsh/config.yaml
# Write to config.yml
echo "api:" >> ~/.platformsh/config.yaml
echo " token_file: apitoken" >> ~/.platformsh/config.yaml
# Remove the apitoken file and ensure the latest in code is used.
rm -f ~/.platformsh/apitoken
# Create a file to hold API token.
touch ~/.platformsh/apitoken
# Write the token in token file
echo $PLATFORM_TOKEN >> ~/.platformsh/apitoken
# End: Setup access to platform.sh.
# Set the project for further CLI commands.
COMMAND_SET_REMOTE="${CLI_CMD} project:set-remote ${PLATFORM_PROJECT_ID}"
eval $COMMAND_SET_REMOTE
# Push to PS.
COMMAND_PUSH="${CLI_CMD} push --verbose --force --target=${PF_BRANCH}"
if [ "$PF_PARENT_ENV" != "$PF_BRANCH" ]; then
COMMAND_PUSH="$COMMAND_PUSH --activate --parent=${PF_PARENT_ENV}"
fi
eval $COMMAND_PUSH
# Clean up already merged and inactive environments.
COMMAND_CLEANUP="${CLI_CMD} environment:delete --verbose --inactive --merged --environment=${PF_PARENT_ENV} --exclude=master --exclude="${PF_BRANCH}" --yes --delete-branch --no-wait || true"
eval $COMMAND_CLEANUP
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment