# Deploy from GitHub Actions without a stored key

Let the GitHub Actions job trade its own OIDC token for credentials that last at most one hour, so no access key is stored in GitHub.

The step below sets `DCLOUD_ACCESS_KEY` for the rest of the job, so every later step of
[Deploy from GitHub Actions](deploy-from-github-actions.md) works unchanged. Replace
`org-EXAMPLE1234` with your organization's id and role-id with the role's id.

## Prerequisites

- GitHub Actions registered as an identity provider in IAM, and a role with the CI policy from
  [Deploy from GitHub Actions](deploy-from-github-actions.md#step-2-create-the-ci-key) and a trust
  policy naming your repository. Setup and the trust policy:
  [IAM, deploy from GitHub Actions without a stored key](/docs/iam#deploy-from-github-actions-without-a-stored-key).

## Add the sign-in step

Put this before the deploy step of the workflow:

```yaml
permissions:
  id-token: write # lets the job request an OIDC token
  contents: read

# … in the job's steps, before the deploy:
- name: Sign in (OIDC)
  env:
    DCLOUD_ROLE_RN: rn:ews::org-EXAMPLE1234:iam:role/role-id
    EWS_AUDIENCE: ${{ vars.DCLOUD_API_URL }}
  run: |
    AUD=$(jq -rn --arg a "$EWS_AUDIENCE" '$a|@uri')
    OIDC_TOKEN=$(curl -sS --fail-with-body -H "Authorization: bearer $ACTIONS_ID_TOKEN_REQUEST_TOKEN" \
      "$ACTIONS_ID_TOKEN_REQUEST_URL&audience=$AUD" | jq -r .value)
    CREDS=$(curl -sS --fail-with-body -X POST "${{ vars.DCLOUD_API_URL }}/iam/api/sts/assume-role-with-web-identity" \
      -H 'content-type: application/json' \
      -d "$(jq -n --arg role "$DCLOUD_ROLE_RN" --arg token "$OIDC_TOKEN" '{roleRn: $role, webIdentityToken: $token}')")
    KEY="$(jq -r .data.accessKeyId <<<"$CREDS"):$(jq -r .data.secretKey <<<"$CREDS")"
    echo "::add-mask::$KEY"
    echo "DCLOUD_ACCESS_KEY=$KEY" >> "$GITHUB_ENV"
```

`EWS_AUDIENCE` must be one of the audiences the identity provider lists in IAM; a token for any
other audience is refused (`400`, `InvalidIdentityToken: …`). A role whose trust policy does not
admit the job answers `403`. The credentials last one hour; add `durationSeconds` (900 to 3600) to
the request body for less.

The deploy step then reads `DCLOUD_ACCESS_KEY` from the environment rather than from
`secrets.DCLOUD_ACCESS_KEY`: drop `DCLOUD_ACCESS_KEY: ${{ secrets.DCLOUD_ACCESS_KEY }}` from the job's
`env:`.

## Result

The job signs in with credentials that last at most one hour; no key is stored in the GitHub
repository. The composite action works the same way, because it reads credentials only from
`DCLOUD_API_URL` and `DCLOUD_ACCESS_KEY`.

## Related

- [Deploy from GitHub Actions](deploy-from-github-actions.md)
- [IAM](/docs/iam)
