# GitLab CI/CD and deployment — ACE services (gitlab.qiwa.tech)

Read this before advising on branching, merge requests, deployments or pipeline failures.
Use the `GitLab` tool's `deploy_check`, `get_pipeline` and `get_job_log` actions instead of guessing.

## Where things live

| What | Path |
|---|---|
| ACE services | `ibm/qiwa-services/<module>/<service>` (one repo per service) |
| Module library | `ibm/qiwa-services/<module>/shared-lib` |
| Cross-module library | `ibm/qiwa-services/common-lib` |
| Pipeline templates | `ibm/qiwa-services/common-cicd` (old flow: `cicd-template/`; v2: `cicd-templates/service/`) |
| GitOps state (what runs where) | `ibm/argocd/ace-deployments` |
| Legacy (pre-container) repos | `ibm-integration/app-connect/*` — default branch `master` |

Module name used by CI = last segment of the project namespace with `_` → `-`.
Service name = project path, lower-cased.

Environments: `staging`, `stg-oci`, `preprod`, `uat-oci`, `prod`.

## Two pipeline flows coexist — check which one a repo uses

`deploy_check` detects it from `.gitlab-ci.yml`. Never give old-flow advice for a v2 repo or vice versa.

### Old flow — branch per environment (most repos)
`.gitlab-ci.yml` includes `cicd-template/.gitlab-ci-ace-template-*.yml` (5 includes, often `ref: $CI_COMMIT_BRANCH`).

- The branch **is** the environment: `stage` → staging, `stg-oci`, `preprod`, `uat-oci`, `main`/`master` → prod.
- Developer workflow: create a branch **from the target environment branch**, commit, push, open an MR into that branch. Merging runs the pipeline that deploys.
- Promotion order: `stage` → `preprod` → `main`. Repos that include `validate-merge-request-file.yml` reject other sources (and require the AI-review include with `AI_REVIEW_VERSION`).
- Jobs are tagged `build-ace-runner-$CI_COMMIT_BRANCH`: a pipeline on a ticket/personal branch sits "stuck, no matching runner". Expected — not an error to fix.
- `shared-lib`, `common-lib` and `common-cicd` are cloned at the **same branch name**. Anything another team merged into `shared-lib@preprod` ships with your service's next preprod build.
- Typical mistakes: branching from the wrong environment (branch far ahead/behind its target — promotes other people's unreviewed commits or reverts newer ones); MR into preprod/main from a ticket branch; forgetting that a lib change on the env branch affects every service.

### v2 flow — feature/release branches, tagged libraries
`.gitlab-ci.yml` includes `cicd-templates/service/.gitlab-ci-ace-template.yml` from `common-cicd` at a version ref (e.g. `v2.2.2`).

- Branches: `feature/*`, `bugfix/*`, `main`, `release/X.Y.Z`, `hotfix/X.Y.Z` (validated: `release/1.2.0` or `release/v1.2.0`, optional `-suffix`).
- `build.properties` pins library **tags**: `shared-lib-version=vX.Y.Z`, `common-lib-version=vX.Y.Z`, plus the library folder names in `shared-lib=` / `common-lib=` and the app folders in `applicationName=`.
- Pipeline: validate → prepare workspace → build BAR → build image → manual `promote-to-stg-oci` / `promote-to-preprod` / `promote-to-uat-oci` / `promote-to-production` → finalize (`tag-and-merge-request`).
- Production only from `release/*` or `hotfix/*`. The staging deploy stage is switched off in common-cicd v2.2 (stg-oci replaces it).
- Each promote job opens an MR in `ace-deployments` (`deploy-<service>-<env>-<timestamp>`) updating `modules/<module>/services/<service>/values-<env>.yaml`; ArgoCD syncs after the MR is approved and merged. Rollback = revert that MR.
- Library configs must be promoted **before** the service: `modules/<module>/configmaps/<env>/shared-lib-config-<ver>.yaml` and `common/<env>/common-lib-config-<ver>.yaml` (stg-oci uses `common/staging`). Lib teams publish configs by tagging the lib repo (auto to staging) and running manual promote jobs per environment.
- The stg-oci and uat-oci promote jobs do **not** stop on missing lib configs (`|| true`) — the service deploys without them. Always run `deploy_check` first.

## Migrating a service from the old flow to v2
Run `migration_check` with `project=<service>` (or `group=<module>` for the module overview). If the user is migrating for one environment first (e.g. "move it to v2 on stg-oci"), pass `environment` — the pins, `build.properties` and steps then match that environment; a full-migration result must not be reused for it. Then:

- **One file for every environment.** v2 builds all environments from the same `build.properties`. Old env branches often differ (e.g. `additional=` absent on stage, `=100` on main). For a trial, keep the target environment's values; before the release, decide what prod runs with.
- **Library pins per environment.** The tool shows, per tag, how many files differ from what each environment builds today (old OCI builds use lib@stg-oci/uat-oci, falling back to stage/preprod) and where configs are published. A trial pin must have configs in the target environment; a release pin must work on the whole stg-oci → preprod → uat-oci → prod path.
- **Image build on feature branches is manual.** Only release/hotfix branches build the image automatically; on `feature/*` the user clicks build-docker-image before promote-to-<env>.
- **Old OCI pipeline.** stg-oci/uat-oci branches with `oci-pipeline.yml` deploy with `oc apply`. After ArgoCD owns the runtime nobody may push to that branch. If `oci-runtimes/<env>/…/<service>.yaml` exists in ace-deployments, a v2 deploy creates a second ArgoCD app for the same runtime — the owners must remove one.
- **Branching rule: main is the source of truth.** Every v2 branch is created from `main` (prod), then takes the code of the target environment's own branch with `git restore` — not a merge, so there are no conflicts: a stg-oci trial is `git checkout main` → `git checkout -b feature/migrate-v2-stg-oci` → `git restore --source=origin/stg-oci --staged --worktree -- .`. Never branch from, take code from, or suggest another environment's branch (a stg-oci trial does not touch `stage`). If the target environment has no branch, ask the user which one to use. A release is `release/X.Y.Z` from `main` with `preprod`'s code restored onto it. `migration_check` lists the files that will differ from `main` and warns about any that prod changed after the branches split (prod changes the restore would replace).
- **Trials stay trials.** Never merge a v2 trial branch into the old env branches; plan production with `migration_check` without environment.

- **Library pins.** The old flow builds with `shared-lib@<env branch>`. Pin the tag `migration_check` proposes; if it says no tag matches, tell the user the service would build with different library code than today and that the library team should tag the branch. A module whose shared-lib has no tags cannot migrate yet — that is the library team's job, not the service developer's.
- **Customisations.** v2 ignores `custom-build/ACE_Deployment.yaml`. The first v2 deploy to each environment writes generic `values-<env>.yaml` defaults in the ace-deployments MR it opens. The user must add the listed overrides (resources, HPA, read-only DB secrets, namespace…) to that MR before it is approved, or they are lost. Settings marked "not expressible" (ACE version pin, env vars, extra ConfigMaps) need the ace-deployments owners.
- **custom-build files** other than `MQEndpointPolicy.policyxml` and `server.conf.yaml` (e.g. `MCR_MQEndpointPolicy.policyxml`) are not applied by v2.
- **Schedulers** must set `SERVICE_TYPE: "scheduler"` in `.gitlab-ci.yml`; otherwise the first prod deploy creates 2 replicas with HPA in namespace `ace` and scheduled jobs run twice. Many already-migrated schedulers lack it — `migration_check`/`deploy_check` flag it.
- **Prod hotfixes** are kept automatically because the branch starts from `main`; they will also run in the trial environment.
- **Applying it.** The tool is read-only. When the user wants the change made: clone the repo (or use their checkout), `git checkout main && git pull && git checkout -b <new branch>`, `git restore --source=origin/<target env branch> --staged --worktree -- .` (in the repo root; no merge, no conflicts), write the proposed `.gitlab-ci.yml` and `build.properties` with the file tools, and show the diff. Commit and push only when the user asks; run `deploy_check` after the push (it reads GitLab, not the local clone).
- **Shell working directory persists.** After `cd <repo>` once, later commands already run inside it — don't `cd <repo>` again; prefer `git -C <absolute repo path>`.
- After the first prod deploy on v2, the old environment branches must stop receiving merges.

## Before any deployment
Run `deploy_check` with the service, the branch and the environment. Relay every ❌ as blocking and every ⚠️ as something to review. Do not tell the user it is safe to deploy while there are ❌ items.

## When a pipeline fails
1. `list_pipelines` (state=failed) or take the pipeline id from the user, then `get_pipeline`.
2. Relay the diagnosis **and whose fix it is**:
   - **developer** — code, build.properties, branch name, MR source/target, `.gitlab-ci.yml`.
   - **lib-team** — library version not promoted to the environment.
   - **cicd-template** — a bug in common-cicd (e.g. `tag-and-merge-request` jq error, ace-deployments MR not created). Report to the common-cicd maintainers; do not loop on re-runs.
   - **infrastructure** — registry 504, artifacts 500, cluster API timeout, App Connect webhook down, runner IP blocked after failed logins. Retry later or escalate; do not change code.
3. For `build-bar-file` failures the real cause is in the `workspace/<service>.log` artifact; `get_pipeline`/`get_job_log` extract the files mqsicreatebar rejected. Open those files and look for unresolved references (a module/schema/routine whose library is not listed in `build.properties`) or syntax errors.
4. Note: `prepare-workspace.sh` never fails on a missing library folder — it prints `cp: cannot stat` and continues. A wrong library name in `build.properties` therefore surfaces only later, as a BAR compilation failure.

## After the merge: ArgoCD (what is actually running)

Use the `ArgoCD` tool (the user connects it with `/argocd`). The server at
`openshift-gitops-server-openshift-gitops.apps.np-jed-ocp-stg.qiwa.info` manages **stg-oci only**;
other environments run on other clusters this tool is not connected to.

- App names are `ace-<module code>-<service>-<env>` (e.g. `ace-chgo-choccsubmits-stg-oci`). The module
  code is not the GitLab module name, so pass `service` (+ `environment`) and let the tool find the app.
- "Is it deployed?" → `deploy_status` (service + environment): latest ace-deployments MR → ArgoCD synced
  that commit → pods rolled out. Relay the verdict and whose move it is.
- "Is it up / why is it failing?" → `pods` (restarts, OOMKilled, CrashLoopBackOff, rollout state,
  IntegrationRuntime), then `events`. Pod **logs are not available** through Orbit (the dev ArgoCD role
  has no `logs` permission and the OpenShift API is not reachable over the VPN): send the user to the
  OpenShift console for logs.
- OutOfSync → `diff` to see what differs, `refresh` to re-read git. Auto-synced apps normally need no
  manual sync; `sync` (asks the user, `-stg-oci` apps only) is for apps without auto-sync or a stuck sync.
- Rollback is still "revert the deployment MR in ace-deployments", never a sync to an old revision.

## The GitLab tool is read-only
It never pushes, merges, approves, runs jobs or edits ace-deployments. Tell the user exactly which MR to open (source → target), which manual job to click, or whom to contact.
