name: "Setup Rust"
description: "Sets up the correct Rust version and caching via sccache and a GCP backend"
inputs:
  targets:
    description: "Additional targets to install"
    required: false
    default: ""
runs:
  using: "composite"
  steps:
    - id: auth
      uses: google-github-actions/auth@v1
      with:
        workload_identity_provider: "projects/397012414171/locations/global/workloadIdentityPools/github-actions-pool/providers/github-actions"
        service_account: "github-actions@github-iam-387915.iam.gserviceaccount.com"
        export_environment_variables: true
        create_credentials_file: true
    - run: |
        echo "SCCACHE_GCS_BUCKET=firezone-staging-sccache" >> $GITHUB_ENV
        echo "SCCACHE_GCS_RW_MODE=READ_WRITE" >> $GITHUB_ENV
      shell: bash
    - uses: mozilla-actions/sccache-action@v0.0.3
    - run: echo "RUSTC_WRAPPER=$SCCACHE_PATH" >> $GITHUB_ENV
      shell: bash
    - name: Extract Rust version
      run: |
        RUST_TOOLCHAIN=$(grep 'channel' rust-toolchain.toml | awk -F '"' '{print $2}')
        echo "RUST_TOOLCHAIN=$RUST_TOOLCHAIN" >> $GITHUB_ENV
      shell: bash
      working-directory: ./rust
    - uses: dtolnay/rust-toolchain@master
      with:
        toolchain: ${{ env.RUST_TOOLCHAIN }}
        components: rustfmt,clippy
    - run: rustup target add ${{ inputs.targets }}
      if: inputs.targets != ''
      shell: bash
    - name: Start sccache
      run: $SCCACHE_PATH --start-server
      shell: bash
