1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-09-16 09:26:52 +02:00

feat: add %pretrans %posttrans scriptlets (#2191)

* tmp changed nfpm dep to fork

* added rpm pretrans and posttrans scriptlets

* cleanup gomod

* docs: added pretrans and posttrans examples in nfpm.md

* added test for rpm pretrans and posttrans scriptlets
This commit is contained in:
Cristian Ciutea
2021-04-28 16:01:03 +03:00
committed by GitHub
parent 860b4a8f81
commit a86a9956de
4 changed files with 92 additions and 1 deletions

View File

@@ -266,6 +266,10 @@ func create(ctx *context.Context, fpm config.NFPM, format, arch string, binaries
KeyPassphrase: getPassphraseFromEnv(ctx, "RPM", fpm.ID),
},
},
Scripts: nfpm.RPMScripts{
PreTrans: overridden.RPM.Scripts.PreTrans,
PostTrans: overridden.RPM.Scripts.PostTrans,
},
},
APK: nfpm.APK{
Signature: nfpm.APKSignature{

View File

@@ -387,6 +387,7 @@ func TestDefaultSet(t *testing.T) {
require.Equal(t, "/bin", ctx.Config.NFPMs[0].Bindir)
require.Equal(t, "foo", ctx.Config.NFPMs[0].FileNameTemplate)
require.Equal(t, []string{"foo"}, ctx.Config.NFPMs[0].Builds)
require.Equal(t, config.NFPMRPMScripts{}, ctx.Config.NFPMs[0].RPM.Scripts)
}
func TestOverrides(t *testing.T) {
@@ -564,6 +565,78 @@ func TestRPMSpecificConfig(t *testing.T) {
})
}
func TestRPMSpecificScriptsConfig(t *testing.T) {
folder := t.TempDir()
dist := filepath.Join(folder, "dist")
require.NoError(t, os.Mkdir(dist, 0o755))
require.NoError(t, os.Mkdir(filepath.Join(dist, "mybin"), 0o755))
binPath := filepath.Join(dist, "mybin", "mybin")
f, err := os.Create(binPath)
require.NoError(t, err)
require.NoError(t, f.Close())
ctx := context.New(config.Project{
ProjectName: "mybin",
Dist: dist,
NFPMs: []config.NFPM{
{
ID: "someid",
Builds: []string{"default"},
Formats: []string{"rpm"},
NFPMOverridables: config.NFPMOverridables{
PackageName: "foo",
RPM: config.NFPMRPM{
Scripts: config.NFPMRPMScripts{
PreTrans: "/does/not/exist_pretrans.sh",
PostTrans: "/does/not/exist_posttrans.sh",
},
},
},
},
},
})
ctx.Version = "1.0.0"
ctx.Git = context.GitInfo{CurrentTag: "v1.0.0"}
for _, goos := range []string{"linux", "darwin"} {
for _, goarch := range []string{"amd64", "386"} {
ctx.Artifacts.Add(&artifact.Artifact{
Name: "mybin",
Path: binPath,
Goarch: goarch,
Goos: goos,
Type: artifact.Binary,
Extra: map[string]interface{}{
"ID": "default",
},
})
}
}
t.Run("PreTrans script file does not exist", func(t *testing.T) {
require.Contains(
t,
Pipe{}.Run(ctx).Error(),
`nfpm failed: open /does/not/exist_pretrans.sh: no such file or directory`,
)
})
t.Run("PostTrans script file does not exist", func(t *testing.T) {
ctx.Config.NFPMs[0].RPM.Scripts.PreTrans = "testdata/testfile.txt"
require.Contains(
t,
Pipe{}.Run(ctx).Error(),
`nfpm failed: open /does/not/exist_posttrans.sh: no such file or directory`,
)
})
t.Run("pretrans and posttrans scriptlets set", func(t *testing.T) {
ctx.Config.NFPMs[0].RPM.Scripts.PreTrans = "testdata/testfile.txt"
ctx.Config.NFPMs[0].RPM.Scripts.PostTrans = "testdata/testfile.txt"
require.NoError(t, Pipe{}.Run(ctx))
})
}
func TestAPKSpecificConfig(t *testing.T) {
folder := t.TempDir()
dist := filepath.Join(folder, "dist")

View File

@@ -332,6 +332,12 @@ type NFPMRPMSignature struct {
KeyPassphrase string `yaml:"-"` // populated from environment variable
}
// NFPMRPMScripts represents scripts only available on RPM packages.
type NFPMRPMScripts struct {
PreTrans string `yaml:"pretrans,omitempty"`
PostTrans string `yaml:"posttrans,omitempty"`
}
// NFPMRPM is custom configs that are only available on RPM packages.
type NFPMRPM struct {
Summary string `yaml:"summary,omitempty"`
@@ -340,6 +346,7 @@ type NFPMRPM struct {
ConfigNoReplaceFiles map[string]string `yaml:"config_noreplace_files,omitempty"` // deprecated: use contents instead
GhostFiles []string `yaml:"ghost_files,omitempty"` // deprecated: use contents instead
Signature NFPMRPMSignature `yaml:"signature,omitempty"`
Scripts NFPMRPMScripts `yaml:"scripts,omitempty"`
}
// NFPMDebScripts is scripts only available on deb packages.

View File

@@ -224,8 +224,15 @@ nfpms:
scripts:
preinstall: "scripts/preinstall-rpm.sh"
# Custon configuration applied only to the RPM packager.
# Custom configuration applied only to the RPM packager.
rpm:
# RPM specific scripts.
scripts:
# The pretrans script runs before all RPM package transactions / stages.
pretrans: ./scripts/pretrans.sh
# The posttrans script runs after all RPM package transactions / stages.
posttrans: ./scripts/posttrans.sh
# The package summary.
# Defaults to the first line of the description.
summary: Explicit Summary for Sample Package