Repository: goreleaser/goreleaser
Pull request #2308 (2021-06-20 14:48:14)

feat(nfpm): add support for APK pre/post upgrade scripts
Binds the NFPM APK support for pre/post upgrade scripts to goreleaser customization.

vercel[bot]: @rs 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%22f20d982d34ab5aade2d19f84e6d31470643e79e7%22%7D%2C%22id%22%3A%22QmVMVddJv9FZQpc7nCmtbqW47AZ34xo2nQ3xFV9xwU4ZUY%22%2C%22org%22%3A%22goreleaser%22%2C%22prId%22%3A2308%2C%22repo%22%3A%22goreleaser%22%7D).


vercel[bot]: This pull request is being automatically deployed with Vercel ([learn more](https://vercel.link/github-learn-more)).  
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: [https://vercel.com/goreleaser/goreleaser/E4wmr6N3NcFYDLvxm49AT6HhafxC](https://vercel.com/goreleaser/goreleaser/E4wmr6N3NcFYDLvxm49AT6HhafxC)  
✅ Preview: *In Progress*


codecov[bot]: # [Codecov](https://codecov.io/gh/goreleaser/goreleaser/pull/2308?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser) Report
> Merging [#2308](https://codecov.io/gh/goreleaser/goreleaser/pull/2308?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser) (fd6c8cc) into [master](https://codecov.io/gh/goreleaser/goreleaser/commit/e4373447181af89a10217ffb2134a64e610eec8d?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser) (e437344) will **increase** coverage by `0.01%`.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/goreleaser/goreleaser/pull/2308/graphs/tree.svg?width=650&height=150&src=pr&token=3bJvv6b7hN&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser)](https://codecov.io/gh/goreleaser/goreleaser/pull/2308?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser)

```diff
@@            Coverage Diff             @@
##           master    #2308      +/-   ##
==========================================
+ Coverage   82.69%   82.71%   +0.01%     
==========================================
  Files          79       79              
  Lines        4305     4309       +4     
==========================================
+ Hits         3560     3564       +4     
  Misses        605      605              
  Partials      140      140              
```


| [Impacted Files](https://codecov.io/gh/goreleaser/goreleaser/pull/2308?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser) | Coverage Δ | |
|---|---|---|
| [pkg/config/config.go](https://codecov.io/gh/goreleaser/goreleaser/pull/2308/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser#diff-cGtnL2NvbmZpZy9jb25maWcuZ28=) | `93.75% <ø> (ø)` | |
| [internal/pipe/nfpm/nfpm.go](https://codecov.io/gh/goreleaser/goreleaser/pull/2308/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser#diff-aW50ZXJuYWwvcGlwZS9uZnBtL25mcG0uZ28=) | `95.96% <100.00%> (+0.06%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/goreleaser/goreleaser/pull/2308?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/goreleaser/goreleaser/pull/2308?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser). Last update [e437344...fd6c8cc](https://codecov.io/gh/goreleaser/goreleaser/pull/2308?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser).

caarlos0: Thanks for the PR, LGTM! 🙏

Patch:
diff --git a/internal/pipe/nfpm/nfpm.go b/internal/pipe/nfpm/nfpm.go
index 9cd793f9..6abe7549 100644
--- a/internal/pipe/nfpm/nfpm.go
+++ b/internal/pipe/nfpm/nfpm.go
@@ -306,6 +306,10 @@ func create(ctx *context.Context, fpm config.NFPM, format, arch string, binaries
 					},
 					KeyName: overridden.APK.Signature.KeyName,
 				},
+				Scripts: nfpm.APKScripts{
+					PreUpgrade:  overridden.APK.Scripts.PreUpgrade,
+					PostUpgrade: overridden.APK.Scripts.PostUpgrade,
+				},
 			},
 		},
 	}
diff --git a/pkg/config/config.go b/pkg/config/config.go
index 374ed619..5c80237a 100644
--- a/pkg/config/config.go
+++ b/pkg/config/config.go
@@ -389,6 +389,11 @@ type NFPMDeb struct {
 	Signature       NFPMDebSignature `yaml:"signature,omitempty"`
 }
 
+type NFPMAPKScripts struct {
+	PreUpgrade  string `yaml:"preupgrade,omitempty"`
+	PostUpgrade string `yaml:"postupgrade,omitempty"`
+}
+
 // NFPMAPKSignature contains config for signing apk packages created by nfpm.
 type NFPMAPKSignature struct {
 	// RSA private key in PEM format
@@ -400,6 +405,7 @@ type NFPMAPKSignature struct {
 
 // NFPMAPK is custom config only available on apk packages.
 type NFPMAPK struct {
+	Scripts   NFPMAPKScripts   `yaml:"scripts,omitempty"`
 	Signature NFPMAPKSignature `yaml:"signature,omitempty"`
 }
 
diff --git a/www/docs/customization/nfpm.md b/www/docs/customization/nfpm.md
index 60cbcb97..b597069d 100644
--- a/www/docs/customization/nfpm.md
+++ b/www/docs/customization/nfpm.md
@@ -318,6 +318,13 @@ nfpms:
         type: origin
 
     apk:
+      # APK specific scripts.
+      scripts:
+        # The preupgrade script runs before APK upgrade.
+        preupgrade: ./scripts/preupgrade.sh
+        # The postupgrade script runs after APK.
+        postupgrade: ./scripts/postupgrade.sh
+
       # The package is signed if a key_file is set
       signature:
         # RSA private key in the PEM format. The passphrase is taken


Test patch:
diff --git a/internal/pipe/nfpm/nfpm_test.go b/internal/pipe/nfpm/nfpm_test.go
index 21b6fe97..7a02ac6d 100644
--- a/internal/pipe/nfpm/nfpm_test.go
+++ b/internal/pipe/nfpm/nfpm_test.go
@@ -808,6 +808,90 @@ func TestAPKSpecificConfig(t *testing.T) {
 	})
 }
 
+func TestAPKSpecificScriptsConfig(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())
+	scripts := config.NFPMAPKScripts{
+		PreUpgrade:  "/does/not/exist_preupgrade.sh",
+		PostUpgrade: "/does/not/exist_postupgrade.sh",
+	}
+	ctx := context.New(config.Project{
+		ProjectName: "mybin",
+		Dist:        dist,
+		NFPMs: []config.NFPM{
+			{
+				ID:         "someid",
+				Maintainer: "me@me",
+				Builds:     []string{"default"},
+				Formats:    []string{"apk"},
+				NFPMOverridables: config.NFPMOverridables{
+					PackageName: "foo",
+					Contents: []*files.Content{
+						{
+							Source:      "testdata/testfile.txt",
+							Destination: "/usr/share/testfile.txt",
+						},
+					},
+					APK: config.NFPMAPK{
+						Scripts: scripts,
+					},
+				},
+			},
+		},
+	})
+	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("PreUpgrade script file does not exist", func(t *testing.T) {
+		ctx.Config.NFPMs[0].APK.Scripts = scripts
+		ctx.Config.NFPMs[0].APK.Scripts.PostUpgrade = "testdata/testfile.txt"
+
+		require.Contains(
+			t,
+			Pipe{}.Run(ctx).Error(),
+			`nfpm failed: stat /does/not/exist_preupgrade.sh: no such file or directory`,
+		)
+	})
+
+	t.Run("PostUpgrade script file does not exist", func(t *testing.T) {
+		ctx.Config.NFPMs[0].APK.Scripts = scripts
+		ctx.Config.NFPMs[0].APK.Scripts.PreUpgrade = "testdata/testfile.txt"
+
+		require.Contains(
+			t,
+			Pipe{}.Run(ctx).Error(),
+			`nfpm failed: stat /does/not/exist_postupgrade.sh: no such file or directory`,
+		)
+	})
+
+	t.Run("preupgrade and postupgrade scriptlets set", func(t *testing.T) {
+		ctx.Config.NFPMs[0].APK.Scripts.PreUpgrade = "testdata/testfile.txt"
+		ctx.Config.NFPMs[0].APK.Scripts.PostUpgrade = "testdata/testfile.txt"
+
+		require.NoError(t, Pipe{}.Run(ctx))
+	})
+}
+
 func TestSeveralNFPMsWithTheSameID(t *testing.T) {
 	ctx := &context.Context{
 		Config: config.Project{