Build · Guide
CI/CD
Use the API from a pipeline: a workflow you can copy, and how to handle the token.
The token in a pipeline
- Create a token just for the pipeline, with only the scopes the job needs (usually
resource.readandresource.write). - Limit it to the project the pipeline works on.
- Give it a 90 day expiry and add the roll date to your calendar.
- Store it as a secret in your pipeline (
secrets.COLOSSIUM_API_TOKEN), never as a plain variable and never in the repository.
Hosted pipeline runners share a large and changing set of addresses, so an address allow list does not work for them. Rely on a narrow scope, a project limit, a short expiry and fast rotation instead. If you run your own runners at fixed addresses, use the allow list as well.
A pipeline that runs code from pull requests of untrusted forks must not receive this secret. Make sure only trusted branches and manual runs can use it.
A GitHub Actions workflow
This workflow asks for a database when you run it, and waits until it is ready. It uses the run id in the idempotency key, so re-running the job does not make a second database.
# .github/workflows/provision.yml
# Asks Colossium for a database when you run the workflow by hand, and waits until it is ready.
# The token lives in the repository's secrets and is never printed. Give it only the permissions this job needs.
name: Provision a database
on:
workflow_dispatch:
inputs:
project_id: { description: Project id, required: true }
name: { description: Database name (lower-case letters, numbers, hyphens), required: true }
permissions: {} # the job needs nothing from GitHub itself
jobs:
provision:
runs-on: ubuntu-latest
timeout-minutes: 30
env:
API: https://api.colossium.cloud/v1
COLOSSIUM_API_TOKEN: ${{ secrets.COLOSSIUM_API_TOKEN }} # a token with the resource.write scope only
steps:
- name: Ask for the database
id: ask
run: |
set -euo pipefail
# The idempotency key identifies this run, so a re-run of the job finds the same request instead of making another.
body=$(jq -n --arg p "${{ inputs.project_id }}" --arg n "${{ inputs.name }}" '{kind:"databases.create",project_id:$p,input:{name:$n,region:"eu"}}')
curl --fail-with-body -sS -X POST "$API/requests" \
-H "Authorization: Bearer $COLOSSIUM_API_TOKEN" -H "Content-Type: application/json" \
-H "Idempotency-Key: gha-${{ github.run_id }}-${{ inputs.name }}" -d "$body" -o request.json
echo "id=$(jq -r .id request.json)" >> "$GITHUB_OUTPUT"
- name: Wait for it
run: |
set -euo pipefail
for i in $(seq 1 180); do
state=$(curl --fail-with-body -sS "$API/requests/${{ steps.ask.outputs.id }}" -H "Authorization: Bearer $COLOSSIUM_API_TOKEN" | jq -r .state)
echo "state: $state"
case "$state" in
succeeded) exit 0 ;;
failed|cancelled) echo "The request ended as $state."; exit 1 ;;
esac
sleep 10
done
echo "Still waiting after 30 minutes. Check the request in the console."; exit 1
The same pattern works in any pipeline: take the token from a secret, send one request with a key built from the run, poll until a final state, fail the job if it ends as failed or cancelled, and set a timeout on the job.
Rules for a pipeline
| Rule | Why |
|---|---|
| Print nothing that came from the token | Pipeline logs are often visible to many people, and kept for a long time. |
Use set -euo pipefail in shell steps | So an error stops the job, and an unset variable is not silently empty. |
| Set a job timeout | A request waits for a person. Your pipeline should not wait forever. |
| Build the idempotency key from the run, not from the time | A rerun must mean the same thing. A new minute must not. |
Never echo Authorization, and avoid curl -v and set -x | They print headers, which means the token. |
Use --fail-with-body with curl | So an error response fails the step and still shows you the error. |
Rotating a pipeline token
- Roll the token in the console with a 1 hour grace period.
- Update the secret in your pipeline.
- Run the pipeline once and check that it succeeds.
- Let the old secret expire, or revoke it.
Because the old secret keeps working during the grace period, no run fails in between.
This page as MarkdownReport a problem with this pageOlympus Group Holdings, Inc.