Search code examples
dockergithub-actionsdocker-registry

GitHub Actions: How to use matrix value inside a step


I have a simple pipeline in GitHub Actions. I'm trying to build and publish a Docker image to Docker Hub within the same. I would also like to build the image for different platforms (read: operating systems), hence I'm using strategy.matrix. This is how it looks like so far:

name: Build and Publish Docker image(s)

jobs:
  publish:
    runs-on: ubuntu-latest
    steps:
      - name: Check out repository
        uses: actions/checkout@v2
      - name: Log in to Docker Hub
        uses: docker/login-action@v1
        with:
          password: ${{ secrets.DOCKERHUB_TOKEN }}
          registry: docker.io
          username: ${{ secrets.DOCKERHUB_USERNAME }}
      - name: Build and push
        uses: docker/build-push-action@v2
        env:
          platform: ${{ matrix.platforms }}
        with:
          context: ./${platform}/
          push: true
          tags: oscarotero/lume:1.3.0-${platform} # TODO: Parameterize version once this is finally working
    strategy:
      matrix:
        platforms:
          - alpine
          - debian

on:
  push:
    branches:
      - main

This is currently failing because it doesn't recognize platform when running the Build and push step. I have a similar one and it works with this approach.

Anyone more knowledgeable in GitHub Actions can shred some light here? All I can think of is that docker/build-push-action@v2 doesn't support that, but in that case, how can I approach this problem?


Solution

  • Removing the environment variable and using the matrix values directly solved the problem:

    jobs:
      publish:
        runs-on: ubuntu-latest
        steps:
          - name: Check out repository
            uses: actions/checkout@v2
          - name: Log in to Docker Hub
            uses: docker/login-action@v1
            with:
              password: ${{ secrets.DOCKERHUB_TOKEN }}
              registry: docker.io
              username: ${{ secrets.DOCKERHUB_USERNAME }}
          - name: Build and push
            uses: docker/build-push-action@v2
            with:
              context: ./${{ matrix.platform }}/
              push: true
              tags: oscarotero/lume:1.3.0-${{ matrix.platform }}
        strategy:
          matrix:
            platform:
              - alpine
              - debian