name: "Releases"

on:
  workflow_dispatch:
  push:
    tags:
    - 'v*'

env:
  REGISTRY: ghcr.io
  REGISTRY_USER: ${{ github.actor }}
  REGISTRY_PASSWORD: ${{ github.token }}
  IMAGE_NAME: ${{ github.repository }}

jobs:
  main:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3

    - name: Set node version
      uses: actions/setup-node@v3
      with:
        node-version: '18.x'

    - name: Install
      run: |
        npm ci
        npm run setheapsize

    - name: Lint
      run: npx grunt lint

    - name: Unit Tests
      run: |
        npm test
        npm run testnodeconsumer

    - name: Production Build
      run: npx grunt prod

    - name: UI Tests
      run: |
        sudo apt-get install xvfb
        xvfb-run --server-args="-screen 0 1200x800x24" npx grunt testui

    - name: Image Metadata
      id: image-metadata
      uses: docker/metadata-action@v4
      with:
        images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
        tags: |
          type=semver,pattern={{major}}
          type=semver,pattern={{major}}.{{minor}}
          type=semver,pattern={{version}}

    - name: Production Image Build
      id: build-image
      uses: redhat-actions/buildah-build@v2
      with:
        tags: ${{ steps.image-metadata.outputs.tags }}
        labels: ${{ steps.image-metadata.outputs.labels }}
        containerfiles: ./Dockerfile
        platforms: linux/amd64
        oci: true
        # Webpack seems to use a lot of open files, increase the max open file limit to accomodate.
        extra-args: |
          --ulimit nofile=10000


    - name: Upload Release Assets
      id: upload-release-assets
      uses: svenstaro/upload-release-action@v2
      with:
        repo_token: ${{ secrets.GITHUB_TOKEN }}
        file: build/prod/*.zip
        tag: ${{ github.ref }}
        overwrite: true
        file_glob: true
        body: "See the [CHANGELOG](https://github.com/gchq/CyberChef/blob/master/CHANGELOG.md) and [commit messages](https://github.com/gchq/CyberChef/commits/master) for details."

    - name: Publish to NPM
      uses: JS-DevTools/npm-publish@v1
      with:
        token: ${{ secrets.NPM_TOKEN }}

    - name: Publish to GHCR
      uses: redhat-actions/push-to-registry@v2
      with:
        tags: ${{ steps.build-image.outputs.tags }}
        registry: ${{ env.REGISTRY }}
        username: ${{ env.REGISTRY_USER }}
        password: ${{ env.REGISTRY_PASSWORD }}
