> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Securing Secrets using HashiCorp Vault

> This article discusses integrating HashiCorp Vault with GitHub Actions for centralized secret management and automation of secret synchronization.

Managing sensitive credentials across multiple repositories can be challenging. GitHub Actions stores secrets at the repository or environment level, but lacks versioning and centralized policy controls. By integrating HashiCorp Vault, you can maintain a single source of truth and automate secret synchronization across all your workflows.

## Why Centralize Secret Management?

GitHub Actions secrets are easy to configure but can become a maintenance burden as your organization scales:

| Storage Type      | Versioning | Access Control                      | Maintenance Overhead   |
| ----------------- | ---------- | ----------------------------------- | ---------------------- |
| GitHub Repository | No         | Per-repo / per-environment policies | Duplicate in each repo |
| HashiCorp Vault   | Yes        | Fine-grained, dynamic ACLs & tokens | Centralized, auditable |

By standardizing on Vault, you gain:

* Automatic versioning and rotation
* Detailed audit logs
* Consistent policies across environments

<Frame>
  ![The image shows a GitHub repository settings page for managing secrets and variables, with a section for environment secrets and a repository secret named "DOCKER\_PASSWORD."](https://kodekloud.com/kk-media/image/upload/v1752876397/notes-assets/images/GitHub-Actions-Certification-Securing-Secrets-using-HashiCorp-Vault/github-repo-settings-secrets-variables.jpg)
</Frame>

## Defining a GitHub Actions Workflow

Create a workflow file under `.github/workflows/vault-demo.yaml` that manually triggers and checks for `AWS_API_KEY`:

<Frame>
  ![The image shows a GitHub repository interface displaying a list of YAML workflow files within the .github/workflows directory, along with their last commit messages and timestamps.](https://kodekloud.com/kk-media/image/upload/v1752876398/notes-assets/images/GitHub-Actions-Certification-Securing-Secrets-using-HashiCorp-Vault/github-repo-yaml-workflows-list.jpg)
</Frame>

```yaml theme={null}
name: Vault Demo
on:
  workflow_dispatch:

jobs:
  echo-vault-secret:
    runs-on: ubuntu-latest
    steps:
      - name: Verify AWS_API_KEY exists
        run: |
          if [[ -z "${{ secrets.AWS_API_KEY }}" ]]; then
            echo "::error::Secret Not Found"
            exit 1
          else
            echo "::notice::Secret Found"
            exit 0
          fi
```

<Callout icon="lightbulb" color="#1CB2FE">
  Ensure the workflow file is committed to the `main` branch (or your default branch) under `.github/workflows`.
</Callout>

When `AWS_API_KEY` is missing, the run fails:

<Frame>
  ![The image shows a GitHub Actions interface with a "Vault Demo" workflow that has been manually run. The interface includes options for managing workflows and viewing workflow runs.](https://kodekloud.com/kk-media/image/upload/v1752876399/notes-assets/images/GitHub-Actions-Certification-Securing-Secrets-using-HashiCorp-Vault/github-actions-vault-demo-workflow.jpg)
</Frame>

```bash theme={null}
# Simulated check when AWS_API_KEY is unset
if [[ -z "" ]]; then
  echo "Secret Not Found"
  exit 1
fi

# Output:
Secret Not Found
```

## Provisioning HashiCorp Vault on HCP

HashiCorp Vault Secrets on the [HashiCorp Cloud Platform](https://cloud.hashicorp.com/) provides a fully managed service for centralized secret storage.

1. Sign in at the [HashiCorp Vault website](https://www.vaultproject.io/).

<Frame>
  ![The image shows a webpage from HashiCorp Vault, highlighting "Standardize secrets management" with options to sign up for free or request a demo.](https://kodekloud.com/kk-media/image/upload/v1752876401/notes-assets/images/GitHub-Actions-Certification-Securing-Secrets-using-HashiCorp-Vault/hashicorp-vault-secrets-management-webpage.jpg)
</Frame>

2. From the HCP dashboard, select **Vault Secrets**:

<Frame>
  ![The image shows a project dashboard from HashiCorp Cloud Platform, displaying various services like Boundary, Consul, Packer, and Vault, along with project status and billing summary.](https://kodekloud.com/kk-media/image/upload/v1752876402/notes-assets/images/GitHub-Actions-Certification-Securing-Secrets-using-HashiCorp-Vault/hashicorp-cloud-platform-dashboard-services.jpg)
</Frame>

3. Click **Create application**, name it (e.g., **Secret App**), then add the `AWS_API_KEY` secret:

<Frame>
  ![The image shows a dashboard interface for HashiCorp Vault Secrets, welcoming the user and offering options to create an application and access resources like documentation and blog posts.](https://kodekloud.com/kk-media/image/upload/v1752876403/notes-assets/images/GitHub-Actions-Certification-Securing-Secrets-using-HashiCorp-Vault/hashicorp-vault-secrets-dashboard-interface.jpg)
</Frame>

4. Use the **Add secret** button to insert your key/value pair:

<Frame>
  ![The image shows a web interface for managing secrets in a project, with options to add or import secrets. A notification at the top indicates that "Vault Secrets" is now generally available.](https://kodekloud.com/kk-media/image/upload/v1752876404/notes-assets/images/GitHub-Actions-Certification-Securing-Secrets-using-HashiCorp-Vault/web-interface-managing-secrets-notification.jpg)
</Frame>

<Callout icon="lightbulb" color="#1CB2FE">
  New users may be eligible for free credits on HCP. Check the [pricing page](https://cloud.hashicorp.com/pricing) for details.
</Callout>

## Integrating Vault with GitHub Actions

Enable automatic synchronization so GitHub Actions can retrieve secrets directly from Vault:

1. In the Vault console, select **Integrations → GitHub Actions**:

<Frame>
  ![The image shows a web interface for integrating secret management tools, featuring options to add AWS Secrets Manager, GitHub Actions, and Vercel, along with documentation links for Terraform and Docker.](https://kodekloud.com/kk-media/image/upload/v1752876405/notes-assets/images/GitHub-Actions-Certification-Securing-Secrets-using-HashiCorp-Vault/secret-management-web-interface-integration.jpg)
</Frame>

2. Authorize access to your GitHub account and grant Vault permission to the target repository:

<Frame>
  ![The image shows a user interface for selecting GitHub repositories to integrate with HashiCorp Vault, featuring a dropdown menu with repository options.](https://kodekloud.com/kk-media/image/upload/v1752876406/notes-assets/images/GitHub-Actions-Certification-Securing-Secrets-using-HashiCorp-Vault/github-repositories-vault-integration-ui.jpg)
</Frame>

3. Configure the sync destination and save:

<Frame>
  ![The image shows a web interface for adding a new sync destination in HashiCorp Vault, with options to select an organization and repository for GitHub Actions integration. There are buttons to save and sync secrets or cancel the action.](https://kodekloud.com/kk-media/image/upload/v1752876408/notes-assets/images/GitHub-Actions-Certification-Securing-Secrets-using-HashiCorp-Vault/hashicorp-vault-github-actions-sync-interface.jpg)
</Frame>

Integration at a glance:

| Step              | Description                               |
| ----------------- | ----------------------------------------- |
| Authorize GitHub  | Grant Vault read access to selected repos |
| Select Repository | Choose the repo containing your workflow  |
| Configure Sync    | Map Vault path to GitHub secret name      |
| Save & Sync       | Trigger initial secret import             |

## Verifying the Workflow

After syncing, revisit **Settings → Secrets and variables → Actions** to confirm `AWS_API_KEY` appears alongside other repository secrets:

<Frame>
  ![The image shows a GitHub repository settings page, specifically the "Secrets and variables" section under "Actions," displaying environment and repository secrets like "AWS\_API\_KEY" and "DOCKER\_PASSWORD."](https://kodekloud.com/kk-media/image/upload/v1752876409/notes-assets/images/GitHub-Actions-Certification-Securing-Secrets-using-HashiCorp-Vault/github-repo-settings-secrets-variables-2.jpg)
</Frame>

Re-run the **Vault Demo** workflow. The secret check now passes:

<Frame>
  ![The image shows a GitHub Actions interface with a workflow named "Vault Demo" running a job called "echo-vault-secret." The job is in the process of starting on a hosted runner.](https://kodekloud.com/kk-media/image/upload/v1752876410/notes-assets/images/GitHub-Actions-Certification-Securing-Secrets-using-HashiCorp-Vault/github-actions-vault-demo-echo-job.jpg)
</Frame>

```bash theme={null}
# Masked secret check
if [[ -z "***" ]]; then
  echo "Secret Not Found"
  exit 1
else
  echo "Secret Found"
  exit 0
fi

# Output:
Secret Found
```

<Callout icon="triangle-alert" color="#FF6B6B">
  Always verify that only the minimum required permissions are granted when authorizing integrations. Avoid exposing secrets in plaintext logs.
</Callout>

## Links and References

* [HashiCorp Vault Documentation](https://www.vaultproject.io/docs)
* [GitHub Actions Secrets](https://docs.github.com/actions/security-guides/encrypted-secrets)
* [HashiCorp Cloud Platform](https://cloud.hashicorp.com/)
* [Vault Terraform Provider](https://registry.terraform.io/providers/hashicorp/vault/latest)

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/github-actions-certification/module/a3e810f5-af92-4e1c-ac54-bdf50ddbe9cf/lesson/c50a8686-8a5d-4c58-bf96-d04a242ea354" />
</CardGroup>
