# Gitlab CI CD Examples ## Requirements 1. SSH Key Pair (Public & Private Key) 2. Add(authorize) Public key to `~/.ssh/authorized_keys` to the server ## Variables Edit CI/CD variables in `Settings -> CI/CD -> Variables` | Variable | Value | Description | | -------- | -------- | --- | | SSH_PRIV_KEY| *content of **private key*** | | SERVER_USER | ubuntu | user of ssh to access deploy server | SERVER_HOST | 192.168.18.14 | IP or hostname to access deploy server | SERVER_PORT | 22 | SSH Port of deploy server | APP_PATH | /home/ubuntu/app | path to application on deploy server ## .gitlab-ci.yml ### Nodejs ```yaml stages: - install_deps # intall deps to be used on lint & test stages - lint # run lint checker - test # run automated tests - deploy # deploy app # Handy pointer to be reused in some jobs .node_modules_cache: &node_modules_cache cache: key: $CI_COMMIT_REF_SLUG-$CI_PROJECT_DIR paths: - node_modules/ - package-lock.json # if lock file is not added to git policy: pull-push # Install npm dependencies npm_install: stage: install_deps image: node:14 <<: *node_modules_cache script: - npm install only: changes: - package.json - package-lock.json # Run lint checker lint_syntax: stage: lint image: node:14 <<: *node_modules_cache script: - npm run lint # Run automated tests test_all: stage: test image: node:14 <<: *node_modules_cache script: - npm run test # Lets deploy to production if branch is master deploy_to_production: stage: deploy image: kroniak/ssh-client before_script: - "which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )" - eval $(ssh-agent -s) - ssh-add <(echo "$SSH_PRIV_KEY") - mkdir -p ~/.ssh - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\tPort $SERVER_PORT\n\n" > ~/.ssh/config' script: - ssh $SERVER_USER@$SERVER_HOST "cd $APP_PATH && git pull && npm intall --only=prod && pm2 reload all" # Modify these commands to suit your app only: - master ``` ### Vuejs ```yaml stages: - install_deps - build - deploy npm_install: stage: install_deps image: node:14 cache: key: $CI_COMMIT_REF_SLUG-$CI_PROJECT_DIR paths: - .npm/ - node_modules/ script: - npm ci --cache .npm --prefer-offline # cache npm packages to speed up installation ref: https://docs.gitlab.com/ee/ci/caching/#cache-nodejs-dependencies only: changes: - package-lock.json build_app: stage: build image: node:14 cache: key: $CI_COMMIT_REF_SLUG-$CI_PROJECT_DIR paths: - node_modules/ - .npm/ policy: pull-push before_script: - '[[ -d "./node_modules" ]] || npm ci --cache .npm --prefer-offline' # Check if node_modules is exists or intall its dependencies script: - npm run build artifacts: expire_in: 1 hour paths: - dist only: - master deploy_to_production: stage: deploy image: kroniak/ssh-client before_script: - "which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )" - eval $(ssh-agent -s) - ssh-add <(echo "$SSH_PRIV_KEY") - mkdir -p ~/.ssh - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\tPort $SERVER_PORT\n\n" > ~/.ssh/config' - apk add rsync script: - rsync -rvz --progress dist/ $SERVER_USER@$SERVER_HOST:$APP_PATH only: - master ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up