Skip to content

Instantly share code, notes, and snippets.

@pjcdawkins
Last active June 12, 2020 15:45
Show Gist options
  • Save pjcdawkins/0b3f7a6da963c129030961f0947746c4 to your computer and use it in GitHub Desktop.
Save pjcdawkins/0b3f7a6da963c129030961f0947746c4 to your computer and use it in GitHub Desktop.
GitLab - Platform.sh CI scripts
stages:
- review
- cleanup
variables:
# The Platform.sh project ID.
PF_PROJECT_ID: abcdefg123456
push-platformsh:
# This Docker image installs the Platform.sh CLI (and PHP, Git and SSH).
# An environment variable PLATFORMSH_CLI_TOKEN (containing an API token) is
# the recommended way to authenticate for the CLI.
image: pjcdawkins/platformsh-cli
stage: review
script:
# Install an SSH key pair set via GitLab CI environment variables
# (SSH_KEY, SSH_KEY_PUB, and SSH_KNOWN_HOSTS).
- 'bash setup-ssh.sh'
# Push the branch to a Platform.sh Standard environment.
- 'bash push-platform.sh'
artifacts:
reports:
dotenv: environment.env
environment:
name: review/$CI_COMMIT_REF_SLUG
# The PRIMARY_URL is set by the push-platform.sh script and the "dotenv" artifact.
url: $PRIMARY_URL
on_stop: delete-platformsh
only:
- branches
except:
# This excludes the master branch from attempted pushes, on the
# assumption that one might want to make master pushes manually.
- master
delete-platformsh:
stage: cleanup
image: pjcdawkins/platformsh-cli
script:
- 'bash delete-platform.sh'
when: manual
environment:
name: review/$CI_COMMIT_REF_SLUG
action: stop
only:
- branches
except:
- master
#!/usr/bin/env bash
set -e
# Config.
if [ -z "$PF_PROJECT_ID" ]; then
echo "PF_PROJECT_ID is required"
exit 1
fi
PF_BRANCH=${PF_DEST_BRANCH:-$CI_COMMIT_REF_SLUG}
if [ -z "$PF_BRANCH" ]; then
echo "Branch name (CI_COMMIT_REF_SLUG or PF_DEST_BRANCH) not defined."
exit 1
fi
# Delete the specified branch.
platform environment:delete --yes --no-wait --project="$PF_PROJECT_ID" --environment="$PF_BRANCH"
# Clean up inactive environments.
platform environment:delete --project="$PF_PROJECT_ID" --inactive --exclude=master --yes --delete-branch --no-wait || true
#!/usr/bin/env bash
set -e
# Push Gitlab branches to Platform.sh environments.
# -------------------------------------------------
#
# This script can be configured by specifying environment variables in your
# repository settings or in the .gitlab-ci.yml file:
#
# variables:
#
# # The project ID (required).
# PF_PROJECT_ID: abcdefg123456
#
#  # The parent environment of new branches.
# PF_PARENT_ENV: master
# Config.
if [ -z "$PF_PROJECT_ID" ]; then
echo "PF_PROJECT_ID is required"
exit 1
fi
PF_PARENT_ENV=${PF_PARENT_ENV:-master}
ALLOW_MASTER=${ALLOW_MASTER:-0}
export PLATFORMSH_CLI_NO_INTERACTION=1
if [ -z "$CI_COMMIT_REF_SLUG" ]; then
echo "Source branch (CI_COMMIT_REF_SLUG) not defined."
exit 1
fi
BRANCH=$CI_COMMIT_REF_SLUG
# This script is not for production deployments.
if [ "$BRANCH" = "master" ] && [ "$ALLOW_MASTER" != 1 ]; then
echo "Not pushing master branch."
exit
fi
# Set the project for further CLI commands.
platform project:set-remote "$PF_PROJECT_ID"
# Get a URL to the web UI for this environment, before pushing.
pf_ui=$(platform web --environment="$BRANCH" --pipe)
echo ""
echo "Web UI: ${pf_ui}"
echo ""
# Build the push command.
push_command="platform push --force --target=${BRANCH}"
if [ "$PF_PARENT_ENV" != "$BRANCH" ]; then
push_command="$push_command --activate --parent=${PF_PARENT_ENV}"
fi
# Run the push command.
$push_command
# Write the environment's primary URL to a dotenv file.
# This can be used by a GitLab job via the "dotenv" artifact type.
echo "PRIMARY_URL=$(platform url --primary --pipe --yes --environment=${BRANCH})" > environment.env
# Clean up already merged and inactive environments.
platform environment:delete --inactive --merged --environment="$PF_PARENT_ENV" --exclude=master --exclude=development --exclude="$BRANCH" --yes --delete-branch --no-wait || true
#!/usr/bin/env bash
set -e
# Set up SSH credentials for pushing to external Git repositories, via GitLab CI
# environment variables. You should add this public key to a Platform user for
# the push to be successful.
if [ -n "$SSH_KEY" ]; then
mkdir -p $HOME/.ssh
echo "$SSH_KEY" > $HOME/.ssh/id_rsa
echo "$SSH_KEY_PUB" > $HOME/.ssh/id_rsa.pub
# Some vague semblance of security for the private key.
chmod go-r $HOME/.ssh/id_rsa
unset SSH_KEY
echo "Created SSH key: .ssh/id_rsa"
fi
# Set up SSH known hosts file.
if [ -n "$SSH_KNOWN_HOSTS" ]; then
mkdir -p $HOME/.ssh
echo "$SSH_KNOWN_HOSTS" > $HOME/.ssh/known_hosts
fi
@pjcdawkins
Copy link
Author

That's now available in GitLab (since version 12.9), so I've updated the gist to set the environment URL dynamically.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment