deploy.go (2865B)
1 // Copyright 2019 The Hugo Authors. All rights reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // http://www.apache.org/licenses/LICENSE-2.0 7 // 8 // Unless required by applicable law or agreed to in writing, software 9 // distributed under the License is distributed on an "AS IS" BASIS, 10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 //go:build !nodeploy 15 // +build !nodeploy 16 17 package commands 18 19 import ( 20 "context" 21 22 "github.com/gohugoio/hugo/deploy" 23 "github.com/spf13/cobra" 24 ) 25 26 var _ cmder = (*deployCmd)(nil) 27 28 // deployCmd supports deploying sites to Cloud providers. 29 type deployCmd struct { 30 *baseBuilderCmd 31 32 invalidateCDN bool 33 maxDeletes int 34 } 35 36 // TODO: In addition to the "deploy" command, consider adding a "--deploy" 37 // flag for the default command; this would build the site and then deploy it. 38 // It's not obvious how to do this; would all of the deploy-specific flags 39 // have to exist at the top level as well? 40 41 // TODO: The output files change every time "hugo" is executed, it looks 42 // like because of map order randomization. This means that you can 43 // run "hugo && hugo deploy" again and again and upload new stuff every time. Is 44 // this intended? 45 46 func (b *commandsBuilder) newDeployCmd() *deployCmd { 47 cc := &deployCmd{} 48 49 cmd := &cobra.Command{ 50 Use: "deploy", 51 Short: "Deploy your site to a Cloud provider.", 52 Long: `Deploy your site to a Cloud provider. 53 54 See https://gohugo.io/hosting-and-deployment/hugo-deploy/ for detailed 55 documentation. 56 `, 57 58 RunE: func(cmd *cobra.Command, args []string) error { 59 cfgInit := func(c *commandeer) error { 60 c.Set("invalidateCDN", cc.invalidateCDN) 61 c.Set("maxDeletes", cc.maxDeletes) 62 return nil 63 } 64 comm, err := initializeConfig(true, true, false, &cc.hugoBuilderCommon, cc, cfgInit) 65 if err != nil { 66 return err 67 } 68 deployer, err := deploy.New(comm.Cfg, comm.hugo().PathSpec.PublishFs) 69 if err != nil { 70 return err 71 } 72 return deployer.Deploy(context.Background()) 73 }, 74 } 75 76 cmd.Flags().String("target", "", "target deployment from deployments section in config file; defaults to the first one") 77 cmd.Flags().Bool("confirm", false, "ask for confirmation before making changes to the target") 78 cmd.Flags().Bool("dryRun", false, "dry run") 79 cmd.Flags().Bool("force", false, "force upload of all files") 80 cmd.Flags().BoolVar(&cc.invalidateCDN, "invalidateCDN", true, "invalidate the CDN cache listed in the deployment target") 81 cmd.Flags().IntVar(&cc.maxDeletes, "maxDeletes", 256, "maximum # of files to delete, or -1 to disable") 82 83 cc.baseBuilderCmd = b.newBuilderBasicCmd(cmd) 84 85 return cc 86 }