How to use same container for different stages in gitlab pipeline?

I have three different stages, each should be running in a same container as there are many customizations and installations which can’t be processed or mapped to a new container.

  1. install the application and run the main logic(deploy command).
  2. Undeploy command.
  3. Clean up

If Stage 1 fails/success, then Stage 2 should still execute. And If Stage1/Stage2 fails then only stage 3 should execute.

As there is no option to use same container for three different stages, I thought to club all of them in a single stage also. But once the stage 1 portion failed, it is not proceeding to next steps.

If I keep allow_failure: true , even with necessary portion also, it is not failing.

How to implement this?

1 Like

Hi @uday.reddy3

Is it really the same container that you want to use or just the same image? If it’s really the same container, do you know which files you need to keep during all three stages of the pipeline?

Hi @snim2

It should be the same container actually.

Let me elaborate:
stage 1: (first container): builds the product rpm file and shares to stage 2 using artifact
stage 2: (second container): installation and configuration.
stage 3: (second container): product testing, just sharing artifacts won’t suffice, require so much configurations and installations at multiple locations. And stage to be separate, so that developers can distinguish where exactly it failed.
stage 4: (second container): if the product failed, cleanup should happen.

Not image, it should be same container.

OK, I’m with you. I think you need something like this:

stages:
    - build
    - install_and_test

build: 
    stage: build
    ...

install:
    stage: install_and_test
    needs: []
    ...

test:
    stage: install_and_test
    needs: ["install"]
    allow_failure: true

cleanup:
    stage: install_and_test
    needs: ["install", "test"]
    rules:
        when: always

The docs for the needs keyword are here. Note:

1 Like

But all these stages will create a new container for each stage. But need stage 2 and 3, same container (not just image). And cleanup should run only when the install_and_test stage fails. Can you tell how to do that also

I have the same problem, needs clause doesn’t solve it…jobs from same stage are executed into different container.
Has anyone addressed the situation in any way?