16

I have an API developed in NodeJS and have successfully set up continuous integration via a .gitlab-ci.yml file. The next stage is to set up continuous deployment to Heroku if all tests pass on the master branch.

There are plenty of tutorials covering the deployment of Ruby and Python apps but nothing on NodeJS. Currently my .gitlab-ci.yml file looks like this:

image: node:latest

job1:
  script: "ls -l"

test:
  script: "npm install;npm test"

production:
  type: deploy
  script:
  - npm install
  - npm start
  - gem install dpl
  - dpl --provider=heroku --app=my-first-nodejs --api-key=XXXXXXXXXX
  only:
  - master

The Ruby and Python tutorials use the dpl tool to deploy but how can I start the NodeJS script on the server once deployed?

After adding the production section and pushing it the tests run and pass but the deploy stage gets stuck on pending. The console is blank. Has anyone set up a successful CD script for NodeJS?

1
  • 1
    Hi, could you share your current solution to this? Thanks :) Jan 31, 2017 at 15:45

3 Answers 3

11

you could use a much more simple YAML script where you can define the stages for the CI (to run test before production deploy) you can then use a different image at the Heroku deploy stage. So for a node app you define the default image as node:latest. Then for the production deployment using dpl you can use the ruby image.

image: node:latest

stages:
  - job1
  - test
  - production

job1:
  stage: job1
  script: "ls -l"

test:
  stage: test
  script: 
    - npm install
    - npm test
  artifacts:
    paths:
      - dist/

production:
  type: deploy
  stage: production
  image: ruby:latest
  script:
    - apt-get update -qy
    - apt-get install -y ruby-dev
    - gem install dpl
    - dpl --provider=heroku --app=my-first-nodejs --api-key=XXXXXXXXXX
  only:
    - master
3
  • xam, I have a question, the --api-key is attached to my account in heroku, so if I have an open sourced project in gitlab anyone could deploy to my heroku servers. How can I make sure that only MY gitlab.ci can deploy with my api-key? Jul 22, 2017 at 11:59
  • 2
    @CynthiaSanchez you can add variables that are only seen by you in gitlab. check settings of your project it's there somewhere
    – Ced
    Aug 19, 2017 at 8:30
  • currently (gitlab >= v10) its in > ci/cd > environments, but yes you create them as environment variables and reference them from the file with $ENV_NAME
    – xam
    Nov 5, 2017 at 11:36
10

Well, this won't be the longest answer ever, but this may help you.

Here's the content if it ever disappear:

  image: node:argon

  before_script:
    - apt-get -qq update
    - apt-get -qq install -y python2.7 python2.7-dev build-essential make gcc g++ libicu-dev
    - npm -g install npm --silent
    - "echo -e \"export default {CLIENT_ID: '$CLIENT_ID'}\" > app/scripts/settings.js"
    - npm set progress=false
    - npm install --silent

  stages:
    - test
    - build
    - clean_up

  run_tests:
    stage: test
    script:
      - npm test

  build_and_deploy_prod:
    stage: build
    script:
      - npm run build
      - mkdir  dist/build
      - tar czfC dist/build/latest.tar.gz dist/$CI_BUILD_REF_NAME/ .
      - tar czfC dist/build/$CI_BUILD_REF.tar.gz dist/$CI_BUILD_REF_NAME/ .
      - apt-get install -yqq ruby ruby-dev
      - gem install dpl
      - dpl --skip_cleanup --provider=s3 --region=eu-west-1 --access-key-id=$AWS_ACCESS_KEY --secret-access-key=$AWS_SECRET_KEY --bucket=$AWS_BUCKET --local-dir=dist/build/ --upload-dir=$CI_BUILD_REF_NAME
    artifacts:
      paths:
        - dist/$CI_BUILD_REF_NAME/
    only:
      - master
      - develop

  clean_up_job:
    stage: clean_up
    script:
      - rm -rf node_modules
      - rm -rf ~/.node-gyp
    when: on_failure
1

A well-explained article for Continuous Deployment of a NodeJS using GitLab :

LINK

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.