コード例 #1
0
        /// <summary>
        /// Deploy a single build.
        /// </summary>
        /// <param name="build"></param>
        protected void ProcessMessage(utils.AppVeyor.Build build)
        {
            Message command = new Message(build.message);

            try
            {
                switch (command.command)
                {
                case "publish":
                case "republish":
                    this.RunPublishCommand(command, build);
                    break;

                default:
                    this.Logger.LogWarning(false, "AppVeyor commit message command not found: '{0}'", Newtonsoft.Json.JsonConvert.SerializeObject(command));
                    break;
                }
            }
            catch (Exception ex)
            {
                var newEx = new Exception("Error executing command: " + Newtonsoft.Json.JsonConvert.SerializeObject(command), ex);
                this.Logger.LogException(newEx);
            }
        }
コード例 #2
0
        /// <summary>
        /// Execute the publish/republish commands.
        /// </summary>
        /// <param name="command"></param>
        /// <param name="build"></param>
        protected void RunPublishCommand(Message command, utils.AppVeyor.Build build)
        {
            double lifetime = double.Parse(command.arguments[0]);

            // Limit the lifetime to 7 days
            if (lifetime > 168)
            {
                lifetime = 168;
            }

            // To prevent colision between projects, add a small project hash
            var projectHash = UtilsEncryption.GetShortHash(build.project.name, 2);

            // Use the branch name to generate a unique application name
            var appId = $"{Application.AutoDeployApplicationIdPrefix}{projectHash}_{build.branch}".ToLower();

            // Use an in-code template to deploy
            string template = string.Format(
                @"id: '{0}'
expires: {4}
tags: '{6}'
autodeploy: true
downloader:
  type: 'appveyor'
  project: '{1}'
  username: '******'
  apitoken: '{5}'
  branch: '{3}'
",
                appId,
                this.Settings.project,
                this.Settings.username,
                build.branch,
                lifetime,
                this.Settings.apitoken,

// Tag to indicate that this is an autodeployment.
                "autodeploy");

            // Update the deployment template
            string templateFilePath = Path.Combine(this.app.GetGlobalSettings().applicationTemplateDir, appId + ".yml");

            File.WriteAllText(templateFilePath, template);

            if ("republish".Equals(command.command, StringComparison.CurrentCultureIgnoreCase))
            {
                bool doRemove = true;

                // To prevent infinite redeployment loops
                // we need to make sure that we do not remove
                // the application if currently installed version (if any)
                var application = this.app.GetInstalledApp(appId);

                var buildVersion    = new Version("0.0.0");
                var deployedVersion = new Version("0.0.0");

                if (application != null)
                {
                    var deployer = this.app.GetDeployer(application);

                    Version.TryParse(build.version, out buildVersion);
                    Version.TryParse(deployer.DeploymentActive?.artifact?.id, out deployedVersion);

                    doRemove = buildVersion > deployedVersion;
                }

                if (doRemove)
                {
                    this.Logger.LogInfo(false, "Application republish from version '{0}' to '{1}'", deployedVersion, buildVersion);
                    this.app.RemoveAppById(appId, true);
                }
            }

            // Deploy from template...
            this.app.RedeployInstalledApplication(true, appId, false);

            // Get rid of the template, this is not useful anymore.
            File.Delete(templateFilePath);
        }