Deploy Previews
![](https://i.imgur.com/T221NrN.png)
Servicios usados
![](https://about.gitlab.com/images/press/logo/jpg/gitlab-logo-gray-stacked-rgb.jpg)
![](https://seekvectorlogo.net/wp-content/uploads/2018/12/heroku-vector-logo.png)
![](https://www.netlify.com/img/press/logos/full-logo-light.png)
¿Para que sirven?
- Permite revisar "en vivo" los cambios enviados
- Permite tener múltiples sites/apps al mismo tiempo
- Permite desplegar en varios servicios
netlify:
image: node:12.10.0-alpine
stage: deploy
before_script:
- apk add --no-cache jq curl
- npm i -g netlify-cli
script:
- npm run build
# Deploy site to netlify
- NETLIFY_DEPLOY_OUTPUT=$(netlify deploy --json)
# Get site url
- NETLIFY_DEPLOY_URL=$(echo $NETLIFY_DEPLOY_OUTPUT | jq -r ".deploy_url")
# Set common api variables
- AUTH_HEADER="Private-Token:"" "$PRIVATE_TOKEN
- BASE_URL="https://gitlab.com/api/v4"
# Get current environment id
- ENDPOINT_LIST_ENVIRONMENTS="$BASE_URL/projects/$CI_PROJECT_ID/environments"
- ENVIRONMENTS=$(curl "$ENDPOINT_LIST_ENVIRONMENTS" -H "$AUTH_HEADER")
- CI_ENVIRONMENT_ID=$(echo $ENVIRONMENTS | jq -r '.[] | select(.name | contains("'$CI_ENVIRONMENT_NAME'")) | .id')
# Update environment url
- ENDPOINT_UPDATE_ENVIRONMENT="$BASE_URL/projects/$CI_PROJECT_ID/environments/$CI_ENVIRONMENT_ID"
- curl -X PUT "$ENDPOINT_UPDATE_ENVIRONMENT" -H "$AUTH_HEADER" --data "external_url=$NETLIFY_DEPLOY_URL"
# Get merge request id
- ENDPOINT_LIST_MR="$BASE_URL/projects/$CI_PROJECT_ID/merge_requests"
- MERGE_REQUESTS=$(curl "$ENDPOINT_LIST_MR" -H "$AUTH_HEADER")
- CI_MERGE_REQUEST_IID=$(echo $MERGE_REQUESTS | jq -r '.[] | select(.source_branch | contains("'$CI_BUILD_REF_NAME'")) | select(.state | contains("open")) | .iid')
# Create merge request note
- ENDPOINT_CREATE_MR_NOTE="$BASE_URL/projects/$CI_PROJECT_ID/merge_requests/$CI_MERGE_REQUEST_IID/notes"
- NOTE_BODY="Deployed the commit $CI_COMMIT_SHORT_SHA to $NETLIFY_DEPLOY_URL"
- curl -X POST "$ENDPOINT_CREATE_MR_NOTE" -H "$AUTH_HEADER" --data "body=$NOTE_BODY"
environment:
name: review/$CI_BUILD_REF_NAME
only:
- branches
except:
- master
start_review:
image: ruby:2.6.5-alpine3.10
stage: review
before_script:
- apk add --no-cache curl jq git
- gem install dpl --no-doc
script:
# Set review app name
- CI_COMMIT_REF_SLUG_FIX=$(echo $CI_COMMIT_REF_SLUG | sed 's/-//g')
- REVIEW_APP_NAME=$(echo "$CI_PROJECT_NAME-$CI_COMMIT_REF_SLUG_FIX" | cut -c 1-30)
# Create review app
- PAYLOAD='{"name":"'"$REVIEW_APP_NAME"'","region":"us","stack":"heroku-18"}'
- HEROKU_HEADER="accept:"" ""application/vnd.heroku+json; version=3"
- JSON_HEADER="content-type:"" ""application/json"
- HEROKU_AUTH_HEADER="authorization:"" Bearer "$HEROKU_API_KEY
- |
curl -n -X POST https://api.heroku.com/apps \
-d "$PAYLOAD" \
-H "$HEROKU_HEADER" \
-H "$HEROKU_AUTH_HEADER" \
-H "$JSON_HEADER"
# Get config vars from production app
- ENDPOINT_LIST_CONFIG_VARS="https://api.heroku.com/apps/cummins-backend/config-vars"
- PAYLOAD=$(curl "$ENDPOINT_LIST_CONFIG_VARS" -H "$HEROKU_HEADER" -H "$HEROKU_AUTH_HEADER" -H "$JSON_HEADER")
# Set config vars to review app
- |
curl -X PATCH https://api.heroku.com/apps/$REVIEW_APP_NAME/config-vars \
-d "$PAYLOAD" \
-H "$HEROKU_HEADER" \
-H "$HEROKU_AUTH_HEADER" \
-H "$JSON_HEADER"
# Deploy review app
- dpl --provider=heroku --app=$REVIEW_APP_NAME --api-key=$HEROKU_API_KEY
# Set review app url
- REVIEW_APP_URL="https://$REVIEW_APP_NAME.herokuapp.com"
# Set common gitlab api variables
- AUTH_HEADER="Private-Token:"" "$GITLAB_HEROKU_API_KEY
- BASE_URL="https://gitlab.com/api/v4"
# Get current environment id
- ENDPOINT_LIST_ENVIRONMENTS="$BASE_URL/projects/$CI_PROJECT_ID/environments"
- ENVIRONMENTS=$(curl "$ENDPOINT_LIST_ENVIRONMENTS" -H "$AUTH_HEADER")
- CI_ENVIRONMENT_ID=$(echo $ENVIRONMENTS | jq -r '.[] | select(.name | contains("'$CI_ENVIRONMENT_NAME'")) | .id')
# Update environment url
- ENDPOINT_UPDATE_ENVIRONMENT="$BASE_URL/projects/$CI_PROJECT_ID/environments/$CI_ENVIRONMENT_ID"
- curl -X PUT "$ENDPOINT_UPDATE_ENVIRONMENT" -H "$AUTH_HEADER" --data "external_url=$REVIEW_APP_URL"
# Get merge request id
- ENDPOINT_LIST_MR="$BASE_URL/projects/$CI_PROJECT_ID/merge_requests"
- MERGE_REQUESTS=$(curl "$ENDPOINT_LIST_MR" -H "$AUTH_HEADER")
- CI_MERGE_REQUEST_IID=$(echo $MERGE_REQUESTS | jq -r '.[] | select(.source_branch | contains("'$CI_BUILD_REF_NAME'")) | select(.state | contains("open")) | .iid')
# Create merge request note
- ENDPOINT_CREATE_MR_NOTE="$BASE_URL/projects/$CI_PROJECT_ID/merge_requests/$CI_MERGE_REQUEST_IID/notes"
- NOTE_BODY="Deployed the commit $CI_COMMIT_SHORT_SHA to $REVIEW_APP_URL"
- curl -X POST "$ENDPOINT_CREATE_MR_NOTE" -H "$AUTH_HEADER" --data "body=$NOTE_BODY"
environment:
name: review/$CI_BUILD_REF_NAME
on_stop: stop_review
only:
- branches
except:
- master
stop_review:
image: alpine:3.10.2
stage: review
variables:
GIT_STRATEGY: none
before_script:
- apk add --no-cache curl
script:
# Set review app name
- CI_COMMIT_REF_SLUG_FIX=$(echo $CI_COMMIT_REF_SLUG | sed 's/-//g')
- REVIEW_APP_NAME=$(echo "$CI_PROJECT_NAME-$CI_COMMIT_REF_SLUG_FIX" | cut -c 1-30)
# Delete heroku review app
- |
curl -X DELETE https://api.heroku.com/apps/$REVIEW_APP_NAME \
-H "Content-Type: application/json" \
-H "Accept: application/vnd.heroku+json; version=3" \
-H "Authorization: Bearer $HEROKU_API_KEY"
when: manual
environment:
name: review/$CI_BUILD_REF_NAME
action: stop
only:
- branches
except:
- master
![](https://i.imgur.com/LU8J96y.png)
![](https://i.imgur.com/VMQCqa0.png)
![](https://i.imgur.com/x7F65KB.png)
![](https://i.imgur.com/lyuOGx4.png)
Gracias
Deploy preview
By c778551
Deploy preview
- 109