How to use needs with parallel matrix (or another way to handle multi-step CI with needs)

I have a Gitlab-CI use case that I can’t properly figure out. The background is that I have multiple environments that need to have their own artifacts built and deployed. Let’s say a simplified case looks like this:

stages:
  - build
  - deploy

AppOne:Build:
  extends: .build
  environment:
    name: AppOne

AppOne:Deploy:
  extends: .deploy
  needs: ['AppOne:Build']
  environment:
    name: AppOne

AppTwo:Build:
  extends: .build
  environment:
    name: AppTwo

AppTwo:Deploy:
  extends: .deploy
  needs: ['AppTwo:Build']
  environment:
    name: AppTwo

.build:
  stage: build
  script:
    - build.sh

.deploy:
  stage: deploy
  when: manual
  script:
    - deploy.sh

The process above works, but as you can see it’s kind of long and repetitious with two apps and super simple process. The real process has three stages and up to 20 apps. I would like to know if there’s a better way to express this? The build processes are identical save for a variable (set in the environment).

I guess I mainly want to know if there’s a better way to express the “needs” without repeating it for each app, i.e. the app needs to be built before it can be manually deployed. I could do needs: ['${NAME}:Build'] but that requires the $NAME:Build step to actually exist.

Please help, oh collective wisdom of the Gitlab forums :slight_smile: