# Publish NPM package and Docker image
name: Release

on:
  push:
    branches-ignore:
      # don't double-build dependabot PRs
      - dependabot/**
    tags: ["**"]
  pull_request:
  workflow_dispatch:

jobs:
  # Run tests using node, publish a package when tagged
  # https://help.github.com/actions/language-and-framework-guides/publishing-nodejs-packages

  publish-npm:
    runs-on: ubuntu-20.04
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v1
        with:
          node-version: 14
          registry-url: https://registry.npmjs.org/
      - run: npm ci
      - run: npm publish
        if: startsWith(github.ref, 'refs/tags/')
        env:
          NODE_AUTH_TOKEN: ${{ secrets.npm_token }}

  publish-docker:
    runs-on: ubuntu-20.04
    steps:
      - uses: actions/checkout@v2

      # Setup docker to build for multiple platforms, see:
      # https://github.com/docker/build-push-action/tree/v2.4.0#usage
      # https://github.com/docker/build-push-action/blob/v2.4.0/docs/advanced/multi-platform.md

      - name: Set up QEMU (for docker buildx)
        uses: docker/setup-qemu-action@25f0500ff22e406f7191a2a8ba8cda16901ca018 # associated tag: v1.0.2

      - name: Set up Docker Buildx (for multi-arch builds)
        uses: docker/setup-buildx-action@2a4b53665e15ce7d7049afb11ff1f70ff1610609 # associated tag: v1.1.2

      - name: Setup push rights to Docker Hub
        # This was setup by...
        # 1. Creating a Docker Hub service account "jupyterhubbot"
        # 2. Creating a access token for the service account specific to this
        #    repository: https://hub.docker.com/settings/security
        # 3. Making the account part of the "bots" team, and granting that team
        #    permissions to push to the relevant images:
        #    https://hub.docker.com/orgs/jupyterhub/teams/bots/permissions
        # 4. Registering the username and token as a secret for this repo:
        #    https://github.com/jupyterhub/configurable-http-proxy/settings/secrets/actions
        if: startsWith(github.ref, 'refs/tags/')
        run: |
          docker login -u "${{ secrets.DOCKERHUB_USERNAME }}" -p "${{ secrets.DOCKERHUB_TOKEN }}"

      # https://github.com/jupyterhub/action-major-minor-tag-calculator
      # If this is a tagged build this will return additional parent tags.
      # E.g. 1.2.3 is expanded to Docker tags
      # [{prefix}:1.2.3, {prefix}:1.2, {prefix}:1, {prefix}:latest] unless
      # this is a backported tag in which case the newer tags aren't updated.
      # For branches this will return the branch name.
      # If GITHUB_TOKEN isn't available (e.g. in PRs) returns no tags [].
      - name: Get list of tags
        id: gettags
        uses: jupyterhub/action-major-minor-tag-calculator@v1
        with:
          githubToken: ${{ secrets.GITHUB_TOKEN }}
          prefix: "jupyterhub/configurable-http-proxy:"

      - name: Display tags
        run: echo "Docker tags ${{ steps.gettags.outputs.tags }}"

      - name: Build and push
        uses: docker/build-push-action@e1b7f96249f2e4c8e4ac1519b9608c0d48944a1f # associated tag: v2.4.0
        with:
          platforms: linux/amd64,linux/arm64
          push: ${{ startsWith(github.ref, 'refs/tags/') }}
          # tags parameter must be a string input so convert `gettags` JSON
          # array into a comma separated list of tags
          tags: ${{ join(fromJson(steps.gettags.outputs.tags)) }}
