Skip to content

Commit eab6e5a

Browse files
ligifjl
authored andcommitted
build: specify the key to use when invoking gpg:sign-and-deploy-file (#16696)
1 parent c4a4613 commit eab6e5a

2 files changed

Lines changed: 25 additions & 8 deletions

File tree

‎build/ci.go‎

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -755,14 +755,18 @@ func doAndroidArchive(cmdline []string) {
755755
os.Rename(archive, meta.Package+".aar")
756756
if *signer != "" && *deploy != "" {
757757
// Import the signing key into the local GPG instance
758-
if b64key := os.Getenv(*signer); b64key != "" {
759-
key, err := base64.StdEncoding.DecodeString(b64key)
760-
if err != nil {
761-
log.Fatalf("invalid base64 %s", *signer)
762-
}
763-
gpg := exec.Command("gpg", "--import")
764-
gpg.Stdin = bytes.NewReader(key)
765-
build.MustRun(gpg)
758+
b64key := os.Getenv(*signer)
759+
key, err := base64.StdEncoding.DecodeString(b64key)
760+
if err != nil {
761+
log.Fatalf("invalid base64 %s", *signer)
762+
}
763+
gpg := exec.Command("gpg", "--import")
764+
gpg.Stdin = bytes.NewReader(key)
765+
build.MustRun(gpg)
766+
767+
keyID, err := build.PGPKeyID(string(key))
768+
if err != nil {
769+
log.Fatal(err)
766770
}
767771
// Upload the artifacts to Sonatype and/or Maven Central
768772
repo := *deploy + "/service/local/staging/deploy/maven2"
@@ -771,6 +775,7 @@ func doAndroidArchive(cmdline []string) {
771775
}
772776
build.MustRunCommand("mvn", "gpg:sign-and-deploy-file", "-e", "-X",
773777
"-settings=build/mvn.settings", "-Durl="+repo, "-DrepositoryId=ossrh",
778+
"-Dgpg.keyname="+keyID,
774779
"-DpomFile="+meta.Package+".pom", "-Dfile="+meta.Package+".aar")
775780
}
776781
}

‎internal/build/pgp.go‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,15 @@ func PGPSignFile(input string, output string, pgpkey string) error {
5757
// Generate the signature and return
5858
return openpgp.ArmoredDetachSign(out, keys[0], in, nil)
5959
}
60+
61+
// PGPKeyID parses an armored key and returns the key ID.
62+
func PGPKeyID(pgpkey string) (string, error) {
63+
keys, err := openpgp.ReadArmoredKeyRing(bytes.NewBufferString(pgpkey))
64+
if err != nil {
65+
return "", err
66+
}
67+
if len(keys) != 1 {
68+
return "", fmt.Errorf("key count mismatch: have %d, want %d", len(keys), 1)
69+
}
70+
return keys[0].PrimaryKey.KeyIdString(), nil
71+
}

0 commit comments

Comments
 (0)