game-ci / unity-builder

Build Unity projects for different platforms
https://github.com/marketplace/actions/unity-builder
MIT License
845 stars 244 forks source link

Add docker mirrors support #653

Open JSBmanD opened 4 months ago

JSBmanD commented 4 months ago

Context

Currently there's no possibility to change list of docker mirrors

Suggested solution

Will be nice to have variable to set list of mirrors for docker

GabLeRoux commented 4 months ago

Hi, I did not know about this before reading this issue. Here is a Stack Overflow post requesting for Docker registry mirrors

Can you elaborate on why we should implement this?

These are honest questions by the way, I am mostly curious to know how having mirrors could benefit the different actions. I suppose that this feature would probably have to reach the different other related github actions too.

Possible Implementation details

In the case where someone would be interested in implementing this, my understanding is that we would have to update these files:

class Input {
  // ... existing code ...

  static get dockerRegistryMirrors(): string[] {
    const input = Input.getInput('dockerRegistryMirrors');
    if (!input) {
      return [];
    }

    try {
      return yaml.load(input) as string[];
    } catch (e) {
      core.error(`Failed to parse dockerRegistryMirrors input: ${e}`);
      return [];
    }
  }

  // ... existing code ...
}

Somewhere in here: https://github.com/game-ci/unity-builder/blob/461ecf7cea76a46f6a2543d84a7d4f498a91dba2/src/model/docker.ts#L35-L89:

We would have to add something like this:

    const registryMirrors = Input.dockerRegistryMirrors.map(mirror => `--registry-mirror=${mirror}`).join(' ');

    return `docker run \
            ${registryMirrors} \
            // ...

And maybe for the windows based docker run command too?

docker.ts

And then, could be used with something like this in the workflow file:

      - name: Build with Docker
        run: |
          # Your build steps here
        with:
          dockerRegistryMirrors: |
            - https://mirror1.example.com
            - https://mirror2.example.com

If this is absolutely necessary, don't forget that you can fork the repository, update the code and try it 👍

JSBmanD commented 4 months ago

Hi! @GabLeRoux

Do you have any use case or specific reasons that makes it better?

Sure, as the first use case - recently some political games happened and docker hub restricted access to machines from Russia where my runner is located. For sure it doesn't anyhow restrict the access as I can enable proxy/vpn on it. Outside of the game-ci people using mirrors like Google's one or any other but here it's not much flexibility so it will be great to see it. Another reason if someone needs custom docker registry.

Have you encountered problems because of the lack of mirrors so far?

Yes, as my runner is located in Russia.

webbertakken commented 4 months ago

Instead of "dockerRegistryMirrors" we should probably consider naming this "dockerRegistry", as the protocol is the same across registries, and I'm assuming the mirror is a custom registry and not an actual mirror (correct me if I'm wrong).

Have you encountered problems because of the lack of mirrors so far?

Yes, as my runner is located in Russia.

That seems like a good enough reason to support this to me.

By the way, have you considered using customImage parameter? Any reason why that wouldn't work as expected?

JSBmanD commented 4 months ago

@webbertakken as I understood, customImage is only for specifying images not full url?

GabLeRoux commented 4 months ago

Technically speaking, we can use customImage to specify an image with the registry url. It is not the same as a mirror, but it does allow you to use a different registry. This is indeed a good alternative which already works right now.

I'm not exactly sure how we could use images on a private registry as a login must happen prior to running docker commands, but there's probably a way for that too.

Here are some examples to illustrate how this can be done:

Registry Image Name Format
Docker Hub unityci/editor:ubuntu-2023.2.20f1-base-3.1.0
GitHub Container Registry (ghcr.io) ghcr.io/unityci/editor:ubuntu-2023.2.20f1-base-3.1.0

Examples using command line

Note: this is only an example, we do not have images hosted on ghcr.io.