[GH-ISSUE #619] Add an example to run the image in .gitlab-ci.yml #107

Open
opened 2026-03-03 12:08:21 +03:00 by kerem · 5 comments
Owner

Originally created by @sorasful on GitHub (Nov 10, 2021).
Original GitHub issue: https://github.com/fsouza/fake-gcs-server/issues/619

Hello,

As I'm starting to use fake-gcs-server for one of my project, I'm trying to setup a service in my .gitlab-ci.yml file. So far I have some troubles to set it up. But once I figure it out, what would you think about adding a section in the documentation to show the configuration to be able to run it in Gitlab-CI ?

Originally created by @sorasful on GitHub (Nov 10, 2021). Original GitHub issue: https://github.com/fsouza/fake-gcs-server/issues/619 Hello, As I'm starting to use fake-gcs-server for one of my project, I'm trying to setup a service in my .gitlab-ci.yml file. So far I have some troubles to set it up. But once I figure it out, what would you think about adding a section in the documentation to show the configuration to be able to run it in Gitlab-CI ?
Author
Owner

@fsouza commented on GitHub (Nov 11, 2021):

@sorasful hey, yeah we could add that to the documentation x)

Let me know if I can help in any way.

<!-- gh-comment-id:966671577 --> @fsouza commented on GitHub (Nov 11, 2021): @sorasful hey, yeah we could add that to the documentation x) Let me know if I can help in any way.
Author
Owner

@sorasful commented on GitHub (Nov 11, 2021):

@fsouza Yeah, I think that a lot of people use this for their projects in their CI, I'm personnally using Gitlab-ci so I thought this would be a good idea. Could be great for other platforms too, but let's open an issue when that will be needed.

Also, I didn't manage to make it work yet, I have some troubles with :

Health check container logs:
2021-11-10T19:27:58.202405563Z FATAL: No HOST or PORT found           

And my Gitlab-ci looks like this :

image: python:3.9

services:
  - name: fsouza/fake-gcs-server
    alias: gcloud-emulator

variables:
  STORAGE_EMULATOR_HOST: gcloud-emulator:4443

... rest of the file
<!-- gh-comment-id:966674755 --> @sorasful commented on GitHub (Nov 11, 2021): @fsouza Yeah, I think that a lot of people use this for their projects in their CI, I'm personnally using Gitlab-ci so I thought this would be a good idea. Could be great for other platforms too, but let's open an issue when that will be needed. Also, I didn't manage to make it work yet, I have some troubles with : ``` Health check container logs: 2021-11-10T19:27:58.202405563Z FATAL: No HOST or PORT found ``` And my Gitlab-ci looks like this : ```yml image: python:3.9 services: - name: fsouza/fake-gcs-server alias: gcloud-emulator variables: STORAGE_EMULATOR_HOST: gcloud-emulator:4443 ... rest of the file ```
Author
Owner

@sorasful commented on GitHub (Nov 12, 2021):

Okay, so despite the warning saying No HOST or PORT found it seems that the service starts well and is working. Now just need to find out how to add volumes.

<!-- gh-comment-id:967061801 --> @sorasful commented on GitHub (Nov 12, 2021): Okay, so despite the warning saying `No HOST or PORT found` it seems that the service starts well and is working. Now just need to find out how to add volumes.
Author
Owner

@matthewbal commented on GitHub (Jun 14, 2023):

I'm also trying to get this to work, I'll post my gitlab-ci when I manage to get it connecting properly.

<!-- gh-comment-id:1591178779 --> @matthewbal commented on GitHub (Jun 14, 2023): I'm also trying to get this to work, I'll post my gitlab-ci when I manage to get it connecting properly.
Author
Owner

@matthewbal commented on GitHub (Jun 15, 2023):

We have a FastAPI project, using the async google cloud storage library gcloud.aio.storage.
This is what I had to do:

in gitlab-ci.yml

pytest-job:
  services:
    - name: fsouza/fake-gcs-server
      command: ["-scheme", "http"]
  stage: test
  script:
    - echo "Your test script"

Then, when you want to connect to the container, you need to reference the hostname like this (gitlab by default maps service names to hostnames and replaces / with -)

BUCKET_EMULATOR_URL: str = (
    "http://fsouza-fake-gcs-server:4443"
)

You can't use volumes in gitlab, so there's no easy way to create your bucket and test files, so I used a pytest autouse fixture to do that.

@pytest.fixture(scope="session", autouse=True)
async def add_test_txt_to_test_bucket():
    """
    Guarentees that we have test.txt uploaded so we can easily test it in gitlab
    """
    async with Session() as session:
        storage = Storage(
            service_file=settings.GCP_BUCKET_CREDS_LOCATION,
            session=session,
            api_root=settings.BUCKET_EMULATOR_URL,
        )

        test_bucket_config = {
            "name": "fake-bucket",
            "location": "US-CENTRAL1",
            "storageClass": "NEARLINE",
            "iamConfiguration": {
                "uniformBucketLevelAccess": {"enabled": True},
            },
        }

        async with AsyncClient() as client:
            await client.post(url=f"{settings.BUCKET_EMULATOR_URL}/storage/v1/b", json=test_bucket_config)

        await storage.upload(
            bucket="fake-bucket",
            object_name="test.txt",
            file_data="This file is served from our fake-bucket locally",
        )

Now from tests we are able to connect to the bucket fake-bucket, and the file test.txt is already loaded onto it.
Locally, if you use docker compose, you would need to connect with http://localhost:4443 from outside the docker stack, but from within the docker stack you can also use http://fsouza-fake-gcs-server:4443 as long as you set the service name up that way.

Here's a docker-compose for completeness:

services:
  api_dev:
    build:
      context: .
      dockerfile: Dockerfile
    volumes:
      - ./:/code/ # ALL config files get loaded into the container
    ports:
      - 9090:8080
    env_file:
      - .docker-api.env
    depends_on:
      - fsouza-fake-gcs-server
  fsouza-fake-gcs-server:
    image: fsouza/fake-gcs-server
    ports:
      - 4443:4443
    command: -scheme http

Hope this helps someone!

<!-- gh-comment-id:1592916192 --> @matthewbal commented on GitHub (Jun 15, 2023): We have a FastAPI project, using the async google cloud storage library gcloud.aio.storage. This is what I had to do: in gitlab-ci.yml ``` pytest-job: services: - name: fsouza/fake-gcs-server command: ["-scheme", "http"] stage: test script: - echo "Your test script" ``` Then, when you want to connect to the container, you need to reference the hostname like this (gitlab by default maps service names to hostnames and replaces / with -) ``` BUCKET_EMULATOR_URL: str = ( "http://fsouza-fake-gcs-server:4443" ) ``` You can't use volumes in gitlab, so there's no easy way to create your bucket and test files, so I used a pytest autouse fixture to do that. ``` @pytest.fixture(scope="session", autouse=True) async def add_test_txt_to_test_bucket(): """ Guarentees that we have test.txt uploaded so we can easily test it in gitlab """ async with Session() as session: storage = Storage( service_file=settings.GCP_BUCKET_CREDS_LOCATION, session=session, api_root=settings.BUCKET_EMULATOR_URL, ) test_bucket_config = { "name": "fake-bucket", "location": "US-CENTRAL1", "storageClass": "NEARLINE", "iamConfiguration": { "uniformBucketLevelAccess": {"enabled": True}, }, } async with AsyncClient() as client: await client.post(url=f"{settings.BUCKET_EMULATOR_URL}/storage/v1/b", json=test_bucket_config) await storage.upload( bucket="fake-bucket", object_name="test.txt", file_data="This file is served from our fake-bucket locally", ) ``` Now from tests we are able to connect to the bucket `fake-bucket`, and the file `test.txt` is already loaded onto it. Locally, if you use docker compose, you would need to connect with `http://localhost:4443` from outside the docker stack, but from within the docker stack you can also use `http://fsouza-fake-gcs-server:4443` as long as you set the service name up that way. Here's a docker-compose for completeness: ``` services: api_dev: build: context: . dockerfile: Dockerfile volumes: - ./:/code/ # ALL config files get loaded into the container ports: - 9090:8080 env_file: - .docker-api.env depends_on: - fsouza-fake-gcs-server fsouza-fake-gcs-server: image: fsouza/fake-gcs-server ports: - 4443:4443 command: -scheme http ``` Hope this helps someone!
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/fake-gcs-server#107
No description provided.