name: Tag Build
on:
  push:
    branches:
      - master
    tags-ignore:
      - '*' # don't needlessly execute on tags

jobs:
  tag:
    name: Tag Build
    runs-on: ubuntu-latest
    steps:
      - name: Determine Tag
        id: s1
        run: |
          version=$(echo "${{github.event.head_commit.message}}" | perl -nle 'm/build:\s*([0-9]+.[0-9]+.[0-9]+.[0-9]+)/; print $1');

          if [ -z "$version" ];
          then
                  echo "No build version found";
                  echo "::set-output name=D_TAG_RESULT::fail";
          else
                  export D_TAG_VERSION=$version;
                  echo "::set-output name=D_TAG_VERSION::$D_TAG_VERSION";
                  echo "::set-output name=D_TAG_RESULT::pass";
          fi
      - name: Create Tag
        if: steps.s1.outputs.D_TAG_RESULT == 'pass'
        uses: actions/github-script@v3
        with:
          github-token: ${{ github.token }}
          script: |
            github.git.createRef({
              owner: context.repo.owner,
              repo: context.repo.repo,
              ref: "refs/tags/${{steps.s1.outputs.D_TAG_VERSION}}",
              sha: context.sha
            })
