docs/current_docs/partials/cookbook/secrets/_use-secret-variable.mdx
The following Dagger Function accepts a GitHub personal access token as a Secret, and uses the Secret to authorize a request to the GitHub API. The secret may be sourced from the host (via an environment variable, host file, or host command) or from an external secrets manager (1Password, Vault, or AWS Secrets Manager/Parameter Store):
You can use a secret sourced from an environment variable by running the following command:
<Tabs groupId="shell"> <TabItem value="System shell"> ```shell dagger -c 'github-api env://GITHUB_API_TOKEN' ``` </TabItem> <TabItem value="Dagger Shell"> ```shell title="First type 'dagger' for interactive mode." github-api env://GITHUB_API_TOKEN ``` </TabItem> <TabItem value="Dagger CLI"> ```shell dagger call github-api --token=env://GITHUB_API_TOKEN ``` </TabItem> </Tabs>You can also pass files to secrets by following the example below:
<Tabs groupId="shell"> <TabItem value="System shell"> ```shell dagger -c 'github-api file://./github.txt' ``` </TabItem> <TabItem value="Dagger Shell"> ```shell title="First type 'dagger' for interactive mode." github-api file://./github.txt ``` </TabItem> <TabItem value="Dagger CLI"> ```shell dagger call github-api --token=file://./github.txt ``` </TabItem> </Tabs>Secrets also support capturing data from running commands.
<Tabs groupId="shell"> <TabItem value="System shell"> ```shell dagger -c 'github-api cmd://"gh auth token"' ``` </TabItem> <TabItem value="Dagger Shell"> ```shell title="First type 'dagger' for interactive mode." github-api cmd://"gh auth token" ``` </TabItem> <TabItem value="Dagger CLI"> ```shell dagger call github-api --token=cmd://"gh auth token" ``` </TabItem> </Tabs>Use a secret from 1Password:
:::note
If using a 1Password service account, ensure that the OP_SERVICE_ACCOUNT_TOKEN environment variable is set.
export OP_SERVICE_ACCOUNT_TOKEN="mytoken"
:::
<Tabs groupId="shell"> <TabItem value="System shell"> ```shell dagger -c 'github-api op://infra/github/credential' ``` </TabItem> <TabItem value="Dagger Shell"> ```shell title="First type 'dagger' for interactive mode." github-api op://infra/github/credential ``` </TabItem> <TabItem value="Dagger CLI"> ```shell dagger call github-api --token=op://infra/github/credential ``` </TabItem> </Tabs>You can retrieve secrets from Hashicorp Vault.
:::note
Ensure that the VAULT_ADDR and either the VAULT_TOKEN or VAULT_APPROLE_ROLE_ID (for Vault AppRole authentication) environment variables are set.
export VAULT_ADDR="https://127.0.0.1:8200"
export VAULT_TOKEN="gue55me7"
export VAULT_APPROLE_ROLE_ID="roleid-xxx-yyy-zzz"
:::
<Tabs groupId="shell"> <TabItem value="System shell"> ```shell dagger -c 'github-api vault://credentials.github' ``` </TabItem> <TabItem value="Dagger Shell"> ```shell title="First type 'dagger' for interactive mode." github-api vault://credentials.github ``` </TabItem> <TabItem value="Dagger CLI"> ```shell dagger call github-api --token=vault://credentials.github ``` </TabItem> </Tabs>You can retrieve secrets from AWS Secrets Manager and AWS Systems Manager Parameter Store. Secrets Manager secrets can by referenced by using the
aws+sm prefix in your URI, while Parameter Store secrets can be referenced using the aws+ps prefix respectively.
:::note
Ensure that AWS credentials are configured via environment variables (AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY), the shared credentials file (~/.aws/credentials), or IAM role credentials. The AWS_REGION environment variable must also be set.
export AWS_REGION="us-east-1"
export AWS_ACCESS_KEY_ID="AKIAIOSFODNN7EXAMPLE"
export AWS_SECRET_ACCESS_KEY="wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"
:::
Use the aws+sm:// scheme with a secret name to retrieve from Secrets Manager:
For JSON secrets, extract a specific field:
<Tabs groupId="shell"> <TabItem value="System shell"> ```shell dagger -c 'github-api aws+sm://prod/credentials?field=github_token' ``` </TabItem> <TabItem value="Dagger Shell"> ```shell title="First type 'dagger' for interactive mode." github-api aws+sm://prod/credentials?field=github_token ``` </TabItem> <TabItem value="Dagger CLI"> ```shell dagger call github-api --token=aws+sm://prod/credentials?field=github_token ``` </TabItem> </Tabs>Use the aws+ps:// scheme to retrieve from Parameter Store: