Skip to main content

NPM Deploy

The npm-deploy jobs are template jobs for deploying Node.js/npm applications. These jobs provide a foundation that can be extended with specific deployment logic.

Overview

The npm-deploy jobs include:

  • .npm-deploy: Base template job for npm deployments
  • .main_post_deploy: Post-deployment notification job for main branch merges

These are template jobs that serve as placeholders and can be extended with specific deployment logic for your use case.

Variables

The following variables are used:

VariableDescriptionDefaultRequired
GIT_STRATEGYGit clone strategynoneNo

Variable Details

  • GIT_STRATEGY: Set to none to skip Git repository cloning, useful when you only need artifacts from previous jobs

Job Types

.npm-deploy

Base template job for npm deployments. This is a placeholder job that can be extended with specific deployment logic.

Usage

.npm-deploy:  
variables:
GIT_STRATEGY: none
script:
- echo "deploy job phase"

Purpose

This job serves as a template that can be extended to implement actual deployment logic. The GIT_STRATEGY: none setting means the job won't clone the repository, making it suitable for jobs that only work with artifacts from previous stages.

Extending the Job

You can extend this job to add your deployment logic:

deploy-npm-app:
extends: .npm-deploy
stage: deploy
script:
- echo "deploy job phase"
- # Add your deployment commands here
- # e.g., rsync, scp, kubectl, helm, etc.
dependencies:
- build
only:
- develop

.main_post_deploy

Post-deployment notification job that runs after merging to the main branch.

Usage

.main_post_deploy:
variables:
GIT_STRATEGY: none
script:
- echo "Merged successfully in main branch; don't forget to align develop too"

Purpose

This job provides a reminder notification after code is merged to the main branch, reminding developers to align the develop branch as well.

Extending the Job

You can extend this job to add post-deployment actions:

post-deploy-notification:
extends: .main_post_deploy
stage: deploy
script:
- echo "Merged successfully in main branch; don't forget to align develop too"
- # Add notification logic (Slack, email, etc.)
- # Add any post-deployment cleanup or tasks
only:
- main

Example Deployment Pattern

The commented-out example in the source shows a pattern for deploying npm applications using Helm:

# Example (commented in source):
deploy-application-dev:
image: dtzar/helm-kubectl
script:
- if [ -f ./develop_storage_user_id ]; then
echo "file develop_storage_user_id exists" &&
export DEV_USER_ID=$(cat develop_storage_user_id | xargs);
else
echo "file develop_storage_user_id does not exist";
fi
- CI_COMMIT_REF_NAME=${CI_COMMIT_REF_NAME/\//_} &&
kubectl config use-context welance-develop &&
mv .env.dev helm-chart/.env &&
rm .env.staging .env.production
- if [ -n "$DEV_USER_ID" ]; then
helm upgrade --install --create-namespace -n $PROJECT_ID-develop web ./helm-chart/ \
-f ./helm-chart/values.yaml \
--set-string deployment.annotations."app\.gitlab\.com/app"=${CI_PROJECT_PATH_SLUG} \
--set-string deployment.annotations."app\.gitlab\.com/env"=${CI_ENVIRONMENT_SLUG} \
--set image.tag=$CI_COMMIT_REF_NAME-$CI_PIPELINE_ID \
--set deployment.securityContext.runAsUser=$DEV_USER_ID \
--set deployment.securityContext.runAsGroup=$DEV_USER_ID \
--set deployment.securityContext.fsGroup=$DEV_USER_ID \
--set ingresses.web.hosts[0].host="${PROJECT_ID}.dev.welance.com" \
--set ingresses.web.tls[0].hosts[0]="${PROJECT_ID}.dev.welance.com" \
--set image.repository=$CI_REGISTRY_IMAGE
else
helm upgrade --install --create-namespace -n $PROJECT_ID-develop web ./helm-chart/ \
-f ./helm-chart/values.yaml \
--set-string deployment.annotations."app\.gitlab\.com/app"=${CI_PROJECT_PATH_SLUG} \
--set-string deployment.annotations."app\.gitlab\.com/env"=${CI_ENVIRONMENT_SLUG} \
--set image.tag=$CI_COMMIT_REF_NAME-$CI_PIPELINE_ID \
--set deployment.securityContext.runAsUser=1001 \
--set deployment.securityContext.runAsGroup=1001 \
--set deployment.securityContext.fsGroup=1001 \
--set ingresses.web.hosts[0].host="${PROJECT_ID}.dev.welance.com" \
--set ingresses.web.tls[0].hosts[0]="${PROJECT_ID}.dev.welance.com" \
--set image.repository=$CI_REGISTRY_IMAGE
fi
variables:
DOCKER_HOST: tcp://docker:2376
DOCKER_TLS_CERTDIR: "/certs"
GIT_STRATEGY: none

This example shows:

  • Conditional storage user ID loading from file
  • Kubernetes context switching
  • Environment file management
  • Helm deployment with conditional security context
  • GitLab deployment annotations

Complete Pipeline Example

stages:
- build
- deploy

# Build npm application
build:
stage: build
script:
- npm install
- npm run build
artifacts:
paths:
- dist/
- .env.dev

.npm-deploy:
variables:
GIT_STRATEGY: none
script:
- echo "deploy job phase"

# Extend with actual deployment logic
deploy-npm-app:
extends: .npm-deploy
stage: deploy
script:
- echo "deploy job phase"
- # Add your deployment commands
- # Example: rsync dist/ to server, deploy to Kubernetes, etc.
dependencies:
- build
only:
- develop

.main_post_deploy:
variables:
GIT_STRATEGY: none
script:
- echo "Merged successfully in main branch; don't forget to align develop too"

post-deploy-main:
extends: .main_post_deploy
stage: deploy
only:
- main

Usage Recommendations

For Simple Deployments

Extend .npm-deploy with your deployment method:

deploy-simple:
extends: .npm-deploy
script:
- echo "deploy job phase"
- rsync -avz dist/ user@server:/var/www/app/

For Kubernetes Deployments

Use the pattern from the commented example or extend .deploy-node-k8s from the k8s-deploy jobs.

For GitLab Auto DevOps

The jobs can be integrated with GitLab's Auto DevOps features using the deployment annotations shown in the commented example.

Notes

  • These are template jobs meant to be extended with specific deployment logic
  • GIT_STRATEGY: none is used to skip repository cloning when only artifacts are needed
  • The commented example shows a Helm-based deployment pattern that can be adapted
  • For Kubernetes deployments, consider using .deploy-node-k8s from the k8s-deploy jobs instead
  • The .main_post_deploy job is useful for post-deployment notifications and reminders
  • Storage user IDs can be loaded from files or use default values (1001)
  • GitLab deployment annotations (app.gitlab.com/app and app.gitlab.com/env) enable integration with GitLab's deployment tracking