2020-04-28 02:42:44 +02:00
package cmd
import (
2021-01-13 19:21:04 +02:00
"runtime"
2020-04-28 02:42:44 +02:00
"time"
"github.com/caarlos0/ctrlc"
2022-06-22 02:11:15 +02:00
"github.com/caarlos0/log"
2021-09-18 15:21:29 +02:00
"github.com/goreleaser/goreleaser/internal/middleware/errhandler"
"github.com/goreleaser/goreleaser/internal/middleware/logging"
"github.com/goreleaser/goreleaser/internal/middleware/skip"
2021-06-05 04:09:12 +02:00
"github.com/goreleaser/goreleaser/internal/pipe/git"
2020-04-28 02:42:44 +02:00
"github.com/goreleaser/goreleaser/internal/pipeline"
"github.com/goreleaser/goreleaser/pkg/context"
2022-05-07 01:38:50 +02:00
"github.com/spf13/cobra"
2020-04-28 02:42:44 +02:00
)
type releaseCmd struct {
2022-05-07 01:38:50 +02:00
cmd * cobra . Command
2020-04-28 02:42:44 +02:00
opts releaseOpts
}
type releaseOpts struct {
2021-12-12 05:21:51 +02:00
config string
releaseNotesFile string
releaseNotesTmpl string
releaseHeaderFile string
releaseHeaderTmpl string
releaseFooterFile string
releaseFooterTmpl string
autoSnapshot bool
snapshot bool
skipPublish bool
skipSign bool
skipValidate bool
skipAnnounce bool
skipSBOMCataloging bool
2022-06-23 02:39:20 +02:00
skipDocker bool
2023-01-17 03:34:49 +02:00
skipKo bool
2022-06-23 02:56:53 +02:00
skipBefore bool
2021-12-12 05:21:51 +02:00
rmDist bool
deprecated bool
parallelism int
timeout time . Duration
2020-04-28 02:42:44 +02:00
}
func newReleaseCmd ( ) * releaseCmd {
2021-04-22 15:45:36 +02:00
root := & releaseCmd { }
2020-05-15 16:19:20 +02:00
// nolint: dupl
2022-05-07 01:38:50 +02:00
cmd := & cobra . Command {
2020-04-28 02:42:44 +02:00
Use : "release" ,
Aliases : [ ] string { "r" } ,
Short : "Releases the current project" ,
SilenceUsage : true ,
SilenceErrors : true ,
2022-05-07 01:38:50 +02:00
Args : cobra . NoArgs ,
2022-06-22 06:48:11 +02:00
RunE : timedRunE ( "release" , func ( cmd * cobra . Command , args [ ] string ) error {
2020-04-28 02:42:44 +02:00
ctx , err := releaseProject ( root . opts )
if err != nil {
2022-06-22 06:48:11 +02:00
return err
2020-04-28 02:42:44 +02:00
}
2022-06-22 06:48:11 +02:00
deprecateWarn ( ctx )
2020-04-28 02:42:44 +02:00
return nil
2022-06-22 06:48:11 +02:00
} ) ,
2020-04-28 02:42:44 +02:00
}
cmd . Flags ( ) . StringVarP ( & root . opts . config , "config" , "f" , "" , "Load configuration from file" )
2022-01-30 16:28:00 +02:00
cmd . Flags ( ) . StringVar ( & root . opts . releaseNotesFile , "release-notes" , "" , "Load custom release notes from a markdown file (will skip GoReleaser changelog generation)" )
2021-05-22 02:07:47 +02:00
cmd . Flags ( ) . StringVar ( & root . opts . releaseHeaderFile , "release-header" , "" , "Load custom release notes header from a markdown file" )
cmd . Flags ( ) . StringVar ( & root . opts . releaseFooterFile , "release-footer" , "" , "Load custom release notes footer from a markdown file" )
cmd . Flags ( ) . StringVar ( & root . opts . releaseNotesTmpl , "release-notes-tmpl" , "" , "Load custom release notes from a templated markdown file (overrides --release-notes)" )
cmd . Flags ( ) . StringVar ( & root . opts . releaseHeaderTmpl , "release-header-tmpl" , "" , "Load custom release notes header from a templated markdown file (overrides --release-header)" )
cmd . Flags ( ) . StringVar ( & root . opts . releaseFooterTmpl , "release-footer-tmpl" , "" , "Load custom release notes footer from a templated markdown file (overrides --release-footer)" )
2022-12-29 15:41:59 +02:00
cmd . Flags ( ) . BoolVar ( & root . opts . autoSnapshot , "auto-snapshot" , false , "Automatically sets --snapshot if the repository is dirty" )
2021-05-25 05:45:59 +02:00
cmd . Flags ( ) . BoolVar ( & root . opts . snapshot , "snapshot" , false , "Generate an unversioned snapshot release, skipping all validations and without publishing any artifacts (implies --skip-publish, --skip-announce and --skip-validate)" )
2022-06-22 06:48:11 +02:00
cmd . Flags ( ) . BoolVar ( & root . opts . skipPublish , "skip-publish" , false , "Skips publishing artifacts (implies --skip-announce)" )
2021-05-25 05:45:59 +02:00
cmd . Flags ( ) . BoolVar ( & root . opts . skipAnnounce , "skip-announce" , false , "Skips announcing releases (implies --skip-validate)" )
2021-09-23 05:39:28 +02:00
cmd . Flags ( ) . BoolVar ( & root . opts . skipSign , "skip-sign" , false , "Skips signing artifacts" )
2021-12-12 05:21:51 +02:00
cmd . Flags ( ) . BoolVar ( & root . opts . skipSBOMCataloging , "skip-sbom" , false , "Skips cataloging artifacts" )
2022-06-23 02:39:20 +02:00
cmd . Flags ( ) . BoolVar ( & root . opts . skipDocker , "skip-docker" , false , "Skips Docker Images/Manifests builds" )
2023-01-17 03:34:49 +02:00
cmd . Flags ( ) . BoolVar ( & root . opts . skipKo , "skip-ko" , false , "Skips Ko builds" )
2022-06-23 02:56:53 +02:00
cmd . Flags ( ) . BoolVar ( & root . opts . skipBefore , "skip-before" , false , "Skips global before hooks" )
2021-09-23 05:39:28 +02:00
cmd . Flags ( ) . BoolVar ( & root . opts . skipValidate , "skip-validate" , false , "Skips git checks" )
cmd . Flags ( ) . BoolVar ( & root . opts . rmDist , "rm-dist" , false , "Removes the dist folder" )
2021-04-22 15:45:36 +02:00
cmd . Flags ( ) . IntVarP ( & root . opts . parallelism , "parallelism" , "p" , 0 , "Amount tasks to run concurrently (default: number of CPUs)" )
2020-04-28 02:42:44 +02:00
cmd . Flags ( ) . DurationVar ( & root . opts . timeout , "timeout" , 30 * time . Minute , "Timeout to the entire release process" )
cmd . Flags ( ) . BoolVar ( & root . opts . deprecated , "deprecated" , false , "Force print the deprecation message - tests only" )
_ = cmd . Flags ( ) . MarkHidden ( "deprecated" )
2022-10-06 11:24:13 +02:00
_ = cmd . Flags ( ) . SetAnnotation ( "config" , cobra . BashCompFilenameExt , [ ] string { "yaml" , "yml" } )
2020-04-28 02:42:44 +02:00
root . cmd = cmd
return root
}
func releaseProject ( options releaseOpts ) ( * context . Context , error ) {
cfg , err := loadConfig ( options . config )
if err != nil {
return nil , err
}
ctx , cancel := context . NewWithTimeout ( cfg , options . timeout )
defer cancel ( )
2020-05-15 16:19:20 +02:00
setupReleaseContext ( ctx , options )
2020-04-28 02:42:44 +02:00
return ctx , ctrlc . Default . Run ( ctx , func ( ) error {
for _ , pipe := range pipeline . Pipeline {
2021-09-18 15:21:29 +02:00
if err := skip . Maybe (
pipe ,
logging . Log (
pipe . String ( ) ,
errhandler . Handle ( pipe . Run ) ,
) ,
2020-04-28 02:42:44 +02:00
) ( ctx ) ; err != nil {
return err
}
}
return nil
} )
}
2022-09-25 23:19:53 +02:00
func setupReleaseContext ( ctx * context . Context , options releaseOpts ) {
2021-04-22 15:45:36 +02:00
ctx . Parallelism = runtime . NumCPU ( )
if options . parallelism > 0 {
ctx . Parallelism = options . parallelism
}
2020-04-28 02:42:44 +02:00
log . Debugf ( "parallelism: %v" , ctx . Parallelism )
2021-05-22 02:07:47 +02:00
ctx . ReleaseNotesFile = options . releaseNotesFile
ctx . ReleaseNotesTmpl = options . releaseNotesTmpl
ctx . ReleaseHeaderFile = options . releaseHeaderFile
ctx . ReleaseHeaderTmpl = options . releaseHeaderTmpl
ctx . ReleaseFooterFile = options . releaseFooterFile
ctx . ReleaseFooterTmpl = options . releaseFooterTmpl
2020-04-28 02:42:44 +02:00
ctx . Snapshot = options . snapshot
2022-04-12 13:35:19 +02:00
if options . autoSnapshot && git . CheckDirty ( ctx ) != nil {
2022-12-29 15:41:59 +02:00
log . Info ( "git repository is dirty and --auto-snapshot is set, implying --snapshot" )
2021-06-05 04:09:12 +02:00
ctx . Snapshot = true
}
2020-04-28 02:42:44 +02:00
ctx . SkipPublish = ctx . Snapshot || options . skipPublish
2021-05-25 05:45:59 +02:00
ctx . SkipAnnounce = ctx . Snapshot || options . skipPublish || options . skipAnnounce
2020-04-28 02:42:44 +02:00
ctx . SkipValidate = ctx . Snapshot || options . skipValidate
ctx . SkipSign = options . skipSign
2021-12-12 05:21:51 +02:00
ctx . SkipSBOMCataloging = options . skipSBOMCataloging
2022-06-23 02:39:20 +02:00
ctx . SkipDocker = options . skipDocker
2023-01-17 03:34:49 +02:00
ctx . SkipKo = options . skipKo
2022-06-23 02:56:53 +02:00
ctx . SkipBefore = options . skipBefore
2020-04-28 02:42:44 +02:00
ctx . RmDist = options . rmDist
// test only
ctx . Deprecated = options . deprecated
}