Give your CI its own key, make that key a controller of your app's canisters, then let the CI run icp deploy with it.
- On your computer, make the key:
icp identity new ci. Thenicp identity principal --identity ciprints its principal. - Add that principal as a controller of each canister in your
icp.yaml, as the identity you deploy with, for exampleicp canister settings update backend --add-controller <principal> --identity opencloud -e ic. For many canisters, run it in a loop:for c in backend frontend; do icp canister settings update "$c" --add-controller <principal> --identity opencloud -e ic; done. - Run
icp identity export ciand store its output as the secretICP_CI_PEMin your CI. On GitHub: Settings → Secrets and variables → Actions → New repository secret. - Commit
.icp/data/mappings/ic.ids.json. CI upgrades only the canisters that this file lists. - Add a workflow file, for example
.github/workflows/deploy.yml. This one deploys on eachv*tag:
name: Deploy
on:
push:
tags: ['v*']
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 22
- run: npm install -g @icp-sdk/icp-cli @icp-sdk/ic-wasm ic-mops
- run: rustup target add wasm32-unknown-unknown
- name: Deploy
env:
ICP_CI_PEM: ${{ secrets.ICP_CI_PEM }}
run: |
set -e
(umask 077 && set -C && printf '%s' "$ICP_CI_PEM" > ci.pem)
trap 'rm -f ci.pem' EXIT
icp identity import ci --from-pem ci.pem --storage plaintext
trap 'rm -f ci.pem; icp identity delete ci >/dev/null 2>&1 || true' EXIT
rm ci.pem
icp deploy --identity ci -e ic --no-createGood to know
- The workflow installs what the official Motoko and Rust recipes need. Add any other build tools of your project before the deploy.
- A Developer role alone cannot upgrade your canisters: it only creates canisters, and only a controller can upgrade one.
--no-createstops the deploy if a canister is not inic.ids.json, so CI never makes a second copy of your app. If CI must create canisters, replace the flag with--subnet <subnet-id>and your engine's subnet, and add the CI principal as a Developer on the engine's Team page.- GitHub does not pass a secret to a step by itself. The
env:line does it. On another CI, put the key in the variableICP_CI_PEMand run the same commands. - A repository secret is readable by every workflow on every branch. To deploy only from a release, store the key as an environment secret, protect that environment, and add
environment: <name>to thedeployjob, or the job cannot read the secret. - To control the order, replace the last line with one command for each canister, for example
icp deploy backend --identity ci -e ic --no-create, then the same command forfrontend. Withset -e, a command runs only if the one before it worked. - An upgrade is atomic for one canister: if the new code traps while it upgrades, the canister keeps its old code and data. Two canisters do not upgrade as one step, so keep each backend change compatible with the frontend that is still live.
- Before a risky upgrade, stop the canister with
icp canister stop, take a snapshot withicp canister snapshot create, then start it again. To roll back, stop it, runicp canister snapshot restore, thenicp canister start. - The step deletes the key file and the imported
ciidentity when it ends, also after a failed deploy, so a self-hosted runner keeps no copy of the key. If the runner already has aciidentity, the import stops the step and leaves that identity alone.