简体   繁体   English

BitBucket管道:在Microsoft / dotnet:sdk映像上为Gulp添加NodeJS

[英]BitBucket Pipeline: Adding NodeJS for Gulp on the microsoft/dotnet:sdk image

I have an ASP.NET Core application. 我有一个ASP.NET Core应用程序。 It uses Gulp to convert Sass to CSS among other things. 它使用Gulp将Sass转换为CSS等。 I have already modified my .csproj file to have the Gulp task(s) run prior to a publish . 我已经修改了.csproj文件以在publish之前运行Gulp任务。 This is to ensure I have all my JS and CSS in place in the artifacts. 这是为了确保我在工件中放置了所有的JS和CSS。

<Target Name="PrePublishScript" BeforeTargets="PrepareForPublish">
  <Exec Command="npm install" />
  <Exec Command="gulp" />
</Target>

Here is my BitBucket Pipeline file: 这是我的BitBucket管道文件:

pipelines:
  branches:
    master:
      - step:
          name: Build
          image: microsoft/dotnet:sdk
          caches:
            - dotnetcore
          script:
            - export PROJECT_NAME=YeGods.sln
            - dotnet restore
            - dotnet build $PROJECT_NAME
            - dotnet publish $PROJECT_NAME --configuration Release --output dist --verbosity minimal
          artifacts:
            - YeGods.Web/dist/**
      - step:
          name: Deploy
          image: alpine:3.8
          script:
            - apk add --update openssh
            - ssh deploy@$SERVER_HOST 'bash -s' < $BITBUCKET_CLONE_DIR/pre-deploy-script.sh
            - scp -r YeGods.Web/dist/* deploy@$SERVER_HOST:$SERVER_PATH_TO_SITE_DIRECTORY
            - ssh deploy@$SERVER_HOST 'bash -s' < $BITBUCKET_CLONE_DIR/post-deploy-script.sh

This currently fails, because the npm install script-call fails as there is no NodeJS installed in the microsoft/dotnet:sdk image. 当前这失败,因为npm install脚本调用失败,因为在microsoft/dotnet:sdk映像中没有安装microsoft/dotnet:sdk I suspect all I have to do is get NodeJS installed in another script-call before this - export PROJECT_NAME=YeGods.sln . 我怀疑我要做的就是在此之前的另一个脚本调用中安装- export PROJECT_NAME=YeGods.sln So I added apt-get install nodejs but that didn't work. 所以我添加了apt-get install nodejs但是没有用。 It said it couldn't find nodejs . 它说找不到nodejs

If what line of thinking is right, what is the correct way of getting NodeJS installed onto the microsoft/dotnet:sdk image? 如果哪种思路正确,那么将NodeJS安装到microsoft/dotnet:sdk映像上的正确方法是什么?

I have an article on this exact issue , here is a summary: 我有一篇关于这个确切问题的文章 ,这里是摘要:

Solution 1: 解决方案1:

You need a docker image which also has both dotnet and node installed. 您需要一个同时安装了dotnet和node的docker镜像。 This can be easily done by extending the official dotnet images from microsoft by installing nodejs. 通过安装nodejs扩展Microsoft的官方dotnet映像,可以轻松完成此操作。 But you do need to know how to use docker. 但是您确实需要知道如何使用docker。

This can be a good opportunity to learn docker if you didn't already, there are plenty of tutorials available online. 如果您还没有学习docker,这可能是一个很好的机会,在线上有很多可用的教程。

I've did this before and uploaded my docker image to docker hub so that Pipelines can use it. 我之前已经做过,然后将docker映像上传到docker hub,以便Pipelines可以使用它。 It's publicly available on dockerhub, but you should really not use images of people who don't trust, but if you still want to test change image: microsoft/dotnet:sdk to image:peterekepeter/dotnet-node-sdk:latest 它在dockerhub上公开可用,但您实际上不应该使用不信任的人的图像,但是如果您仍然想测试更改image: microsoft/dotnet:sdkimage:peterekepeter/dotnet-node-sdk:latest

Here is a Dockerfile example how to take the dotnet image and install nodejs onto it: 这是一个Dockerfile示例,该示例如何获取dotnet映像并将nodejs安装到其上:

FROM microsoft/dotnet:2-sdk

# install nodejs for building & testing frontend

RUN curl -sL https://deb.nodesource.com/setup_10.x  | bash -
RUN apt-get -y install nodejs
RUN node -v

Solution 2: 解决方案2:

Separate the frontend build to a separate Pipelines step and run gulp tasks on a docker image which has nodejs installed. 将前端构建分离到单独的Pipelines步骤,并在安装了nodejs的docker映像上运行gulp任务。 If you have build dependencies between the two steps, persist them using artifacts. 如果您在两个步骤之间有构建依赖项,请使用构件将其持久化。

But for this to work you'll need to modify your build scripts, remove gulp command from csproj project, and run them as part of a pipelines script. 但是,要执行此操作,您将需要修改构建脚本,从csproj项目中删除gulp命令,并将其作为管道脚本的一部分运行。

Here is an example how to do this: 这是一个示例如何执行此操作:

pipelines:
  default:
    - step:
        name: .NET Core build & test
        image: microsoft/dotnet:2-sdk
        caches:
          - dotnetcore
        script:
          - dotnet restore
          - dotnet build --no-restore
          - dotnet test --no-build --no-restore
    - step:
        name: Frontend build & test
        image: node:6.9.4
        caches:
         - node
        script:
         - npm install
         - npm run build
         - npm run test

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM