Skip to content

Instantly share code, notes, and snippets.

@pjcdawkins

pjcdawkins/.gitlab-ci.yml

Last active Jun 12, 2020
Embed
What would you like to do?
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

This comment has been minimized.

Copy link

@rvanlaak rvanlaak commented Feb 17, 2017

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

@pjcdawkins

This comment has been minimized.

Copy link
Owner Author

@pjcdawkins 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

This comment has been minimized.

Copy link

@dfeyer dfeyer commented Aug 30, 2017

Thanks for sharing this, I will test it tomorrow

@rvanlaak

This comment has been minimized.

Copy link

@rvanlaak rvanlaak commented Nov 29, 2017

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

@pjcdawkins

This comment has been minimized.

Copy link
Owner Author

@pjcdawkins pjcdawkins commented Dec 8, 2017

@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

This comment has been minimized.

Copy link

@stoopman stoopman commented Feb 12, 2020

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

This comment has been minimized.

Copy link
Owner Author

@pjcdawkins pjcdawkins commented Feb 12, 2020

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

This comment has been minimized.

Copy link

@stoopman stoopman commented Feb 12, 2020

@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

This comment has been minimized.

Copy link
Owner Author

@pjcdawkins pjcdawkins commented Feb 12, 2020

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

This comment has been minimized.

Copy link
Owner Author

@pjcdawkins pjcdawkins commented Apr 28, 2020

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
You can’t perform that action at this time.