devcontainers / ci

A GitHub Action and Azure DevOps Task designed to simplify using Dev Containers (https://containers.dev) in CI/CD systems.
MIT License
333 stars 51 forks source link

Support for Docker build arguments #270

Closed lukin0110 closed 8 months ago

lukin0110 commented 9 months ago

To test a python package I'm using a matrix build and I want to be able to build different devcontainer images for different python versions. In order to that I need to be able to specify the python version at build time.

Possible example syntax:

jobs:
  test:
    runs-on: ubuntu-latest
    permissions: write-all
    strategy:
      matrix:
        python-version: [ "3.10", "3.11" ]
    name: Python ${{ matrix.python-version }}
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Build DevContainer
        uses: devcontainers/ci@v0.3
        with:
          buildArgs:
            PYTHON_VERSION: ${{ matrix.python-version }}
          imageName: ghcr.io/${{ github.repository }}
          cacheFrom: ghcr.io/${{ github.repository }}:${{ matrix.python-version }}
          imageTag: ${{ matrix.python-version }}
          push: always

      ...

I can open a merge request to implement this solution if we agree on the approach.

huxuan commented 9 months ago

+1 for almost the same requirements.

huxuan commented 9 months ago

Seems this can be done with a combination of build.args in devcontainer.json and env in GitHub Actions. Related code snippets are like the following:

{
    "build": {
        "args": {
            "PYTHON_VERSION": "${localEnv:PYTHON_VERSION}"
        },
    }
}
      - uses: devcontainers/ci@v0.3
        env:
          PYTHON_VERSION: ${{ matrix.python-version }}
        with:
          imageName: ghcr.io/${{ github.repository }}
          imageTag: ${{ matrix.python-version }}

I have an ongoing pull request for this: https://github.com/serious-scaffold/ss-python/pull/263

It is partialy mentioned here (https://github.com/devcontainers/ci/blob/main/docs/github-action.md#remoteenv), but not so straightforward. Maybe we should improve the documentation.

lukin0110 commented 8 months ago

@huxuan thanks for the suggestion. I've applied your fix and it works as expected. I'm using docker compose in the devontainer.json but the principle is the same.

It does make sense to manage build args through the devcontainer.json config file. It's not the responsibility of this GitHub Action. I'm closing the issue.