Get value of package.json in gitLab CI YML

后端 未结 6 2146
小鲜肉
小鲜肉 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:26

    You might not be able to do this purely in the gitlab.yml unfortunately, you could create a shell script as follows and check this into your source control

    #!/bin/sh
    args=("$@")
    CI_REGISTRY_IMAGE=${args[0]}
    
    PACKAGE_VERSION=$(cat package.json \
    | grep version \
      | head -1 \
      | awk -F: '{ print $2 }' \
      | sed 's/[",]//g' \
      | tr -d '[[:space:]]')
    CONTAINER_RELEASE_IMAGE=$CI_REGISTRY_IMAGE\:$PACKAGE_VERSION
    cd /opt/core/bundle && docker build -t $CONTAINER_RELEASE_IMAGE .
    docker push $CONTAINER_RELEASE_IMAGE
    

    Then execute this script with the argument of $CI_REGISTRY_IMAGE in gitlab.yml

    # ...
    build:
      stage: build
      script:
        # ...
        - chmod +x script.sh
        - ./script.sh $CI_REGISTRY_IMAGE
    

    To the best of my knowledge this should work for you.

    Thank you to DarrenN and dbaba on Github for his package.json version extract shell function

提交回复
热议问题