From 2b6bf9f8b821f538242299b915b24b8a0a82e9ae Mon Sep 17 00:00:00 2001 From: Raul Date: Mon, 30 Jul 2018 15:20:05 +0200 Subject: [PATCH] Add AWS deployments and PR dynamic environments --- .travis.yml | 50 +++++++++++++++-- config/deploy/deploy.sh | 82 ---------------------------- config/travis/deploy_pull_request.sh | 44 +++++++++++++++ config/travis/deploy_release.sh | 22 ++++++++ 4 files changed, 112 insertions(+), 86 deletions(-) delete mode 100644 config/deploy/deploy.sh create mode 100755 config/travis/deploy_pull_request.sh create mode 100755 config/travis/deploy_release.sh diff --git a/.travis.yml b/.travis.yml index 065875a0..9b5cfd7f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,13 +1,19 @@ +if: (branch = development) OR (branch = master) OR (type = pull_request) OR (tag IS present) language: node_js node_js: - "8" os: - linux +before_install: + # Needed to deploy pull request and releases + - sudo apt-get -y install python-pip python-dev + - pip install awscli --upgrade --user before_script: - - yarn global add surge + # Used in the tests of the project - export NODE_ENV=testing after_success: - | + if [ ${TRAVIS_BRANCH} = "master" ]; then export NODE_ENV=production; else @@ -15,6 +21,42 @@ after_success: fi - yarn build-storybook - yarn build - - cd build_webpack/ && cp index.html 200.html && cd .. - - chmod ugo+x ./config/deploy/deploy.sh - - ./config/deploy/deploy.sh \ No newline at end of file + # Pull Request - Deploy it to a review environment + # Travis doesn't do deploy step with pull requests builds + - ./config/travis/deploy_pull_request.sh + # Releases (tagged commits) - Deploy it to a release environment + - ./config/travis/deploy_release.sh + +deploy: + # Development environment +- provider: s3 + bucket: $DEV_BUCKET_NAME + access_key_id: $AWS_ACCESS_KEY_ID + secret_access_key: $AWS_SECRET_ACCESS_KEY + skip_cleanup: true + local_dir: build_webpack + upload-dir: app + on: + branch: development + + # Development environment - Storybook +- provider: s3 + bucket: $DEV_BUCKET_NAME + access_key_id: $AWS_ACCESS_KEY_ID + secret_access_key: $AWS_SECRET_ACCESS_KEY + skip_cleanup: true + local_dir: build_storybook + upload-dir: storybook + on: + branch: development + + # Staging environment +- provider: s3 + bucket: $STAGING_BUCKET_NAME + access_key_id: $AWS_ACCESS_KEY_ID + secret_access_key: $AWS_SECRET_ACCESS_KEY + skip_cleanup: true + local_dir: build_webpack + upload-dir: current + on: + branch: master diff --git a/config/deploy/deploy.sh b/config/deploy/deploy.sh deleted file mode 100644 index 851809aa..00000000 --- a/config/deploy/deploy.sh +++ /dev/null @@ -1,82 +0,0 @@ - -#!/bin/bash - -echo "Deployment script for gnosis-safe-team" - -RANGE=500 - -number=$RANDOM -let "number %= $RANGE" - -# Split on "/", ref: http://stackoverflow.com/a/5257398/689223 -REPO_SLUG_ARRAY=(${TRAVIS_REPO_SLUG//\// }) -REPO_OWNER=${REPO_SLUG_ARRAY[0]} -REPO_NAME=${REPO_SLUG_ARRAY[1]} - -DEPLOY_PATH=./build_webpack -DEPLOY_PATH_STORYBOOK=./build_storybook - -DEPLOY_SUBDOMAIN_UNFORMATTED_LIST=() - -if [ "$TRAVIS_PULL_REQUEST" != "false" ] -then - if [ "$NODE_ENV" == "production" ] - then - DEPLOY_SUBDOMAIN_UNFORMATTED_LIST+=(release-${TRAVIS_PULL_REQUEST}-pr-${number}) - else - DEPLOY_SUBDOMAIN_UNFORMATTED_LIST+=(staging-${TRAVIS_PULL_REQUEST}-pr-${number}) - fi -elif [ -n "${TRAVIS_TAG// }" ] #TAG is not empty -then - if [ "$NODE_ENV" == "production" ] - then - - #sorts the tags and picks the latest - #sort -V does not work on the travis machine - #sort -V ref: http://stackoverflow.com/a/14273595/689223 - #sort -t ... ref: http://stackoverflow.com/a/4495368/689223 - #reverse with sed ref: http://stackoverflow.com/a/744093/689223 - #git tags | ignore release candidates | sort versions | reverse | pick first line - LATEST_TAG=`git tag | grep -v rc | sort -t. -k 1,1n -k 2,2n -k 3,3n -k 4,4n | sed '1!G;h;$!d' | sed -n 1p` - echo $LATEST_TAG - - if [ "$TRAVIS_TAG" == "$LATEST_TAG" ] - then - DEPLOY_SUBDOMAIN_UNFORMATTED_LIST+=(latest) - fi - - DEPLOY_SUBDOMAIN_UNFORMATTED_LIST+=(${TRAVIS_TAG}-tag) - fi -else - DEPLOY_SUBDOMAIN_UNFORMATTED_LIST+=(${TRAVIS_BRANCH}-branch) -fi - -for DEPLOY_SUBDOMAIN_UNFORMATTED in "${DEPLOY_SUBDOMAIN_UNFORMATTED_LIST[@]}" -do - echo $DEPLOY_SUBDOMAIN_UNFORMATTED - - # replaces non alphanumeric symbols with "-" - # sed -r is only supported in linux, ref http://stackoverflow.com/a/2871217/689223 - # Domain names follow the RFC1123 spec [a-Z] [0-9] [-] - # The length is limited to 253 characters - # https://en.wikipedia.org/wiki/Domain_Name_System#Domain_name_syntax - DEPLOY_SUBDOMAIN=`echo "$DEPLOY_SUBDOMAIN_UNFORMATTED" | sed -r 's/[^A-Za-z0-9]+/\-/g'` - echo $DEPLOY_SUBDOMAIN - - DEPLOY_DOMAIN=https://${DEPLOY_SUBDOMAIN}-${REPO_NAME}-${REPO_OWNER}.surge.sh - DEPLOY_STORYBOOK=https://storybook-${DEPLOY_SUBDOMAIN}-${REPO_NAME}-${REPO_OWNER}.surge.sh - - surge --project ${DEPLOY_PATH} --domain $DEPLOY_DOMAIN; - surge --project ${DEPLOY_PATH_STORYBOOK} --domain $DEPLOY_STORYBOOK - - if [ "$TRAVIS_PULL_REQUEST" != "false" ] - then - # Using the Issues api instead of the PR api - # Done so because every PR is an issue, and the issues api allows to post general comments, - # while the PR api requires that comments are made to specific files and specific commits - GITHUB_PR_COMMENTS=https://api.github.com/repos/${TRAVIS_REPO_SLUG}/issues/${TRAVIS_PULL_REQUEST}/comments - curl -H "Authorization: token ${GITHUB_API_TOKEN}" --request POST ${GITHUB_PR_COMMENTS} --data '{"body":"Travis automatic deployment:\r\n '${DEPLOY_DOMAIN}' \r\n \r\n Storybook book automatic deployment: \r\n '${DEPLOY_STORYBOOK}'"}' - fi -done - -echo "Deploy domain: ${DEPLOY_DOMAIN}" \ No newline at end of file diff --git a/config/travis/deploy_pull_request.sh b/config/travis/deploy_pull_request.sh new file mode 100755 index 00000000..368c08c6 --- /dev/null +++ b/config/travis/deploy_pull_request.sh @@ -0,0 +1,44 @@ +#!/bin/bash + +function deploy_pull_request { + REVIEW_ENVIRONMENT_DOMAIN='review.gnosisdev.com' + + # Pull request name with "pr" prefix + PULL_REQUEST_NAME="pr$TRAVIS_PULL_REQUEST" + + # Feature name without all path. Example gnosis/pm-trading-ui -> pm-trading-ui + REPO_NAME=$(basename $TRAVIS_REPO_SLUG) + # Only alphanumeric characters. Example pm-trading-ui -> pmtradingui + REPO_NAME_ALPHANUMERIC=$(echo $REPO_NAME | sed 's/[^a-zA-Z0-9]//g') + + # TRAVIS_PULL_REQUEST contains pull request number + REVIEW_FEATURE_FOLDER="$REPO_NAME_ALPHANUMERIC/$PULL_REQUEST_NAME" + # Specific for this project only + REVIEW_FEATURE_STORYBOOK_FOLDER="${REPO_NAME_ALPHANUMERIC}storybook/$PULL_REQUEST_NAME" + + # Deploy safe-team project + aws s3 sync build_webpack s3://${REVIEW_BUCKET_NAME}/${REVIEW_FEATURE_FOLDER} --delete + # Deploy safe-team storybook project + aws s3 sync build_storybook s3://${REVIEW_BUCKET_NAME}/${REVIEW_FEATURE_STORYBOOK_FOLDER} --delete +} + +function publish_pull_request_urls_in_github { + REVIEW_FEATURE_URL="https://$PULL_REQUEST_NAME--$REPO_NAME_ALPHANUMERIC.$REVIEW_ENVIRONMENT_DOMAIN" + # Specific for this project only + REVIEW_FEATURE_STORYBOOK_URL="https://$PULL_REQUEST_NAME--${REPO_NAME_ALPHANUMERIC}storybook.$REVIEW_ENVIRONMENT_DOMAIN" + + # Using the Issues api instead of the PR api + # Done so because every PR is an issue, and the issues api allows to post general comments, + # while the PR api requires that comments are made to specific files and specific commits + GITHUB_PR_COMMENTS=https://api.github.com/repos/${TRAVIS_REPO_SLUG}/issues/${TRAVIS_PULL_REQUEST}/comments + curl -H "Authorization: token ${GITHUB_API_TOKEN}" --request POST ${GITHUB_PR_COMMENTS} --data '{"body":"Travis automatic deployment:\r\n '${REVIEW_FEATURE_URL}' \r\n \r\n Storybook book automatic deployment: \r\n '${REVIEW_FEATURE_STORYBOOK_URL}'"}' +} + +# Only: +# - Pull requests +# - Security env variables are available. PR from forks don't have them. +if [ "$TRAVIS_PULL_REQUEST" != "false" ] && [ -n "$AWS_ACCESS_KEY_ID" ] +then + deploy_pull_request + publish_pull_request_urls_in_github +fi diff --git a/config/travis/deploy_release.sh b/config/travis/deploy_release.sh new file mode 100755 index 00000000..b6991e51 --- /dev/null +++ b/config/travis/deploy_release.sh @@ -0,0 +1,22 @@ +#!/bin/bash + +# Only: +# - Tagged commits +# - Security env variables are available. +if [ -n "$TRAVIS_TAG" ] && [ -n "$AWS_ACCESS_KEY_ID" ] +then + REVIEW_ENVIRONMENT_DOMAIN='review.gnosisdev.com' + + # Feature name without all path. Example gnosis/pm-trading-ui -> pm-trading-ui + REPO_NAME=$(basename $TRAVIS_REPO_SLUG) + # Only alphanumeric characters. Example pm-trading-ui -> pmtradingui + REPO_NAME_ALPHANUMERIC=$(echo $REPO_NAME | sed 's/[^a-zA-Z0-9]//g') + + # Only alphanumeric characters. Example v1.0.0 -> v100 + TRAVIS_TAG_ALPHANUMERIC=$(echo $TRAVIS_TAG | sed 's/[^a-zA-Z0-9]//g') + + REVIEW_RELEASE_FOLDER="$REPO_NAME_ALPHANUMERIC/$TRAVIS_TAG_ALPHANUMERIC" + + # Deploy safe-team release project + aws s3 sync build_webpack s3://${REVIEW_BUCKET_NAME}/${REVIEW_RELEASE_FOLDER} --delete +fi