Skip to content

Instantly share code, notes, and snippets.

@pjcdawkins
Last active June 12, 2020 15:45
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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
@rvanlaak
Copy link

@pjcdawkins I've created an issue at Gitlab for this as well: https://gitlab.com/gitlab-org/gitlab-ce/issues/28338

@pjcdawkins
Copy link
Author

pjcdawkins commented Feb 17, 2017

Thanks. By the way, one of my main problems in GitLab is using review apps/environments: we are not able to set the environment URL based on the result of a CI stage / script (the GitLab environment URLs can only include the ref name variable and maybe one or two other things, but are otherwise static). But Platform.sh environment URLs are very dynamic.

Also I should note: the CLI use in push-platform.sh and delete-platform.sh requires a variable PLATFORMSH_CLI_TOKEN

@dfeyer
Copy link

dfeyer commented Aug 30, 2017

Thanks for sharing this, I will test it tomorrow

@rvanlaak
Copy link

@pjcdawkins does your recent PR on Platform.sh CLI mean that using the CLI tool would make these workflow steps even easier?

@pjcdawkins
Copy link
Author

@rvanlaak yes, see https://github.com/platformsh/platformsh-docs/pull/701/files (to be merged soon) - the new GitLab integration, now available on nearly any project, adds Platform.sh as an "external pipeline" to a GitLab repo

@stoopman
Copy link

Thanks. By the way, one of my main problems in GitLab is using review apps/environments: we are not able to set the environment URL based on the result of a CI stage / script (the GitLab environment URLs can only include the ref name variable and maybe one or two other things, but are otherwise static). But Platform.sh environment URLs are very dynamic.

@pjcdawkins would it be possible to set the URL of the environment in the MR through the Gitlab API? I think this would work in theory, but I'm not sure how the setup of this would look like

@pjcdawkins
Copy link
Author

Sadly I don't think GitLab provides that API.

GitLab allows the URL to contain variables (other than those defined in the script itself), so on most projects I've been using:

  environment:
    url: https://console.platform.sh/projects/$PF_PROJECT_ID/$CI_COMMIT_REF_SLUG

That will get you (via a redirect) to the Console page for that environment, and then you can access the other URLs.

I'll update the scripts above with that.

@stoopman
Copy link

@pjcdawkins, thanks! that's actually a pretty nice solution since you can have more than 1 url, especially for multi-app projects.

As for the Gitlab API, I think it's possible (although not very pretty), as discussed here https://gitlab.com/gitlab-org/gitlab-foss/issues/27424

@pjcdawkins
Copy link
Author

Ah, that issue was moved to https://gitlab.com/gitlab-org/gitlab/issues/17066 and it looks like they're willing to work on a simpler solution

@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