name: 'Common Go'
description: 'Checks out, and handles Go setup and caching'
runs:
  using: "composite"
  steps:
    - name: Set up Go
      if: matrix.go-version != 'tip'
      uses: actions/setup-go@v2
      with:
        go-version: ${{ matrix.go-version }}
    - uses: actions/cache@v2
      with:
        path: |
          ~/.cache/go-build
          ~/go/pkg/mod
        # The OS defines the directories to use, then this is specific to go. The go version could
        # affect the dependencies. The job can affect what is actually downloaded, and provides
        # collision resistance. Finally, the hash of the go.sum files ensures a new cache is created
        # when the dependencies change. Note if this were just a mod cache, we might do this based
        # on time or something.
        key: ${{ runner.os }}-go-${{ matrix.go-version }}-${{ github.job }}-${{ hashFiles('**/go.sum') }}
        restore-keys: |
          ${{ runner.os }}-go-${{ matrix.go-version }}-${{ github.job }}-
          ${{ runner.os }}-go-${{ matrix.go-version }}-
          ${{ runner.os }}-go-
    - run: |
        echo GOTIP_REVISION="`git ls-remote https://github.com/golang/go refs/heads/master | cut -f1`" >> "$GITHUB_ENV"
        echo GOTIP_PATH="$HOME/gotip" >> "$GITHUB_ENV"
      if: matrix.go-version == 'tip'
      shell: bash
    - uses: actions/cache@v2
      if: matrix.go-version == 'tip'
      with:
        path: |
          ${{ env.GOTIP_PATH }}
        # The build varies by OS (and arch, but I haven't bothered to add that yet.) We always want
        # the latest snapshot that works for us, the revision is only used to store differentiate
        # builds.
        key: gotip-ls-remote-${{ runner.os }}-${{ env.GOTIP_REVISION }}
        restore-keys: |
          gotip-ls-remote-${{ runner.os }}-${{ env.GOTIP_REVISION }}
          gotip-ls-remote-${{ runner.os }}-
          gotip-env-home-${{ runner.os }}-
          gotip-${{ runner.os }}-
    - name: Install gotip
      if: matrix.go-version == 'tip'
      run: |
        git clone --depth=1 https://github.com/golang/go "$GOTIP_PATH" || true
        cd "$GOTIP_PATH"
        git pull
        echo "GOROOT=$GOTIP_PATH" >> "$GITHUB_ENV"
        echo "$(go env GOPATH)/bin:$PATH" >> "$GITHUB_PATH"
        echo "$GOTIP_PATH/bin:$PATH" >> "$GITHUB_PATH"
        echo "anacrolix.built:" $(cat anacrolix.built)
        [[ -x bin/go && `git rev-parse HEAD` == `cat anacrolix.built` ]] && exit
        cd src
        ./make.bash || exit
        git rev-parse HEAD > ../anacrolix.built
        env
      shell: bash
          
