Skip to content
Snippets Groups Projects
Select Git revision
  • b60cc2c8902366fe7df31722eb66d3aff45e24d0
  • main default protected
  • renovate/main-ghcr.io-renovatebot-base-image-11.x
  • refactor/pin-new-value
  • revert-37021-fix/36927-maven-tags
  • fix/user-agent
  • feat/37531-npm-install-twice
  • feat/37517-base64-private-key
  • next
  • feat/gnupg
  • fix/markdown/linking
  • fix/36615b-branch-reuse-no-cache
  • chore/punycode
  • feat/36219--git-x509-signing
  • feat/structured-logger
  • hotfix/39.264.1
  • feat/skip-dangling
  • gh-readonly-queue/next/pr-36034-7a061c4ca1024a19e2c295d773d9642625d1c2be
  • hotfix/39.238.3
  • refactor/gitlab-auto-approve
  • feat/template-strings
  • 41.76.0
  • 41.75.0
  • 41.74.5
  • 41.74.4
  • 41.74.3
  • 41.74.2
  • 41.74.1
  • 41.74.0
  • 41.73.4
  • 41.73.3
  • 41.73.2
  • 41.73.1
  • 41.73.0
  • 41.72.1
  • 41.72.0
  • 42.0.0-next.2
  • 42.0.0-next.1
  • 41.71.1
  • 41.71.0
  • 41.70.3
41 results

private-key.ts

Blame
  • private-key.ts 1.27 KiB
    import os from 'os';
    import path from 'path';
    import fs from 'fs-extra';
    import { PLATFORM_GPG_FAILED } from '../../constants/error-messages';
    import { logger } from '../../logger';
    import { exec } from '../exec';
    
    let gitPrivateKey: string;
    let keyId: string;
    
    export function setPrivateKey(key: string): void {
      gitPrivateKey = key;
    }
    
    async function importKey(): Promise<void> {
      if (keyId) {
        return;
      }
      const keyFileName = path.join(os.tmpdir() + '/git-private.key');
      await fs.outputFile(keyFileName, gitPrivateKey);
      const { stdout, stderr } = await exec(`gpg --import ${keyFileName}`);
      logger.debug({ stdout, stderr }, 'Private key import result');
      keyId = (stdout + stderr)
        .split('\n')
        .find((line) => line.includes('secret key imported'))
        .replace('gpg: key ', '')
        .split(':')
        .shift();
      await fs.remove(keyFileName);
    }
    
    export async function writePrivateKey(cwd: string): Promise<void> {
      if (!gitPrivateKey) {
        return;
      }
      logger.debug('Setting git private key');
      try {
        await importKey();
        await exec(`git config user.signingkey ${keyId}`, { cwd });
        await exec(`git config commit.gpgsign true`, { cwd });
      } catch (err) {
        logger.warn({ err }, 'Error writing git private key');
        throw new Error(PLATFORM_GPG_FAILED);
      }
    }