version: 2.1

workflows:
  build:
    jobs:
      - linux-arm64-glibc-node-18:
          filters:
            tags:
              only: /^v.*/
      - linux-arm64-musl-node-18:
          filters:
            tags:
              only: /^v.*/
      - linux-arm64-glibc-node-20:
          filters:
            tags:
              only: /^v.*/
      - linux-arm64-musl-node-20:
          filters:
            tags:
              only: /^v.*/

jobs:
  linux-arm64-glibc-node-18:
    resource_class: arm.medium
    machine:
      image: ubuntu-2004:current
    steps:
      - checkout
      - run: |
          sudo docker run -dit --name sharp --volume "${PWD}:/mnt/sharp" --workdir /mnt/sharp arm64v8/debian:bullseye
          sudo docker exec sharp sh -c "apt-get update && apt-get install -y build-essential git python3 curl fonts-noto-core"
          sudo docker exec sharp sh -c "mkdir -p /etc/apt/keyrings"
          sudo docker exec sharp sh -c "curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg"
          sudo docker exec sharp sh -c "echo 'deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_18.x nodistro main' | tee /etc/apt/sources.list.d/nodesource.list"
          sudo docker exec sharp sh -c "apt-get update && apt-get install -y nodejs"
      - run: sudo docker exec sharp sh -c "npm install --build-from-source"
      - run: sudo docker exec sharp sh -c "npm test"
      - run: |
          sudo docker exec sharp sh -c "npm run package-from-local-build"
          sudo docker exec sharp sh -c "npm pkg set \"optionalDependencies.@sharpen/sharp-linux-arm64=file:./npm/linux-arm64\""
          sudo docker exec sharp sh -c "npm run clean"
          sudo docker exec sharp sh -c "npm install --ignore-scripts"
          sudo docker exec sharp sh -c "npm test"
      - run: "[[ -n $CIRCLE_TAG ]] && sudo docker exec --env prebuild_upload sharp sh -c \"cd src && ln -s ../package.json && npx prebuild --upload=$prebuild_upload\" || true"
  linux-arm64-glibc-node-20:
    resource_class: arm.medium
    machine:
      image: ubuntu-2004:current
    steps:
      - checkout
      - run: |
          sudo docker run -dit --name sharp --workdir /mnt/sharp arm64v8/debian:bullseye
          sudo docker exec sharp sh -c "apt-get update && apt-get install -y build-essential git python3 curl fonts-noto-core"
          sudo docker exec sharp sh -c "mkdir -p /etc/apt/keyrings"
          sudo docker exec sharp sh -c "curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg"
          sudo docker exec sharp sh -c "echo 'deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_20.x nodistro main' | tee /etc/apt/sources.list.d/nodesource.list"
          sudo docker exec sharp sh -c "apt-get update && apt-get install -y nodejs"
          sudo docker exec sharp sh -c "mkdir -p /mnt/sharp"
          sudo docker cp . sharp:/mnt/sharp/.
      - run: sudo docker exec sharp sh -c "npm install --build-from-source"
      - run: sudo docker exec sharp sh -c "npm test"
      - run: |
          sudo docker exec sharp sh -c "npm run package-from-local-build"
          sudo docker exec sharp sh -c "npm pkg set \"optionalDependencies.@sharpen/sharp-linux-arm64=file:./npm/linux-arm64\""
          sudo docker exec sharp sh -c "npm run clean"
          sudo docker exec sharp sh -c "npm install --ignore-scripts"
          sudo docker exec sharp sh -c "npm test"
  linux-arm64-musl-node-18:
    resource_class: arm.medium
    machine:
      image: ubuntu-2004:current
    steps:
      - checkout
      - run: |
          sudo docker run -dit --name sharp --volume "${PWD}:/mnt/sharp" --workdir /mnt/sharp node:18-alpine3.17
          sudo docker exec sharp sh -c "apk add build-base git python3 font-noto --update-cache"
      - run: sudo docker exec sharp sh -c "npm install --build-from-source"
      - run: sudo docker exec sharp sh -c "npm test"
      - run: |
          sudo docker exec sharp sh -c "npm run package-from-local-build"
          sudo docker exec sharp sh -c "npm pkg set \"optionalDependencies.@sharpen/sharp-linuxmusl-arm64=file:./npm/linuxmusl-arm64\""
          sudo docker exec sharp sh -c "npm run clean"
          sudo docker exec sharp sh -c "npm install --ignore-scripts"
          sudo docker exec sharp sh -c "npm test"
      - run: "[[ -n $CIRCLE_TAG ]] && sudo docker exec --env prebuild_upload sharp sh -c \"cd src && ln -s ../package.json && npx prebuild --upload=$prebuild_upload\" || true"
  linux-arm64-musl-node-20:
    resource_class: arm.medium
    machine:
      image: ubuntu-2004:current
    steps:
      - checkout
      - run: |
          sudo docker run -dit --name sharp --workdir /mnt/sharp node:20-alpine3.18
          sudo docker exec sharp sh -c "apk add build-base git python3 font-noto --update-cache"
          sudo docker exec sharp sh -c "mkdir -p /mnt/sharp"
          sudo docker cp . sharp:/mnt/sharp/.
      - run: sudo docker exec sharp sh -c "npm install --build-from-source"
      - run: sudo docker exec sharp sh -c "npm test"
      - run: |
          sudo docker exec sharp sh -c "npm run package-from-local-build"
          sudo docker exec sharp sh -c "npm pkg set \"optionalDependencies.@sharpen/sharp-linuxmusl-arm64=file:./npm/linuxmusl-arm64\""
          sudo docker exec sharp sh -c "npm run clean"
          sudo docker exec sharp sh -c "npm install --ignore-scripts"
          sudo docker exec sharp sh -c "npm test"
