如何从jenkins插件取回android签名证书

时间:2019-05-16 11:44:11

标签: android jenkins signing

我有用于Android应用程序的jenkins项目。使用jenkins插件进行APK签名(请参见下图):

jenkins sertificate settings

如何从jenkins取回.p12证书文件?

1 个答案:

答案 0 :(得分:1)

首先,确定正确的Jenkins主目录。转到“管理詹金斯”,然后单击“系统信息”。找到一个值JENKINS_HOME

接下来,通过SSH登录到Jenkins,然后转到该目录。名为credentials.xml的文件将包含您的机密。

找到您的证书ID(例如在URL中):

Credentials ID in URL

最后,启动您的Jenkins实例的脚本控制台(可通过/script URL或通过“ Manage Jenkins”->“脚本控制台”获得)并执行以下脚本:

import com.cloudbees.plugins.credentials.*
import hudson.security.*
import java.security.*
import javax.xml.bind.DatatypeConverter

def creds =  CredentialsMatchers
                .firstOrNull(
                        CredentialsProvider
                            .lookupCredentials(
                                Credentials.class,
                                Jenkins.getActiveInstance(),
                                ACL.SYSTEM,
                                Collections.emptyList()
                            ),
                        CredentialsMatchers.withId("9X9X99XX-XX9X-9X99-9X9X-9X9X9999XXX9")
                )

// This will print a decrypted password
def password = creds.password
println password

// This will print all the available aliases
creds.keyStore.aliases().each { println it }

// Imagine, the alias you need is myapp.
// Get JVM representation of you certificate and key
def cert = creds.keyStore.getEntry("myapp", new KeyStore.PasswordProtection(password.toCharArray())).certificate
def privKey = creds.keyStore.getEntry("myapp", new KeyStore.PasswordProtection(password.toCharArray())).privateKey

// Format certificate and key
certpem = "-----BEGIN CERTIFICATE-----\n" +
        DatatypeConverter.printBase64Binary(cert.encoded) +
        "\n-----END CERTIFICATE-----\n";
keypem  = "-----BEGIN RSA PRIVATE KEY-----\n" +
        DatatypeConverter.printBase64Binary(privKey.encoded) +
        "\n-----END RSA PRIVATE KEY-----\n";

// Print them
println certpem
println keypem

顺便说一句,您可以在Jenins主页的credentials.xml中按ID查找编码的凭据。看起来应该像这样:

<com.cloudbees.plugins.credentials.impl.CertificateCredentialsImpl>
    <scope>GLOBAL</scope>
    <id>9X9X99XX-XX9X-9X99-9X9X-9X9X9999XXX9</id>
    <description>App signing certificate</description>
    <keyStoreSource class="com.cloudbees.plugins.credentials.impl.CertificateCredentialsImpl$UploadedKeyStoreSource">
        <uploadedKeystoreBytes>{ENCRYPTED_CERTIFICATE}</uploadedKeystoreBytes>
    </keyStoreSource>
    <password>{ENCRYPTED_PASSWORD}</password>
</com.cloudbees.plugins.credentials.impl.CertificateCredentialsImpl>