Build and deploy

Deploy an app from CI

All help topics

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.

  1. On your computer, make the key: icp identity new ci. Then icp identity principal --identity ci prints its principal.
  2. Add that principal as a controller of each canister in your icp.yaml, as the identity you deploy with, for example icp 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.
  3. Run icp identity export ci and store its output as the secret ICP_CI_PEM in your CI. On GitHub: Settings → Secrets and variables → Actions → New repository secret.
  4. Commit .icp/data/mappings/ic.ids.json. CI upgrades only the canisters that this file lists.
  5. Add a workflow file, for example .github/workflows/deploy.yml. This one deploys on each v* 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-create
Good 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-create stops the deploy if a canister is not in ic.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 variable ICP_CI_PEM and 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 the deploy job, 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 for frontend. With set -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 with icp canister snapshot create, then start it again. To roll back, stop it, run icp canister snapshot restore, then icp canister start.
  • The step deletes the key file and the imported ci identity when it ends, also after a failed deploy, so a self-hosted runner keeps no copy of the key. If the runner already has a ci identity, the import stops the step and leaves that identity alone.

More about the same page

Still stuck? Contact support