---
name: CI/CD

on:
  push:

jobs:
  build:
    name: Build
    runs-on: ubuntu-latest
    strategy:
      matrix:
        ruby_versions: [2.7, 3.0, 3.1, 3.2]

    steps:
    - uses: actions/checkout@v3
    - name: Set up Ruby ${{ matrix.ruby_versions }}
      uses: ruby/setup-ruby@v1
      with:
        ruby-version: ${{ matrix.ruby_versions }}
    - uses: actions/cache@v3
      with:
        path: vendor/bundle
        key: ${{ runner.os }}-gems-${{ hashFiles('**/Gemfile.lock') }}
        restore-keys: ${{ runner.os }}-gems-
    - name: Install Bundler
      run: gem install bundler
    - name: Configure Bundler
      run: bundle config path vendor/bundle
    - name: Bundle Install
      run: bundle install --jobs 4 --retry 3
    - name: Set APP_STORE_CONNECT_VERSION
      run: echo "APP_STORE_CONNECT_VERSION=$(bundle exec rake version:current)" >> $GITHUB_ENV
    - name: Build
      run: bundle exec rake build
    - name: Upload Gem
      uses: actions/upload-artifact@v3
      with:
        name: app_store_connect-${{ env.APP_STORE_CONNECT_VERSION }}.gem
        path: app_store_connect-${{ env.APP_STORE_CONNECT_VERSION }}.gem

  lint:
    name: Lint
    runs-on: ubuntu-latest
    needs: [build]
    strategy:
      matrix:
        ruby_versions: [2.7, 3.0, 3.1, 3.2]

    steps:
    - uses: actions/checkout@v3
    - name: Set up Ruby ${{ matrix.ruby_versions }}
      uses: ruby/setup-ruby@v1
      with:
        ruby-version: ${{ matrix.ruby_versions }}
    - uses: actions/cache@v3
      with:
        path: vendor/bundle
        key: ${{ runner.os }}-gems-${{ hashFiles('**/Gemfile.lock') }}
        restore-keys: ${{ runner.os }}-gems-
    - name: Install Bundler
      run: gem install bundler
    - name: Configure Bundler
      run: bundle config path vendor/bundle
    - name: Bundle Install
      run: bundle install --jobs 4 --retry 3
    - name: Run Rubocop
      run: bundle exec rubocop

  test:
    name: Test
    runs-on: ubuntu-latest
    needs: [build]
    strategy:
      matrix:
        ruby_versions: [2.7, 3.0, 3.1, 3.2]

    steps:
    - uses: actions/checkout@v3
    - name: Set up Ruby ${{ matrix.ruby_versions }}
      uses: ruby/setup-ruby@v1
      with:
        ruby-version: ${{ matrix.ruby_versions }}
    - uses: actions/cache@v3
      with:
        path: vendor/bundle
        key: ${{ runner.os }}-gems-${{ hashFiles('**/Gemfile.lock') }}
        restore-keys: ${{ runner.os }}-gems-
    - name: Install Bundler
      run: gem install bundler
    - name: Configure Bundler
      run: bundle config path vendor/bundle
    - name: Bundle Install
      run: bundle install --jobs 4 --retry 3
    - name: Run RSpec
      run: bundle exec rake spec

  release:
    name: Release
    runs-on: ubuntu-latest
    if: ${{ startsWith(github.ref, 'refs/tags/v') }}
    needs: [test, lint]

    steps:
    - uses: actions/checkout@v3
    - name: Set up Ruby 3.2
      uses: ruby/setup-ruby@v1
      with:
        ruby-version: 3.2
    - uses: actions/cache@v3
      with:
        path: vendor/bundle
        key: ${{ runner.os }}-gems-${{ hashFiles('**/Gemfile.lock') }}
        restore-keys: ${{ runner.os }}-gems-
    - name: Install Bundler
      run: gem install bundler
    - name: Configure Bundler
      run: bundle config path vendor/bundle

    - name: Configure Gem
      run: |
        mkdir -p $HOME/.gem
        touch $HOME/.gem/credentials
        chmod 0600 $HOME/.gem/credentials
        printf -- "---\n:rubygems_api_key: ${{secrets.RUBYGEMS_API_KEY}}\n:github: Bearer ${{secrets.PERSONAL_ACCESS_TOKEN}}" > $HOME/.gem/credentials

    - name: Bundle Install
      run: bundle install --jobs 4 --retry 3
    - name: Set APP_STORE_CONNECT_VERSION
      run: echo "APP_STORE_CONNECT_VERSION=$(bundle exec rake version:current)" >> $GITHUB_ENV
    - uses: actions/download-artifact@v3
      with:
        name: app_store_connect-${{ env.APP_STORE_CONNECT_VERSION }}.gem
        path: /tmp
    - run: mv /tmp/app_store_connect-${{ env.APP_STORE_CONNECT_VERSION }}.gem .
    - name: Push Gem to Rubygems
      run: bundle exec rake push[rubygems]
      continue-on-error: true
    - name: Push Gem to Github Package Registry
      run: bundle exec rake push[github]
      continue-on-error: true
    - name: Create Release
      id: create_release
      uses: actions/create-release@latest
      env:
        GITHUB_TOKEN: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
      with:
        tag_name: ${{ github.ref }}
        release_name: ${{ github.ref }}
        draft: false
        prerelease: false
    - name: Upload Release Asset
      uses: actions/upload-release-asset@v1
      env:
        GITHUB_TOKEN: ${{ secrets.PERSONAL_ACCESS_TOKEN  }}
      with:
        upload_url: ${{ steps.create_release.outputs.upload_url }}
        asset_path: app_store_connect-${{ env.APP_STORE_CONNECT_VERSION }}.gem
        asset_name: app_store_connect-${{ env.APP_STORE_CONNECT_VERSION }}.gem
        asset_content_type: application/binary

