Skip to content

Commit

Permalink
Add support for detecting values from project properties, and global …
Browse files Browse the repository at this point in the history
…prefix properties.
  • Loading branch information
LexManos committed Nov 4, 2023
1 parent d71878e commit 0b61648
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 11 deletions.
20 changes: 15 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,23 @@ You can also configure the task itself to specify any of the information set in
### Github Secrets
A large motivation for this was wanting to use Github Actions and still be able to sign my built files. Github does not allow you to have files as [secrets](https://docs.github.com/en/actions/security-guides/encrypted-secrets) just strings and the workarounds I found involved committing a encrypted form of your keystore to your repo and then decrypting it during an Action. Instead I decided to allow you to specify the keystore file as a base64 encoded string which can be used as a Secret.

You can either manually configure the information by pulling the secrets yourself, or I added a simple helper `jarSigner.fromEnvironmentVariables()` which does the following:
You can either manually configure the information by pulling the secrets yourself, or I added a simple helper `jarSigner.autoDetect()` which which search the following locations in order:

if (prefix != null) {
project.findProperty(prefix + '.' + prop)
System.getenv(prefix + '.' + prop)
}
project.findProperty(prop)
System.getenv(prop)
`prefix` defaults to `project.name` you can override by calling `jarSigner.autoDetect('prefix')`

For the following properties:

jarSigner {
alias = System.env('SIGN_KEY_ALIAS')
keyPass = System.env('SIGN_KEY_PASSWORD')
storePass = System.env('SIGN_KEYSTORE_PASSWORD')
keyStoreData = System.env('SIGN_KEYSTORE_DATA')
alias = 'SIGN_KEY_ALIAS'
keyPass = 'SIGN_KEY_PASSWORD'
storePass = 'SIGN_KEYSTORE_PASSWORD'
keyStoreData = 'SIGN_KEYSTORE_DATA'
}

### Conclusion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,22 @@ public SignTask sign(Zip task, Closure<SignTask> cfg) {
}

public void fromEnvironmentVariables() {
set("SIGN_KEY_ALIAS", this::setAlias);
set("SIGN_KEY_PASSWORD", this::setKeyPass);
set("SIGN_KEYSTORE_PASSWORD", this::setStorePass);
set("SIGN_KEYSTORE_DATA", this::setKeyStoreData);
fromEnvironmentVariables(project.getName());
}

public void fromEnvironmentVariables(String prefix) {
autoDetect(prefix);
}

public void autoDetect() {
autoDetect(project.getName());
}

public void autoDetect(String prefix) {
set(prefix, "SIGN_KEY_ALIAS", this::setAlias);
set(prefix, "SIGN_KEY_PASSWORD", this::setKeyPass);
set(prefix, "SIGN_KEYSTORE_PASSWORD", this::setStorePass);
set(prefix, "SIGN_KEYSTORE_DATA", this::setKeyStoreData);
}

public void setAlias(String value) {
Expand Down Expand Up @@ -77,8 +89,19 @@ void fill(SignTask task) {
task.setKeyStoreFile(this.keyStoreFile);
}

private void set(String key, Consumer<String> prop) {
String data = System.getenv(key);
private void set(String prefix, String key, Consumer<String> prop) {
String data = null;
if (prefix != null) {
data = (String)project.findProperty(prefix + '.' + key);
if (data == null)
data = System.getenv(prefix + '.' + key);
}

if (data == null)
data = (String)project.findProperty(key);
if (data == null)
data = System.getenv(key);

if (data != null)
prop.accept(data);
}
Expand Down

0 comments on commit 0b61648

Please sign in to comment.