liveBook · Manning

GitHub Actions secret

As explained in Road to Kubernetes

A GitHub Actions secret is an encrypted repository-scoped value that workflows can access without exposing its contents in source code or logs.

A GitHub Actions secret is a stored repository value that a workflow can use without putting the value in its source file. This matters when a deployment needs an SSH private key, a Docker Hub token, or application settings. Keep those values out of Git repositories, and do not print them during a workflow run. section 4.1.2 section 4.1.5 section 7.3.1

To give a workflow SSH access to a virtual machine:

  • Generate a new SSH key pair for this deployment rather than using your personal key. Put the public key on the virtual machine. section 4.1.2
  • In the GitHub repository, open Settings → Secrets and Variables → Actions → New Repository Secret. Name the secret SSH_PRIVATE_KEY and paste in the private key’s contents. section 4.1.2
  • Store the machine’s IP address as AKAMAI_INSTANCE_IP_ADDRESS. Refer to either stored value in workflow YAML with `${{ secrets.SECRET_NAME }}`. section 4.1.5

Use the secret during an NGINX installation

A workflow writes `${{ secrets.SSH_PRIVATE_KEY }}` to `~/.ssh/id_rsa`, sets the file mode to `600`, and uses `${{ secrets.AKAMAI_INSTANCE_IP_ADDRESS }}` as the SSH destination. Its next commands connect to the machine and install NGINX. The workflow contains references to the stored values, not the private key itself. section 4.1.5

Listing 4.4 Installing NGINX with GitHub Actions
name: Install NGINX
on:
  workflow_dispatch:
  push:

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3
    - name: Configure the SSH Private Key Secret
      run: |
        mkdir -p ~/.ssh/
        echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_rsa
        chmod 600 ~/.ssh/id_rsa
    - name: Set Strict Host Key Checking
      run: echo "StrictHostKeyChecking=no" > ~/.ssh/config
    - name: Install NGINX
      run: |
        export MY_HOST="${{ secrets.AKAMAI_INSTANCE_IP_ADDRESS }}"
        ssh root@$MY_HOST sudo apt update
        ssh root@$MY_HOST sudo apt install nginx -y

The workflow listing puts secret references alongside the SSH setup and remote installation commands. section 4.1.5

The same pattern extends to container deployments. A workflow can pass DOCKERHUB_USERNAME and DOCKERHUB_TOKEN to a Docker Hub login action before building and pushing an image. For a production Docker Compose deployment, it can also create a `.env` file from stored application secrets and copy that file to the virtual machine. Once created, that file needs its own care: encrypting it during transfer is not covered here, and the workflow includes a step to remove its local copy. section 7.3.1 section 7.3.3

Check that every name used in workflow YAML matches a stored repository secret. The pattern also works for other deployment credentials: a Kubernetes deployment workflow stores a kubeconfig as KUBECONFIG_SA, then writes its value to the runner’s kubectl configuration. section 7.3.1 section 8.5.2

How Road to Kubernetes covers it

How deep it goes
Walks through repository setup and workflow use, then flags dotenv transfer encryption as outside scope.
Examples
Uses an NGINX installation workflow, Docker Hub image publishing, and a generated production dotenv file.
What it assumes
Assumes a GitHub repository and a workflow, but gives explicit secret-creation steps and points SSH newcomers to further guidance.
What you can do after
Operate/configure it by storing repository values, referencing them in workflows, and checking that secret names match.
How it connects
Connects secrets to SSH, Docker Hub authentication, Docker Compose environment files, and Kubernetes kubeconfig access.

Also explained in

Compare how 3 books explain GitHub Actions secret →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Road to Kubernetes. The section links point to the parts of the book it draws on.