Repository: goreleaser/goreleaser
Pull request #2191 (2021-04-26 14:10:55)

feat: add %pretrans %posttrans scriptlets
This PR adds in goreleaser nfpm configuration options for specifing %pretrans %posttrans scriptlets according to this PR: https://github.com/goreleaser/nfpm/pull/322

vercel[bot]: @cristianciutea is attempting to deploy a commit to the **Goreleaser** Team on [Vercel](https://vercel.com).

A member of the Team first needs to [authorize it](https://vercel.com/git/authorize?team=Goreleaser&type=github&job=%7B%22headInfo%22%3A%7B%22sha%22%3A%22e4af96119660af6477e857159adb576c8f2f0394%22%7D%2C%22id%22%3A%22QmbGHowh12Wr3AKwA6MXoRXeXKR7rTm2kwwTzgJb2scShb%22%2C%22org%22%3A%22goreleaser%22%2C%22prId%22%3A2191%2C%22repo%22%3A%22goreleaser%22%7D).



Patch:
diff --git a/internal/pipe/nfpm/nfpm.go b/internal/pipe/nfpm/nfpm.go
index cb71d25b..380906ba 100644
--- a/internal/pipe/nfpm/nfpm.go
+++ b/internal/pipe/nfpm/nfpm.go
@@ -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{
diff --git a/pkg/config/config.go b/pkg/config/config.go
index 231f1c87..f546e8d9 100644
--- a/pkg/config/config.go
+++ b/pkg/config/config.go
@@ -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.
diff --git a/www/docs/customization/nfpm.md b/www/docs/customization/nfpm.md
index 81888349..7916ce06 100644
--- a/www/docs/customization/nfpm.md
+++ b/www/docs/customization/nfpm.md
@@ -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


Test patch:
diff --git a/internal/pipe/nfpm/nfpm_test.go b/internal/pipe/nfpm/nfpm_test.go
index 93f5bacd..eaee27f8 100644
--- a/internal/pipe/nfpm/nfpm_test.go
+++ b/internal/pipe/nfpm/nfpm_test.go
@@ -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")