Sample node project deployment on Gitlab with caching of node_modules, test, build.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
image: node:12.9.0-alpine | |
.ssh: &ssh | |
before_script: | |
- "which ssh-agent || ( apk update && apk add openssh-client )" | |
- eval $(ssh-agent -s) | |
- mkdir -p ~/.ssh | |
- chmod 700 ~/.ssh | |
- '[[ -f /.dockerenv ]] && echo -e "Host *.$STAGING_ENV\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config' | |
- echo "$SSH_PRIVATE_KEY" > ~/.ssh/knit.pem | |
- chmod 400 ~/.ssh/knit.pem | |
- ssh-keyscan $STAGING_ENV >> ~/.ssh/known_hosts | |
- chmod 644 ~/.ssh/known_hosts | |
stages: | |
- install_dependencies | |
- test | |
- build | |
- review | |
- deploy | |
install_dependencies: | |
stage: install_dependencies | |
script: | |
- echo "Installing dependencies..." | |
- npm ci | |
cache: | |
key: ${CI_COMMIT_REF_SLUG} | |
paths: | |
- node_modules/ | |
artifacts: | |
paths: | |
- node_modules/ | |
expire_in: 1 hour | |
test: | |
stage: test | |
script: | |
- echo "Running tests..." | |
- npm run test:unit | |
only: | |
- pushes | |
build: | |
stage: build | |
script: | |
- echo "Building..." | |
- npm run build | |
- tar -czf dist.tar.gz dist/ | |
artifacts: | |
paths: | |
- dist.tar.gz | |
expire_in: 1 hour | |
only: | |
refs: | |
- master | |
build:review: | |
stage: build | |
script: | |
- echo "Building... $CI_BUILD_REF_SLUG" | |
- npm run build | |
- mv dist/ $CI_BUILD_REF_SLUG | |
- tar -czf $CI_BUILD_REF_SLUG.tar.gz $CI_BUILD_REF_SLUG/ | |
artifacts: | |
paths: | |
- $CI_BUILD_REF_SLUG.tar.gz | |
expire_in: 1 hour | |
only: | |
- branches | |
except: | |
- master | |
review: | |
stage: review | |
<<: *ssh | |
script: | |
- echo "Deploying review-apps $CI_BUILD_REF_SLUG" | |
- ./scripts/review.sh | |
environment: | |
name: review/$CI_BUILD_REF_NAME | |
url: http://$CI_BUILD_REF_SLUG-play.$APPS_DOMAIN | |
on_stop: review:stop | |
dependencies: | |
- build:review | |
only: | |
- branches | |
except: | |
- master | |
review:stop: | |
stage: review | |
<<: *ssh | |
script: | |
- echo "Stopping review apps $CI_BUILD_REF_SLUG.." | |
- ssh -i ~/.ssh/knit.pem $USERNAME@$STAGING_ENV " | |
cd $REVIEW_APPS_DIR && | |
rm -rf $CI_BUILD_REF_SLUG " | |
when: manual | |
variables: | |
GIT_STRATEGY: none | |
environment: | |
name: review/$CI_BUILD_REF_NAME | |
action: stop | |
dependencies: [] | |
deploy_staging: | |
stage: deploy | |
<<: *ssh | |
script: | |
- echo "Deploy to staging server" | |
- ./scripts/deploy_staging.sh | |
environment: | |
name: staging | |
url: http://staging.domain.com/ | |
only: | |
- master | |
deploy_prod: | |
stage: deploy | |
script: | |
- echo "Deploy to production server" | |
environment: | |
name: production | |
url: https://example.com | |
when: manual | |
only: | |
- master |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment