How can I automate hosting Angular app on IIS server using Jenkins Pipeline?

huangapple go评论52阅读模式
英文:

How can I automate hosting Angular app on IIS server using Jenkins Pipeline?

问题

将Angular应用程序部署到虚拟机,并通过Jenkins流水线将其部署到IIS。

我已成功在虚拟机中构建和部署它,但需要将其托管在IIS服务器上,我需要通过Jenkins来自动化托管在IIS服务器上的部分。你有什么方法或者我需要在批处理命令或Jenkins文件中添加什么内容?

英文:

Deploy angular application in a virtual machine and deploy it to IIS with help of Jenkins pipeline

I have successfully build and deploy it in VM but hosting in IIS server I need to do it through Jenkins basically want automate the hosting in IIS server part also so how can I do it any idea what I need to add in batch command or Jenkins file

答案1

得分: 1

设置您的Jenkins管道:

  1. 打开Jenkins并创建一个新的管道任务。
    配置必要的设置,例如存储库URL和凭据。
    在管道配置中,选择"Pipeline script"选项。
  2. 编写Jenkins管道脚本:在Jenkins管道脚本中,您需要定义部署过程的各个阶段。
pipeline {
  agent any

  stages {
    stage('构建') {
      steps {
        // 添加构建Angular应用程序的命令
        sh 'npm install'
        sh 'ng build --prod'
      }
    }

    stage('部署到IIS') {
      steps {
        // 添加将构建成果部署到IIS的命令
        bat 'xcopy /s /y "dist\\*" "C:\\inetpub\\wwwroot"'
      }
    }

    stage('创建IIS应用程序') {
      steps {
        script {
          // 从环境变量获取分支名称
          def branchName = env.BRANCH_NAME

          // 基于分支名称生成应用程序名称
          def appName = branchName.replaceAll("/", "-").toLowerCase()

          // 设置IIS应用程序路径
          def appPath = "Default Web Site/${appName}"

          // 设置应用程序池名称
          def appPoolName = "MyAppPool"

          // 创建IIS应用程序
          bat "appcmd add app /site.name:\"Default Web Site\" /path:\"/${appName}\" /physicalPath:\"C:\\inetpub\\wwwroot\${appName}\""

          // 为新创建的应用程序设置应用程序池
          bat "appcmd set app /app.name:\"Default Web Site/${appName}\" /applicationPool:\"${appPoolName}\""
        }
      }
    }
  }
}

您可以编辑构建阶段,不仅通过npm install和ng build --prod来安装node_module并构建,还可以添加一些其他自定义配置或命令,例如运行测试,甚至添加新的测试阶段等。

如果您的IIS服务器的Web根目录位于不同的目录中,请调整Deploy to IIS阶段中xcopy命令的路径。

将管道脚本保存在Jenkins作业配置中。
运行Jenkins作业,它将执行定义的阶段来构建您的Angular应用程序并将其部署到IIS服务器。

通常情况下,我在Angular项目中有一个Jenkins文件。因此,如果我对主分支/开发分支进行了一些提交,它将自动从jenkinsfile运行该管道任务。

英文:

Set up your Jenkins pipeline:

  1. Open Jenkins and create a new pipeline job.
    Configure the necessary settings such as the repository URL and credentials.
    In the pipeline configuration, select the "Pipeline script" option.
  2. Write the Jenkins pipeline script: In the Jenkins pipeline script,
    you'll need to define the stages of your deployment process.
pipeline {
  agent any

  stages {
    stage('Build') {
      steps {
        // Add the commands to build your Angular application
        sh 'npm install'
        sh 'ng build --prod'
      }
    }

    stage('Deploy to IIS') {
      steps {
        // Add the commands to deploy the build artifacts to IIS
        bat 'xcopy /s /y "dist\\*" "C:\\inetpub\\wwwroot"'
      }
    }

    stage('Create IIS Application') {
      steps {
        script {
          // Get the branch name from the environment variable
          def branchName = env.BRANCH_NAME

          // Generate the application name based on the branch name
          def appName = branchName.replaceAll("/", "-").toLowerCase()

          // Set the IIS application path
          def appPath = "Default Web Site/${appName}"

          // Set the application pool name
          def appPoolName = "MyAppPool"

          // Create the IIS application
          bat "appcmd add app /site.name:\"Default Web Site\" /path:\"/${appName}\" /physicalPath:\"C:\\inetpub\\wwwroot\${appName}\""

          // Set the application pool for the newly created application
          bat "appcmd set app /app.name:\"Default Web Site/${appName}\" /applicationPool:\"${appPoolName}\""
        }
      }
    }
  }
}

You can edit build stage to not only instal node_module via npm install and build via ng build --prod. But add some other custom configuration or commands like run test or even add new stage for tests etc...

Adjust the path in the xcopy command in the Deploy to IIS stage if your IIS server's webroot is located in a different directory.

Save the pipeline script in your Jenkins job configuration.
Run the Jenkins job, and it will execute the defined stages to build your Angular application and deploy it to the IIS server.

Mostly I have one of the jenkins file in Angular project. So if I made some of the commit to master/dev branche it will automaticaly run that pipeline job from jenskinsfile.

huangapple
  • 本文由 发表于 2023年5月25日 04:22:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76327160.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定