How do i run loops simultaneously in gitlab-ci?

I have the following script in my gitlab-ci and will like to run the loops same time, anyone knows a great way to do this? so that they both run at same time

NOTE the job is a manual job and am looking for a single button click to loop through all the packages in the bash script as shown below

    when: manual
    script:
        - |-
            for PACKAGE in name1 name2; do
                export IMAGE="$CI_REGISTRY/$GITLAB_REPO/$PACKAGE:${BUILD_TAG}"
                docker build -t ${IMAGE} -f $PACKAGE/Dockerfile .
                docker push ${IMAGE}
            done

currently it runs first for name1 and then after that is finished then runs for name2. I will like to run both at same exact time since there is no dependency

Here is what i tried from an answer on SO => (shell script - Parallelize a Bash FOR Loop - Unix & Linux Stack Exchange)

    when: manual
    script:
        - |-
            task(){
                export IMAGE="$CI_REGISTRY/$GITLAB_REPO/$1:${BUILD_TAG}"
                docker build -t ${IMAGE} -f $1/Dockerfile .
                docker push ${IMAGE}         
            }
            for PACKAGE in name1 name2; do
                task "$PACKAGE" &
            done

This works in regular bash script but when i used it with gitlab-ci, it doesnt run as expected and does not even run any of the commands and just succeeds the job instantly

Anyone willing to help on where the issue is and how to solve this issue?

Hi @gurumacanoob

I think you need a parallel matrix here. For example:

docker:
  stage: docker
  script:
    - export IMAGE="$CI_REGISTRY/$GITLAB_REPO/$PACKAGE:${BUILD_TAG}"
    - docker build -t ${IMAGE} -f $PACKAGE/Dockerfile .
    - docker push ${IMAGE}
  parallel:
    matrix:
      - PACKAGE: [name1, name2]

or something like that!

HTH,

Sarah

@snim2
This is a manual job, when i tried this, it gives me 2 buttons for each of the packages
how can i have just 1 button to then run through all the packages?

i need 1 button click to loop through all packages, i can have up to 10 packages at any given time

Hmm, manual jobs are a bit tricky. I’m not quite sure how I’d do this, and I’d be interested to hear ideas from others. However, the first thing that springs to mind, is to make the manual job a parent job, which starts a child job which runs the parallel:matrix of 10 jobs.