Powershell 脚本允许您利用 .NET 库并编写几乎与 .NET 代码本身一样强大的脚本。您可以执行许多强大的操作,例如调用外部 DLL、使用 System.IO、System.Net 等 .NET 命名空间、运行进程和拦截输出、调用 Web 服务等。可能性是无穷无尽的。在这里,我将向您展示一个 Powershell 脚本,它可以帮助您进行网站的日常部署。每天,我们都会对 Web 项目进行更改,这些项目需要部署到开发服务器上,有时会部署在 beta 服务器上,最后会部署到生产服务器上。使用此脚本,您可以在每次将网站上传到某个服务器时,自动执行您在部署包上一次又一次地执行的所有手动工作。在本地开发服务器上传、测试版发布和最终生产服务器发布期间,我们每天都在 Pageflakes 中使用此脚本。我们所做的就是运行脚本,转到服务器,然后在 web 文件夹中提取一个 zip 文件,仅此而已。新版本在两分钟内完成部署,完全无需任何人工操作,完全消除了部署过程中出现人为错误的可能性。
自动化部署
我已经编写了一个 Powershell 脚本为您执行以下操作:
* Maintains different configuration information for different deployments. For example, different connection strings for development servers and production servers (one or more production servers).
* Creates a deployment folder using the deployment date, time, and version so that you have a separate folder for each deployment and can keep track of things deployed on a day, e.g., 20061214-1.
* Copies only the change files and some predefined files to the deployment folder. So, you don't deploy the whole website every day.
* Copies the web.config and customizes the <appSettings>, <connectionString>, <assemblies> etc., as per the deployment configuration. For example, you can have different connection strings for different servers.
* Updates all JavaScript files with a version number so that in every deployment, a new file gets downloaded by client browsers.
* Updates default.aspx automatically with the modified script file name.
* Compresses all JavaScript files that gets deployed.
* Compresses all static HTML files using an Absolute HTML Optimizer.
* Creates a zip file which contains the deployment package.
* FTP the zip file to a target server.
部署脚本运行后,您只需在服务器上解压 zip 文件即可!
通过更改脚本末尾的 FTP 部分,您可以轻松地 FTP 修改后的文件,而不是仅复制 zip 文件。