Skip to content

Commit

Permalink
secrets: factor out into own action
Browse files Browse the repository at this point in the history
This is so discovery can properly setup secrets for uploading results
to a Nix cache.
  • Loading branch information
nrdxp committed Dec 12, 2022
1 parent da2419a commit 97afc03
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 55 deletions.
8 changes: 0 additions & 8 deletions discover/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,6 @@ outputs:
runs:
using: "composite"
steps:
- name: Set Environment
run: |
delim=$RANDOM
cat << $delim >> "$GITHUB_ENV"
$delim
shell: bash

- name: Cache Nix Store
id: cache-nix
uses: divnix/[email protected]
Expand Down
8 changes: 8 additions & 0 deletions discover/eval.sh
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ function cache() {

drvs=$(jq -r '.[]|select(.targetDrv != null)|.targetDrv' <<< "$result")

if [[ $CACHE =~ ^s3:// && -f $NIX_KEY_PATH ]]; then
if [[ $CACHE =~ \? ]]; then
CACHE="$CACHE&secret-key=$NIX_KEY_PATH"
else
CACHE="$CACHE?secret-key=$NIX_KEY_PATH"
fi
fi

if [[ -n $drvs ]]; then
#shellcheck disable=SC2086
nix copy --derivation --to "$CACHE" $drvs
Expand Down
48 changes: 1 addition & 47 deletions run/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,10 @@ name: "Sandard Action Runner"
description: "Build a target and run its action."

inputs:
key_path:
description: "The path to place the Nix secret key."
required: false
default: "${{ runner.temp }}/.nix-key"
github_pat:
description: "GitHub token for accessing private repos."
required: false
default: ${{ github.token }}
nix_key:
description: "Secret key used for Nix package signing."
required: false
s3_key:
description: "Key to upload to S3 Buckets with Nix."
required: false
s3_id:
description: "ID to upload to S3 Buckets with Nix."
required: false
cache:
description: "Nix cache to optionally upload artifacts to"
# auto refers to the local nix store, so nothing is uploaded by default
Expand All @@ -31,14 +18,6 @@ inputs:
json:
description: "JSON describing the action, found in the discovery phase."
required: true
nix_ssh_key:
description: "SSH private key to send builds to a remote Nix builder."
default: none
required: false
ssh_known_hosts:
description: "SSH known host keys for Nix builders"
default: none
required: false
extra_nix_config:
description: "Configuration to append to the nix.conf."
required: false
Expand All @@ -53,8 +32,6 @@ runs:
cat << $delim >> "$GITHUB_ENV"
DISC_PATH=${{ runner.temp }}/discovery
DISC_ARC_PATH=${{ runner.temp }}/discovery.tar.zstd
SSH_AUTH_SOCK=${{ runner.temp }}/ssh_agent.sock
NIX_KEY_PATH=${{ inputs.key_path }}
BUILDER=${{ inputs.builder }}
CACHE=${{ inputs.cache }}
$delim
Expand All @@ -64,15 +41,6 @@ runs:
echo "$delim" >> $GITHUB_ENV
shell: bash

- name: Emplace Secrets
if: inputs.nix_key != '' || inputs.s3_id != '' || inputs.s3_key != ''
run: ${{ github.action_path }}/secrets.sh
shell: bash
env:
NIX_SECRET_KEY: ${{ inputs.nix_key }}
AWS_ACCESS_KEY_ID: ${{ inputs.s3_id }}
AWS_SECRET_ACCESS_KEY: ${{ inputs.s3_key }}

- name: Restore Cache & Discovery Results
id: restore-cache
uses: divnix/[email protected]
Expand All @@ -93,20 +61,6 @@ runs:
secret-key-files = ${{ env.NIX_KEY_PATH }}
${{ inputs.extra_nix_config }}
- name: Setup SSH Keys
if: inputs.nix_ssh_key != 'none' && inputs.builder != 'auto'
run: |
ssh-agent -a "$SSH_AUTH_SOCK" > /dev/null
ssh-add - <<< "${{ inputs.nix_ssh_key }}"
shell: bash

- name: Setup SSH Known Hosts
if: inputs.nix_ssh_key != 'none' && inputs.ssh_known_hosts != 'none' && inputs.builder != 'auto'
run: |
sudo sh -c 'echo "${{ inputs.ssh_known_hosts }}" >> /etc/ssh/ssh_known_hosts'
sudo chmod 0644 /etc/ssh/ssh_known_hosts
shell: bash

- name: Build ${{ fromJSON(env.JSON).name }}
id: build
run: |
Expand All @@ -124,7 +78,7 @@ runs:
- name: Populate Cache
env:
UNCACHED: ${{ steps.build.outputs.uncached }}
if: always() && env.CACHE != 'auto' && inputs.nix_key != '' && env.UNCACHED != '' && steps.build.conclusion == 'success'
if: always() && env.CACHE != 'auto' env.NIX_KEY_PATH != '' && env.UNCACHED != '' && steps.build.conclusion == 'success'
run: |
${{ github.action_path }}/cache.sh
shell: bash
61 changes: 61 additions & 0 deletions secrets/action.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
name: "Sandard Action Secrets"
description: "Setup common Nix secrets."

inputs:
key_path:
description: "The path to place the Nix secret key."
required: false
default: "${{ runner.temp }}/.nix-key"
nix_key:
description: "Secret key used for Nix package signing."
required: false
s3_key:
description: "Key to upload to S3 Buckets with Nix."
required: false
s3_id:
description: "ID to upload to S3 Buckets with Nix."
required: false
nix_ssh_key:
description: "SSH private key to send builds to a remote Nix builder."
default: none
required: false
ssh_known_hosts:
description: "SSH known host keys for Nix builders"
default: none
required: false

runs:
using: "composite"
steps:
- name: Set Environment
run: |
delim=$RANDOM
cat << $delim >> "$GITHUB_ENV"
SSH_AUTH_SOCK=${{ runner.temp }}/ssh_agent.sock
NIX_KEY_PATH=${{ inputs.key_path }}
$delim
shell: bash

- name: Emplace Secrets
if: inputs.nix_key != '' || inputs.s3_id != '' || inputs.s3_key != ''
run: ${{ github.action_path }}/secrets.sh
shell: bash
env:
NIX_SECRET_KEY: ${{ inputs.nix_key }}
AWS_ACCESS_KEY_ID: ${{ inputs.s3_id }}
AWS_SECRET_ACCESS_KEY: ${{ inputs.s3_key }}

- name: Setup SSH Keys
if: inputs.nix_ssh_key != 'none'
run: |
ssh-agent -a "$SSH_AUTH_SOCK" > /dev/null
ssh-add - <<< "${{ inputs.nix_ssh_key }}"
shell: bash

- name: Setup SSH Known Hosts
if: inputs.nix_ssh_key != 'none' && inputs.ssh_known_hosts != 'none'
run: |
sudo sh -c 'echo "${{ inputs.ssh_known_hosts }}" >> /etc/ssh/ssh_known_hosts'
sudo chmod 0644 /etc/ssh/ssh_known_hosts
shell: bash
File renamed without changes.

0 comments on commit 97afc03

Please sign in to comment.