简体   繁体   中英

Jenkins: Creating pipeline to read dockerfile

Creating a Pipeline to read a Dockerfile and create container for this app be run.

Jenkinsfile:

pipeline {
  agent any
  tools {nodejs "node" }
  stages {
    stage('Cloning Git') {
      steps {
        git url: 'https://github.com/user/private-repo.git',
        credentialsId: 'Git-2'
      }
    }
    stage('Build Container Image') {
      steps {
        agent{
          dockerfile {
              filename '$workspace/Dockerfile',
              label 'node'              
          }
        }
      }
    }
    stage('Build') {
       steps {
         sh 'npm install'
       }
    }
    stage('Test') {
      steps {
        sh "pwd"
      }
    }    
  }
}

Error:

WorkflowScript: 15: Expected a step @ line 15, column 15. filename '$workspace/Dockerfile',

I was reading this article Using Docker with Pipeline . I new in Jenkins and in my mind, I was thinking in this steps:

  • Clone Repo
  • Read Dockerfile
  • Run Container abd Bind Workspace Jenkins to Container Workspace
  • Install NPM Packages
  • Run Application

But as I don't know how to deal with Jenkins I'm kind lost here.

Dockerfile:

FROM node

RUN apt-get update && apt-get upgrade -y \
    && apt-get clean

RUN mkdir /app
WORKDIR /app

COPY package*.json /app/

RUN npm install

COPY src /app/src

EXPOSE 3000

CMD [ "npm", "start" ]

Anyone can please help me, or suggest an article?

Jenkins works in my thoughts

在此处输入图片说明

Using docker and Jenkins can be fairly complicated. The first question you usually need to ask is how complicated of a Jenkins build do you need to do? If you are just trying to run a container that houses your build, you can usually just use a declarative pipeline.

However, I find that anything more than that, usually requires a scripting pipeline. With a scripting pipeline, the logic is usually a little more complicated, but it is closer to native groovy.

https://www.jenkins.io/doc/book/pipeline/docker/#building-containers

A good overview of how to make this work is located https://tutorials.releaseworksacademy.com/learn/building-your-first-docker-image-with-jenkins-2-guide-for-developers

As a note, you should not be calling npm install from your jenkinsfile. This should only be executed from within the docker container during the build process.

  1. Checkout source code. You can either configure the jenkins server to know what your upstream repository is, or you can follow the steps here to add a URL and credentials.

  2. Build your dockerfile

  3. Push/tag your docker image to a registry as shown in the above tutorial and here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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