CI/CD with Service Tokens
Automate secret injection in GitHub Actions, GitLab CI, and Docker using scoped non-interactive Service Tokens with zero plaintext on disk.
Automated CI/CD pipelines require credentials to run tests, build container images, and deploy services. Storing plaintext secrets in multiple CI runner settings or exporting .env files inside Docker contexts leads to credential leakage and drift.
Ray Secrets solves this with Service Tokens: non-interactive credentials strictly scoped to a single Project and Environment.
1. Create a Scoped Service Token
Service Tokens provide headless machine authentication for CI/CD runners and automated pipelines. You can generate a token from the Developer Console or directly from your Ray Secrets project dashboard:
- Navigate to the Developer Console and select your Workspace.
- In the sidebar under Workspace Management, select Access Tokens (or click Tokens in your Ray Secrets project sidebar).
- On the Service Tokens tab, click + Create Token.
- In the creation sheet:
- Enter a descriptive Token Name (e.g.
github-actions-ci). - Select an Expiration preset (
7 days,30 days,90 days,Custom, orNever). - Under Access & Permissions, configure the scope for your target project and assign secret read permissions.
- Enter a descriptive Token Name (e.g.
- Click Generate Token and copy the revealed secret:
ray_st_example_000000Service Tokens are displayed only once upon creation. Store the token immediately in your CI provider's encrypted secret store as RAY_TOKEN.
2. GitHub Actions Integration
In your GitHub repository settings, add a repository secret named RAY_TOKEN containing your Service Token.
Add the following workflow configuration in .github/workflows/ci.yml:
name: CIon: push: branches: [main] pull_request: branches: [main]jobs: test: runs-on: ubuntu-latest steps: - name: Checkout repository uses: actions/checkout@v4 - name: Setup Node.js uses: actions/setup-node@v4 with: node-version: 22 cache: npm - name: Install dependencies run: npm ci - name: Install Ray CLI run: curl -fsSL https://secrets.rcmade.io/install.sh | sh - name: Run test suite with injected secrets env: RAY_TOKEN: ${{ secrets.RAY_TOKEN }} run: ray run --base --strict --ci -- npm testNotice the critical flags (--ci, --strict, and --base) added for robust CI automation:
| Flag / Option | Type | Description |
|---|---|---|
--ci | boolean | Optimize execution for headless CI/CD runners (disables spinners, prompts, colors, and update checks) |
--strict(--exit-code) | boolean | Abort execution immediately with an exit code if any required secret is missing, empty, or drifting |
-b, --base(-t, --team) | boolean | Fetch canonical team environment secrets, ignoring personal workstation overrides (recommended for CI/CD) |
If running in a repository without a .ray/workspace.json file, pass the project (--project) and environment (--env (--environment <name>)) explicitly:
- name: Run test suite with explicit project binding env: RAY_TOKEN: ${{ secrets.RAY_TOKEN }} run: ray run --env "<env-name>" --base --project "<project-id>" --strict --ci -- npm test| Flag / Option | Type | Description |
|---|---|---|
-p, --project <id_or_name> | string | Target project ID or slug to link or query, bypassing interactive selection |
-e, --env <name>(--environment <name>) | string | Target environment name (e.g. development, staging, production) to read or modify |
3. GitLab CI Integration
In GitLab, navigate to Settings > CI/CD > Variables and add RAY_TOKEN as a Masked and Protected variable.
Configure your .gitlab-ci.yml:
stages: - test - buildtest_job: stage: test image: node:22-bullseye-slim before_script: - apt-get update && apt-get install -y curl - curl -fsSL https://secrets.rcmade.io/install.sh | sh - export PATH="$HOME/.ray/bin:$PATH" script: - npm ci - ray run --base --strict --ci -- npm test4. Docker & Container Builds
When building Docker images, you often need secrets during build time (e.g. private npm registries, database seed migrations) without leaking them into the final image layers.
Use BuildKit secret mounting combined with ray run:
# syntax=docker/dockerfile:1.4FROM node:22-alpine AS builderWORKDIR /appCOPY package*.json ./# Install Ray CLI (requires C++ runtime packages on Alpine musl)RUN apk add --no-cache libstdc++ libgcc ca-certificates curl \ && curl -fsSL https://secrets.rcmade.io/install.sh | shCOPY . .# Inject secrets during build with zero disk persistenceRUN --mount=type=secret,id=RAY_TOKEN \ RAY_TOKEN=$(cat /run/secrets/RAY_TOKEN) \ ray run --base --strict --ci -- npm run buildFROM node:22-alpine AS runnerWORKDIR /appCOPY --from=builder /app/.next ./.nextCOPY --from=builder /app/node_modules ./node_modulesCOPY --from=builder /app/package.json ./package.jsonEXPOSE 3000CMD ["npm", "start"]Build the container image using BuildKit secret forwarding:
docker build --secret id=RAY_TOKEN,env=RAY_TOKEN -t my-app .5. Exporting Secrets with Ray Pull in CI
While ray run provides zero-disk in-memory secret injection during testing and container builds, certain CI/CD workflows and deployment pipelines require pulling static environment files or streaming secrets to external toolchains (e.g. mobile compilation pipelines, Kubernetes manifest generation, Docker Compose, or Terraform inputs).
Service Tokens authenticate ray pull non-interactively across diverse CI/CD automation scenarios:
Case 1: Standard .env Generation for Build Toolchains
For compilation tools, mobile frameworks (e.g. Expo, React Native, Xcode, Gradle), or Docker Compose that expect physical .env files:
ray pull --file .env --base --strict --ci| Flag / Option | Type | Description |
|---|---|---|
-f, --file <path> | string | Path to the source or destination .env file (defaults to .env in current directory) |
-b, --base(-t, --team) | boolean | Fetch canonical team environment secrets, ignoring personal workstation overrides (recommended for CI/CD) |
--strict(--exit-code) | boolean | Abort execution immediately with an exit code if any required secret is missing, empty, or drifting |
--ci | boolean | Optimize execution for headless CI/CD runners (disables spinners, prompts, colors, and update checks) |
Case 2: Structured Machine Formats (JSON & YAML) for Deployments
Export environment secrets into structured machine formats for Kubernetes Secret/ConfigMap manifests, Terraform variables (terraform.tfvars.json), or Ansible playbooks:
ray pull --file secrets.json --format json --base --strict --ci| Flag / Option | Type | Description |
|---|---|---|
-f, --file <path> | string | Path to the source or destination .env file (defaults to .env in current directory) |
--format <type> | string | File serialization format for exported secrets (env, json, yaml, csv, shell, docker) |
-b, --base(-t, --team) | boolean | Fetch canonical team environment secrets, ignoring personal workstation overrides (recommended for CI/CD) |
--strict(--exit-code) | boolean | Abort execution immediately with an exit code if any required secret is missing, empty, or drifting |
--ci | boolean | Optimize execution for headless CI/CD runners (disables spinners, prompts, colors, and update checks) |
Case 3: Monorepo Package Scoping
In monorepos, pull only secrets targeted for a specific package or microservice directory using --scope, preventing unwanted secrets from leaking into adjacent packages:
ray pull --file apps/web/.env --base --scope apps/web --strict --ci| Flag / Option | Type | Description |
|---|---|---|
-f, --file <path> | string | Path to the source or destination .env file (defaults to .env in current directory) |
-b, --base(-t, --team) | boolean | Fetch canonical team environment secrets, ignoring personal workstation overrides (recommended for CI/CD) |
-s, --scope <path> | string | Subdirectory path or workspace scope to target instead of auto-detecting from the working directory (e.g. apps/web or packages/backend) |
--strict(--exit-code) | boolean | Abort execution immediately with an exit code if any required secret is missing, empty, or drifting |
--ci | boolean | Optimize execution for headless CI/CD runners (disables spinners, prompts, colors, and update checks) |
Case 4: Explicit Project & Environment Binding (Dynamic Matrices)
In dynamic matrix jobs or deployment pipelines where the runner does not have a committed .ray/workspace.json:
ray pull --project "<project-id>" --env production --file .env --base --strict --ci| Flag / Option | Type | Description |
|---|---|---|
-p, --project <id_or_name> | string | Target project ID or slug to link or query, bypassing interactive selection |
-e, --env <name>(--environment <name>) | string | Target environment name (e.g. development, staging, production) to read or modify |
-f, --file <path> | string | Path to the source or destination .env file (defaults to .env in current directory) |
-b, --base(-t, --team) | boolean | Fetch canonical team environment secrets, ignoring personal workstation overrides (recommended for CI/CD) |
--strict(--exit-code) | boolean | Abort execution immediately with an exit code if any required secret is missing, empty, or drifting |
--ci | boolean | Optimize execution for headless CI/CD runners (disables spinners, prompts, colors, and update checks) |
Case 5: Zero-Disk Stdout Streaming & Process Piping
Stream decrypted secrets directly to standard output with --stdout to pipe into Docker or process inputs without creating plaintext files on the runner disk:
ray pull --stdout --base --strict --ciExample: Pipe directly into Docker without writing secrets to the filesystem:
ray pull --stdout --base --strict --ci | docker run --env-file - my-app:latestExample: Source secrets into the current runner shell session:
eval "$(ray pull --format shell --stdout --base --strict --ci)"| Flag / Option | Type | Description |
|---|---|---|
--stdout | boolean | Stream decrypted secret content directly to standard output without writing files to disk |
-b, --base(-t, --team) | boolean | Fetch canonical team environment secrets, ignoring personal workstation overrides (recommended for CI/CD) |
--strict(--exit-code) | boolean | Abort execution immediately with an exit code if any required secret is missing, empty, or drifting |
--ci | boolean | Optimize execution for headless CI/CD runners (disables spinners, prompts, colors, and update checks) |
Case 6: Template Drift Audit & Verification
Validate pulled secrets against a reference template file (such as .env.example) with --template. When combined with --strict, the pipeline aborts immediately if any required variable declared in the template is missing from the remote vault:
ray pull --file .env --template .env.example --base --strict --ci| Flag / Option | Type | Description |
|---|---|---|
-f, --file <path> | string | Path to the source or destination .env file (defaults to .env in current directory) |
--template <path> | string | Compare pulled secrets against a reference template file (e.g. .env.example) to identify missing keys |
-b, --base(-t, --team) | boolean | Fetch canonical team environment secrets, ignoring personal workstation overrides (recommended for CI/CD) |
--strict(--exit-code) | boolean | Abort execution immediately with an exit code if any required secret is missing, empty, or drifting |
--ci | boolean | Optimize execution for headless CI/CD runners (disables spinners, prompts, colors, and update checks) |
6. Exit Codes Reference
Ray CLI uses deterministic exit codes to help CI runners handle errors gracefully:
| Code | Meaning | Recommended Action |
|---|---|---|
| 0 | Success. The command completed with no errors. | None required. |
| 1 | Runtime or execution failure. A child process returned non-zero status or a general error occurred. | Inspect process output or run with --verbose. |
| 2 | Authentication failed. Token is missing, expired, or invalid. | Run `ray login` or update `RAY_TOKEN`. |
| 3 | Configuration or binding error. Project or environment context was not found. | Run `ray init` or specify `--project` and `--env`. |
| 4 | Network or API failure. Failed to reach the Ray Secrets vault. | Check internet connection or run `ray doctor`. |
7. Troubleshooting CI Failures
Authentication Failure (Exit Code 2)
- Problem: Pipeline fails with
Error: Token is invalid or expired. - Cause: The
RAY_TOKENsecret contains leading/trailing whitespace, newlines, or was revoked in the Developer Console. - Fix: Re-generate the Service Token under Workspace Management > Access Tokens in the Developer Console (or the Tokens sidebar in Ray Secrets) and paste it directly into your CI secret manager without newline characters.
Project Configuration Not Found (Exit Code 3)
- Problem: Pipeline fails with
Error: Project configuration not found. - Cause: The CI runner checked out a fresh workspace without a committed
.ray/workspace.json. - Fix: Commit
.ray/workspace.jsonto version control, or pass explicit project and environment flags to the CLI:ray run --env production --base --project proj_12345 --strict --ci -- npm test
Strict Mode Failure
- Problem: Pipeline fails with
Error: Missing required secret in strict mode. - Cause:
--strictwas specified, and one or more variables referenced in the project are unset in the environment vault. - Fix: Add the missing secret in the Developer Console under the target environment, or verify if the secret key was renamed.
Ready to eliminate plaintext secrets?
Create a workspace to organize environments, invite your team, and generate scoped Service Tokens for CI/CD.