name: Build comlib.deb package

on:
  push:
    branches:
      - build-test


jobs:
  build-complib-deb:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      packages: write

    steps:
      - name: Checkout
        uses: actions/checkout@v3
        with:
          fetch-depth: 0
      - name: Bump version number
        uses: paulhatch/semantic-version@v4.0.2
        id: next_semantic_version
        with:
          branch: "build-test"
          tag_prefix: ""
          major_pattern: "(MAJOR)"
          minor_pattern: "(MINOR)"
          format: "${major}.${minor}.${patch}-${increment}"
          bump_each_commit: true
      - name: Print Version Numbers
        run: |
          echo ${{join(steps.next_semantic_version.outputs.*, ' - ')}}
          echo "Next version: ${{steps.next_semantic_version.outputs.version}}"
          echo "Next version tag: ${{steps.next_semantic_version.outputs.version_tag}}"
      - name: Tag Commit
        uses: actions/github-script@v5
        with:
          script: |
            github.rest.git.createRef({
              owner: context.repo.owner,
              repo: context.repo.repo,
              ref: 'refs/tags/${{steps.next_semantic_version.outputs.version}}',
              sha: context.sha
            })
      - name: Install fpm
        run: sudo apt install ruby -y && sudo gem install fpm
      - name: Build complib deb Package
        env:
          VERSION: ${{steps.next_semantic_version.outputs.version}}
        run: bash ./build.sh # creates packages in "output" directory
      - name: Pushes to another repository
        uses: cpina/github-action-push-to-another-repository@main
        env:
          API_TOKEN_GITHUB: ${{ secrets.API_TOKEN_GITHUB }}
        with:
          source-directory: 'output'
          target-directory: 'debs/complib/'
          destination-github-username: 'F-WuTS'
          destination-repository-name: 'compREP-dev'
          user-email: joel.klimont@comp-air.at
          target-branch: master
