Search code examples
angularjenkinscontinuous-integrationcontinuous-deploymenttomcat9

Jenkins deploy Angular 2+ Project To Tomcat


I'm new to Jenkins and I'm trying to Build and deploy My angular project to Tomcat using Jenkins. I cannot find any Jenkins Job Configuration.

I found some useful Video

but this will run npm install and npm run build and creates dist folder with files

I'm stuck in deployment.

enter image description here


Solution

  • If your intention is to automate the deploy step

    Jenkins is designed to work as a centralised CD server which can deliver artefact to repository or a server. In your case, I assume you are running the Jenkins on same machine where you are doing development. That is technical feasible setup. But, that is not how it is intended to be used. For you a single sh or batch script would work instead of a whole Jenkins server if your intention is to put reusable commands in one place. In that case, yes you would just copy the content to the webapp or similar directory.

    If your intention is to make use of the continuous delivery Jenkins is a right tool. The "ideal" flow is dev machine => SCM => CI => STAGING/PRD

    All four of them run on different server.

    This is how you can configure and use Jenkins.

    • Create a Jenkinsfile and add it to the repository and commit it.
    • Configure Jenkins by adding a new Task -> Muti-branch Pipeline -> Select SCM as your git repository -> Select the branches you want to build -> Scan repository now

    Begin with the below Jenkinsfile

    pipeline {
    agent any
    stages {
      stage("Build Project") {
          script {
                  bat "npm install"
                  bat "npm run build"
                }
       }
      stage("deploy"){
         script {
                 bat "<Your deploy command>"
              }
        }
    }
    post {
      always{
         deleteDir()   
      }
    }
    }
    

    The advantage of using such a Jenkins file is, you can put it in scm and track the changes. If you have a New Jenkins, you can simply reconfigure the Job and all your pipeline stages can be visualised.

    For Linux, replace bat with sh.

    For more information please check the official guide https://jenkins.io/doc/book/pipeline/syntax/#pipeline-syntax