如何通过Visual Studio Online实现Cordova/PhoneGap构建与持续集成交付?
Setting Up Cordova/PhoneGap Builds & CI/CD in Visual Studio Online (VSO)
Great question! Even though VSO doesn’t include a dedicated Cordova/PhoneGap build template, you can easily set up custom builds and full CI/CD pipelines with a few manual steps. Let’s break this down step by step.
Part 1: Creating a Cordova Build Process in VSO
1. Prepare Your Build Agent
First, make sure your VSO build agent has all the required tools installed:
- Node.js: Cordova relies on Node, so install a stable LTS version.
- Cordova CLI: You can install it globally on the agent with
npm install -g cordova, or add it as a project dependency (we’ll cover that option later). - Platform-specific SDKs: For Android, install the Android SDK and set the
ANDROID_HOMEenvironment variable. For iOS, you’ll need a macOS agent with Xcode and proper developer certificates/provisioning profiles configured.
2. Create a Custom Build Definition
Start with an empty process since there’s no pre-built template:
- Go to your VSO project’s Builds tab and click New build definition.
- Select Empty process and click Apply.
3. Add Build Steps
Now add these steps to your build pipeline (in order):
- Get Sources: This is added by default—just confirm it’s pointing to your Cordova project repository.
- Install Node Dependencies: Add an npm task, set the command to
install, and point it to your project’spackage.jsonfile. This installs all project dependencies, including any local Cordova version if you’ve added it topackage.json. - Install Cordova CLI (if not global): If you didn’t install Cordova globally on the agent, add another npm task with the command
install -g cordova. Alternatively, usenpx cordovain subsequent commands to avoid global installs. - Add Target Platform: Add a Command Line task with the script:
Swapcordova platform add android --saveandroidforiosif building for iOS. - Build the Cordova Project: Add another Command Line task with the release build command:
Adjust the platform and add flags likecordova build --release android--debugif you need a debug build. - Publish Build Artifacts: Add a Publish Build Artifacts task to save your generated
.apk(Android) or.ipa(iOS) file. Set the artifact name to something likeCordovaAppand the path to the build output (e.g.,platforms/android/app/build/outputs/apk/release/for Android).
Part 2: Setting Up Continuous Integration & Delivery (CI/CD)
1. Enable Continuous Integration (CI)
Trigger builds automatically when code is pushed:
- In your build definition, go to the Triggers tab.
- Toggle on Continuous integration.
- Select the branches you want to trigger builds for (e.g.,
main,develop). - Save the build definition—now every code push to those branches will kick off a build.
2. Set Up Continuous Delivery (CD)
Deploy your built app automatically to testing or production environments:
- Go to your VSO project’s Releases tab and click New release definition.
- Select Empty process and click Apply.
- Link Build Artifacts: Add an artifact source pointing to your Cordova build definition.
- Add Deployment Steps:
- For Android: Use the Google Play task to deploy your
.apkto the Play Store (either internal testing, beta, or production). You’ll need to configure your Google Play service account credentials. - For iOS: Use the App Store Connect task to upload your
.ipato Apple’s App Store. Make sure your macOS agent has the correct certificates and provisioning profiles. - For testing environments: You can add tasks to distribute the app to testing tools like Firebase App Distribution or TestFlight.
- For Android: Use the Google Play task to deploy your
- Configure Deployment Triggers: Go to the Triggers tab of your release definition, toggle on Continuous deployment, and select the build branch you want to deploy automatically.
- Add Environments (Optional): Set up multiple environments (e.g., Test, Staging, Production) with approval gates if you want to manually sign off before deploying to production.
Key Tips & Troubleshooting
- Use variables to make your pipeline flexible: Define variables like
$(platform)or$(buildConfiguration)so you can easily switch between Android/iOS or debug/release builds without editing tasks. - For iOS builds: Ensure your macOS agent has the correct developer certificates installed, and use
cordova build ios --release --developmentTeam="YOUR_TEAM_ID"to specify your team ID. - Logs: If builds fail, check the build logs—VSO will show you exactly where a step broke (e.g., missing dependencies, SDK errors).
内容的提问来源于stack exchange,提问作者Doug Thompson - DouggyFresh
相关产品推荐
相关产品推荐

