2020-05-03 14:46:05 -04:00
|
|
|
import * as core from '@actions/core';
|
2020-05-04 14:59:11 -04:00
|
|
|
import * as git from './git';
|
2020-05-03 15:33:19 -04:00
|
|
|
import * as gpg from './gpg';
|
|
|
|
import * as openpgp from './openpgp';
|
2020-05-03 14:46:05 -04:00
|
|
|
import * as stateHelper from './state-helper';
|
2020-05-04 14:06:27 -04:00
|
|
|
import os from 'os';
|
2020-05-03 14:46:05 -04:00
|
|
|
|
|
|
|
async function run(): Promise<void> {
|
|
|
|
try {
|
2020-05-04 14:06:27 -04:00
|
|
|
if (os.platform() == 'win32') {
|
|
|
|
core.setFailed('Windows platform not supported');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-03 14:46:05 -04:00
|
|
|
if (!process.env.SIGNING_KEY) {
|
|
|
|
core.setFailed('Signing key required');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-03 15:33:19 -04:00
|
|
|
core.info('📣 GnuPG info');
|
|
|
|
const version = await gpg.getVersion();
|
2020-05-04 10:17:14 -04:00
|
|
|
const dirs = await gpg.getDirs();
|
2020-05-04 10:40:21 -04:00
|
|
|
core.info(`Version : ${version.gnupg} (libgcrypt ${version.libgcrypt})`);
|
|
|
|
core.info(`Libdir : ${dirs.libdir}`);
|
|
|
|
core.info(`Libexecdir : ${dirs.libexecdir}`);
|
|
|
|
core.info(`Datadir : ${dirs.datadir}`);
|
|
|
|
core.info(`Homedir : ${dirs.homedir}`);
|
2020-05-03 14:46:05 -04:00
|
|
|
|
2020-05-04 14:09:52 -04:00
|
|
|
core.info('🔮 Checking signing key');
|
2020-05-03 15:33:19 -04:00
|
|
|
const privateKey = await openpgp.readPrivateKey(process.env.SIGNING_KEY);
|
2020-05-04 10:17:14 -04:00
|
|
|
core.debug(`Fingerprint : ${privateKey.fingerprint}`);
|
|
|
|
core.debug(`KeyID : ${privateKey.keyID}`);
|
|
|
|
core.debug(`UserID : ${privateKey.userID}`);
|
|
|
|
core.debug(`CreationTime : ${privateKey.creationTime}`);
|
2020-05-03 14:46:05 -04:00
|
|
|
|
2020-05-04 14:09:52 -04:00
|
|
|
core.info('🔑 Importing secret key');
|
2020-05-04 10:17:14 -04:00
|
|
|
await gpg.importKey(process.env.SIGNING_KEY).then(stdout => {
|
|
|
|
core.debug(stdout);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (process.env.PASSPHRASE) {
|
2020-05-04 14:09:52 -04:00
|
|
|
core.info('⚙️ Configuring GnuPG agent');
|
2020-05-04 10:17:14 -04:00
|
|
|
await gpg.configureAgent(gpg.agentConfig);
|
|
|
|
|
2020-05-04 14:09:52 -04:00
|
|
|
core.info('📌 Getting keygrip');
|
2020-05-04 10:17:14 -04:00
|
|
|
const keygrip = await gpg.getKeygrip(privateKey.fingerprint);
|
|
|
|
core.debug(`${keygrip}`);
|
|
|
|
|
2020-05-04 14:09:52 -04:00
|
|
|
core.info('🔓 Preset passphrase');
|
2020-05-04 10:17:14 -04:00
|
|
|
await gpg.presetPassphrase(keygrip, process.env.PASSPHRASE).then(stdout => {
|
|
|
|
core.debug(stdout);
|
|
|
|
});
|
|
|
|
}
|
2020-05-04 14:59:11 -04:00
|
|
|
|
|
|
|
if (/true/i.test(core.getInput('git_gpgsign'))) {
|
|
|
|
core.info('💎 Enable signing for this Git repository');
|
|
|
|
await git.enableCommitGpgsign();
|
|
|
|
await git.setUserSigningkey(privateKey.keyID);
|
|
|
|
}
|
2020-05-03 14:46:05 -04:00
|
|
|
} catch (error) {
|
|
|
|
core.setFailed(error.message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function cleanup(): Promise<void> {
|
2020-05-03 15:22:08 -04:00
|
|
|
if (!process.env.SIGNING_KEY) {
|
2020-05-04 10:17:14 -04:00
|
|
|
core.debug('Signing key is not defined. Skipping cleanup.');
|
2020-05-03 14:46:05 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
try {
|
2020-05-04 14:09:52 -04:00
|
|
|
core.info('🚿 Removing keys');
|
2020-05-03 15:33:19 -04:00
|
|
|
const privateKey = await openpgp.readPrivateKey(process.env.SIGNING_KEY);
|
|
|
|
await gpg.deleteKey(privateKey.fingerprint);
|
2020-05-03 14:46:05 -04:00
|
|
|
} catch (error) {
|
|
|
|
core.warning(error.message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Main
|
|
|
|
if (!stateHelper.IsPost) {
|
|
|
|
run();
|
|
|
|
}
|
|
|
|
// Post
|
|
|
|
else {
|
|
|
|
cleanup();
|
|
|
|
}
|