Get value of package.json in gitLab CI YML

后端 未结 6 2264
小鲜肉
小鲜肉 2021-02-13 03:25

I\'m using gitLab CI for my nodejs application. In my YML file I need to call a script to build a docker image. But instead of using latest I need to use the curren

6条回答
  •  天命终不由人
    2021-02-13 03:39

    You can use docker to get the package.json version. In this example we tag and push with "latest" and package.json version:

    .gitlab-ci.yml

    docker-build-master:
      # Official docker image.
      image: docker:latest
      stage: build
      services:
        - docker:dind
      before_script:
        - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
      script:
        - export VERSION=$(docker run --rm -v "$PWD":/app:ro -w /app node:slim node -p "require('./package.json').version")
        - docker build --pull -t "$CI_REGISTRY_IMAGE" .
        - docker push "$CI_REGISTRY_IMAGE:latest"
        - docker image tag "$CI_REGISTRY_IMAGE:latest" "$CI_REGISTRY_IMAGE:$VERSION"
        - docker push "$CI_REGISTRY_IMAGE:$VERSION"
      only:
        - master
    

提交回复
热议问题