Get value of package.json in gitLab CI YML

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

    You can use YAML anchor feature to set a variable that you can use in a job scripts.

    .get_version: &get_version
      - "export SERVICE_VERSION=$(cat package.json | grep version | head -1 | awk -F: '{ print $2 }' | sed 's/[\",]//g' | tr -d '[[:space:]]')"
      - "Current version is: $SERVICE_VERSION"
    

    and then somewhere in script part:

    .gitlab-ci.yml:

      variables:
        CONTAINER_NAME: myService
      script:
        - echo "My own script"
        - *get_version
        - docker build --iidfile imageid.txt --cache-from $CONTAINER_NAME:$SERVICE_VERSION -t $CONTAINER_NAME:$SERVICE_VERSION .
        - docker push $CONTAINER_NAME:$SERVICE_VERSION
    

提交回复
热议问题