如何在VSTS中仅当工件变更时触发夜间部署并避免重复发布
Perfect question—let’s walk through exactly how to set this up in Azure DevOps (previously VSTS) to hit all your requirements: nightly scheduled deployments that only run when artifacts have changed since the last release, plus blocking duplicate deployments of the same version no matter if they’re triggered manually or on schedule.
First, let’s make sure that nightly schedule doesn’t waste time running when nothing has changed:
- Open your release definition, go to the Triggers tab (the schedule icon you mentioned in the artifacts area links directly here).
- Edit your existing nightly scheduled trigger.
- Look for the Artifacts section in the trigger settings, then check the box labeled "Only trigger a release if the artifact has changed since the last successful release".
- This tells Azure DevOps to skip the nightly run entirely if none of your linked artifacts (builds, packages, etc.) have new versions since the last time the release completed successfully.
Even with the trigger locked down, someone might accidentally manually trigger a release using an already-deployed artifact version. Let’s add a safety net here:
- Go to your target deployment stage (like "Production") and click the Pre-deployment conditions icon (the little lightning bolt next to the stage name).
- Scroll down to the Deployment conditions section, then enable the option "Skip deployment if the same artifact version was already successfully deployed to this stage".
- This will automatically abort the deployment if the exact artifact version you’re trying to push has already been successfully deployed to this stage before—whether it’s a manual trigger or a scheduled one.
If you need more precision (like checking only specific artifacts instead of all), you can use custom variables and conditions:
- First, create a variable in your release’s Variables tab to track the last successful artifact version (you can populate this with a post-deployment script that saves the current version to a variable group, or use the built-in
Release.Artifacts.{YourArtifactAlias}.SourceVersionvariable). - Then, in the pre-deployment conditions, add a custom condition like:
ne(variables['Release.Artifacts.MyAppArtifact.SourceVersion'], variables['LastSuccessfulDeployedVersion'])- Swap
MyAppArtifactwith your artifact’s alias, andLastSuccessfulDeployedVersionwith your tracking variable. This ensures deployment only proceeds if the current artifact version is different from the last successful one.
- Swap
To make sure everything works:
- Try manually triggering a release with an artifact version that’s already been deployed—you’ll see the stage skip automatically.
- Let the nightly schedule run when no artifacts have changed—no release will be created.
- Push a new build or package to your artifact source, and the nightly schedule should kick off a release as expected.
内容的提问来源于stack exchange,提问作者Liero

