Skip to content

Instantly share code, notes, and snippets.

@Tymek
Last active March 1, 2023 10:08
Show Gist options
  • Save Tymek/a31ec577b20cd41e880f43d4b47fa097 to your computer and use it in GitHub Desktop.
Save Tymek/a31ec577b20cd41e880f43d4b47fa097 to your computer and use it in GitHub Desktop.
Static files server build for NodeJS→GitLab CI→Docker→Nginx pipeline
image: alpine
cache:
paths:
- node_modules/
before_script:
- DIST_DIR=$([ "$DIST_DIR" ] || echo "./dist")
stages:
- build
- deploy
build:
image: node:lts
stage: build
script:
- npm install
- npm run build
artifacts:
paths:
- "$DIST_DIR"
deploy:
stage: deploy
only:
- master
dependencies:
- build
cache: {}
script:
- REGISTRY_URL=$([ "$REGISTRY_URL" ] || echo "registry.gitlab.com")
- REGISTRY_USER=$([ "$REGISTRY_USER" ] || echo "gitlab-ci-token")
- REGISTRY_PROJECT_URL="$REGISTRY_URL/$CI_PROJECT_PATH"
- docker login -u gitlab-ci-token -p $REGISTRY_TOKEN $REGISTRY_URL
- docker build --build-arg DIST_DIR=${DIST_DIR} -t $REGISTRY_PROJECT_URL .
- docker push $REGISTRY_PROJECT_URL

Define following CI variables:

  • REGISTRY_TOKEN- registry password
  • REGISTRY_USER (optional, default: gitlab-ci-token)
  • REGISTRY_URL (optional, default: registry.gitlab.com)
  • DIST_DIR (optional, default: ./dist) - npm build output directory
FROM nginx:alpine
ARG DIST_DIR=./dist
WORKDIR /usr/share/nginx/html
RUN rm -rf ./*
COPY ${DIST_DIR}/* ./
EXPOSE 80
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment