name:   Publish Crate

on:
    push:
        tags:
            - tree-hash-v*
            - tree-hash-derive-v*
            - eth2-ssz-v*
            - eth2-ssz-derive-v*
            - eth2-ssz-types-v*
            - eth2-serde-util-v*
            - eth2-hashing-v*

env:
    CARGO_API_TOKEN: ${{ secrets.CARGO_API_TOKEN }}

jobs:
    extract-tag:
        runs-on: ubuntu-latest
        steps:
            - name: Extract tag
              run: echo "::set-output name=TAG::$(echo ${GITHUB_REF#refs/tags/})"
              id: extract_tag
        outputs:
            TAG: ${{ steps.extract_tag.outputs.TAG }}

    publish-crate:
        runs-on: ubuntu-latest
        needs: [extract-tag]
        env:
            TAG: ${{ needs.extract-tag.outputs.TAG }}
        steps:
            - uses: actions/checkout@v2
            - name: Update Rust
              run: rustup update stable
            - name: Cargo login
              run: |
                  echo "${CARGO_API_TOKEN}" | cargo login
            - name: publish eth2 ssz derive
              if: startsWith(env.TAG, 'eth2-ssz-derive-v')
              run: |
                ./scripts/ci/publish.sh consensus/ssz_derive eth2_ssz_derive "$TAG"
            - name: publish eth2 ssz
              if: startsWith(env.TAG, 'eth2-ssz-v')
              run: |
                ./scripts/ci/publish.sh consensus/ssz eth2_ssz "$TAG"
            - name: publish eth2 hashing
              if: startsWith(env.TAG, 'eth2-hashing-v')
              run: |
                ./scripts/ci/publish.sh crypto/eth2_hashing eth2_hashing "$TAG"
            - name: publish tree hash derive
              if: startsWith(env.TAG, 'tree-hash-derive-v')
              run: |
                ./scripts/ci/publish.sh consensus/tree_hash_derive tree_hash_derive "$TAG"
            - name: publish tree hash
              if: startsWith(env.TAG, 'tree-hash-v')
              run: |
                ./scripts/ci/publish.sh consensus/tree_hash tree_hash "$TAG"
            - name: publish ssz types
              if: startsWith(env.TAG, 'eth2-ssz-types-v')
              run: |
                ./scripts/ci/publish.sh consensus/ssz_types eth2_ssz_types "$TAG"
            - name: publish serde util
              if: startsWith(env.TAG, 'eth2-serde-util-v')
              run: |
                ./scripts/ci/publish.sh consensus/serde_utils eth2_serde_utils "$TAG"
