diff --git a/.github/workflows/build-and-push.yml b/.github/workflows/build-and-push.yml new file mode 100644 index 00000000..51d7ca92 --- /dev/null +++ b/.github/workflows/build-and-push.yml @@ -0,0 +1,78 @@ +name: Build and push images + +on: + push: + branches: [develop, main, 731-create-build-and-push-workflow] + pull_request: + branches: [develop, main, 731-create-build-and-push-workflow] + +jobs: + docker-build: + runs-on: ubuntu-latest + strategy: + matrix: + include: + - image: ghcr.io/button-inc/iot-system-prototype/app + context: . + dockerfile: app/Dockerfile + name: app + - image: ghcr.io/button-inc/iot-system-prototype/api + context: . + dockerfile: api/Dockerfile + name: api + - image: ghcr.io/button-inc/iot-system-prototype/real_fake_sensors + context: . + dockerfile: real_fake_sensors/Dockerfile + name: realFakeSensors + - image: ghcr.io/button-inc/iot-system-prototype/sensational_sensors + context: . + dockerfile: sensational_sensors/Dockerfile + name: sensationalSensors + name: Build ${{ matrix.name }} docker image + steps: + - uses: actions/checkout@v3 + - name: Set up Docker Buildx + id: buildx + uses: docker/setup-buildx-action@v2 + with: + install: true + - name: Docker meta + id: meta + uses: docker/metadata-action@v3 + with: + images: ${{ matrix.image }} + tags: | + type=sha,format=long,prefix= + latest + type=ref,event=pr + - name: Login to GitHub Container Registry + uses: docker/login-action@v2 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + - name: Cache Docker layers + uses: actions/cache@v3 + with: + path: /tmp/.buildx-cache + key: ${{ runner.os }}-buildx-${{ matrix.name }}-${{ github.sha }} + restore-keys: | + ${{ runner.os }}-buildx-${{ matrix.name }} + - name: Build image + uses: docker/build-push-action@v3 + with: + context: ${{ matrix.context }} + builder: ${{ steps.buildx.outputs.name }} + push: true + file: ${{ matrix.dockerfile }} + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + cache-from: type=local,src=/tmp/.buildx-cache + cache-to: type=local,dest=/tmp/.buildx-cache-new + # Temp fix + # https://github.com/docker/build-push-action/issues/252 + # https://github.com/moby/buildkit/issues/1896 + - name: Move cache + run: | + rm -rf /tmp/.buildx-cache + mv /tmp/.buildx-cache-new /tmp/.buildx-cache