|
| 1 | +name: Setup GPG |
| 2 | +description: Configures this action to run gpg with a given key and pass |
| 3 | +inputs: |
| 4 | + gpg-private-key: |
| 5 | + description: GPG private key exported as an ASCII armored version or its base64 encoding |
| 6 | + required: true |
| 7 | + gpg-key-pass: |
| 8 | + description: GPG key pass |
| 9 | + required: true |
| 10 | + gpg-public-key: |
| 11 | + description: GPG public key exported as an ASCII armored version or its base64 encoding |
| 12 | + required: true |
| 13 | +runs: |
| 14 | + using: composite |
| 15 | + steps: |
| 16 | + - name: Check Ubuntu version |
| 17 | + shell: bash |
| 18 | + run: | |
| 19 | + if [[ "$(cat /etc/os-release | grep VERSION_ID)" != *"22.04"* ]]; then |
| 20 | + echo "This action only supports Ubuntu 22.04" |
| 21 | + exit 1 |
| 22 | + fi |
| 23 | +
|
| 24 | + - name: check if private key is not empty |
| 25 | + shell: bash |
| 26 | + env: |
| 27 | + PRIVATE_KEY: ${{ inputs.gpg-private-key }} |
| 28 | + if: ${{ env.PRIVATE_KEY == '' }} |
| 29 | + run: | |
| 30 | + echo "the gpg-private-key was empty" |
| 31 | + exit 1 |
| 32 | +
|
| 33 | + - name: check if key pass is not empty |
| 34 | + shell: bash |
| 35 | + env: |
| 36 | + KEY_PASS: ${{ inputs.gpg-key-pass }} |
| 37 | + if: ${{ env.KEY_PASS == '' }} |
| 38 | + run: | |
| 39 | + echo "the secret gpg-key-pass was empty" |
| 40 | + exit 1 |
| 41 | +
|
| 42 | + - name: check if public key pass is empty |
| 43 | + shell: bash |
| 44 | + env: |
| 45 | + PUBLIC_KEY: ${{ inputs.gpg-public-key }} |
| 46 | + if: ${{ env.PUBLIC_KEY == '' }} |
| 47 | + run: | |
| 48 | + echo "the secret gpg-public-pass was empty" |
| 49 | + exit 1 |
| 50 | +
|
| 51 | + - name: install tools |
| 52 | + shell: bash |
| 53 | + run: | |
| 54 | + sudo apt-get update && sudo apt-get install ca-certificates gnupg rpm pinentry-tty -y |
| 55 | +
|
| 56 | + - name: Set up GPG |
| 57 | + shell: bash |
| 58 | + env: |
| 59 | + GPG_PRIVATE_KEY: ${{ inputs.gpg-private-key }} |
| 60 | + GPG_KEY_PASS: ${{ inputs.gpg-key-pass }} |
| 61 | + GPG_PUBLIC_KEY: ${{ inputs.gpg-public-key }} |
| 62 | + HOME: /home/runner |
| 63 | + GNUPGHOME: /home/runner/.gnupg |
| 64 | + run: | |
| 65 | + # Ensure environment variables are exported |
| 66 | + export HOME="${HOME}" |
| 67 | + export GNUPGHOME="${GNUPGHOME}" |
| 68 | +
|
| 69 | + # Setup gpg with consistent path |
| 70 | + mkdir -p "$GNUPGHOME" |
| 71 | + chmod 700 "$GNUPGHOME" |
| 72 | +
|
| 73 | + # Import the private key |
| 74 | + gpg --import --batch --yes <<< "$GPG_PRIVATE_KEY" |
| 75 | +
|
| 76 | + # Get the key fingerprint and name from the imported key |
| 77 | + KEY_FP=$(gpg --list-secret-keys --with-colons | awk -F: '/^fpr:/ { print $10 }' | head -n1) |
| 78 | + KEY_NAME=$(gpg --list-secret-keys --with-colons | awk -F: '/^uid:/ { print $10 }' | head -n1) |
| 79 | +
|
| 80 | + # Set default key |
| 81 | + echo "default-key $KEY_FP" >> "$GNUPGHOME/gpg.conf" |
| 82 | +
|
| 83 | + # Configure passphrase handling |
| 84 | + echo "allow-preset-passphrase" >> "$GNUPGHOME/gpg-agent.conf" |
| 85 | + echo "allow-loopback-pinentry" >> "$GNUPGHOME/gpg-agent.conf" |
| 86 | +
|
| 87 | + # Configure GPG for non-interactive use |
| 88 | + echo "use-agent" >> "$GNUPGHOME/gpg.conf" |
| 89 | + echo "pinentry-mode loopback" >> "$GNUPGHOME/gpg.conf" |
| 90 | + echo "batch" >> "$GNUPGHOME/gpg.conf" |
| 91 | + echo "no-tty" >> "$GNUPGHOME/gpg.conf" |
| 92 | + echo "passphrase-file $GNUPGHOME/passphrase" >> "$GNUPGHOME/gpg.conf" |
| 93 | +
|
| 94 | + # Create passphrase file |
| 95 | + echo "$GPG_KEY_PASS" > "$GNUPGHOME/passphrase" |
| 96 | + chmod 600 "$GNUPGHOME/passphrase" |
| 97 | +
|
| 98 | + # Set permissions on specific files we know exist |
| 99 | + for file in "$GNUPGHOME/gpg.conf" "$GNUPGHOME/gpg-agent.conf" "$GNUPGHOME/passphrase"; do |
| 100 | + if [ -f "$file" ]; then |
| 101 | + chmod 600 "$file" |
| 102 | + fi |
| 103 | + done |
| 104 | +
|
| 105 | + # Create symlink for gpg2 (required for rpm signing) |
| 106 | + sudo ln -sf $(which gpg) /usr/bin/gpg2 |
| 107 | +
|
| 108 | + # Configure rpm macros line by line |
| 109 | + { |
| 110 | + echo "%_signature gpg" |
| 111 | + echo "%_gpg_path $GNUPGHOME" |
| 112 | + echo "%_gpg_name $KEY_FP" |
| 113 | + echo "%_gpgbin /usr/bin/gpg2" |
| 114 | + echo "%__gpg /usr/bin/gpg2" |
| 115 | + echo "%__gpg_sign_cmd %{__gpg} --batch --pinentry-mode loopback --passphrase-file $GNUPGHOME/passphrase --no-armor --no-secmem-warning --no-tty -u \"%{_gpg_name}\" -sbo %{__signature_filename} %{__plaintext_filename}" |
| 116 | + } > "$HOME/.rpmmacros" |
| 117 | + chmod 600 "$HOME/.rpmmacros" |
| 118 | +
|
| 119 | + # Import public key for verification |
| 120 | + echo -e "$GPG_PUBLIC_KEY" > "$GNUPGHOME/public.key" |
| 121 | + chmod 600 "$GNUPGHOME/public.key" |
| 122 | + gpg --import "$GNUPGHOME/public.key" |
| 123 | + rpm --import "$GNUPGHOME/public.key" |
| 124 | +
|
| 125 | + # Reload gpg-agent configuration |
| 126 | + gpg-connect-agent reloadagent /bye |
0 commit comments