name: CI/CD

on:
  push:
    branches: [ "main", "master" ]
    tags:
      - 'v*.*.*'
  pull_request:
    branches: [ "main", "master" ]

jobs:
  build-theta-agent:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3
      with:
        submodules: recursive

    - name: Set up Go
      uses: actions/setup-go@v4
      with:
        go-version: '1.21'

    - name: Build Agent
      working-directory: ./theta-agent
      run: go build -v ./...

  docker-push:
    needs: [build-theta-agent]
    if: startsWith(github.ref, 'refs/tags/v')
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3
      with:
        submodules: recursive

    - name: Log in to GitHub Container Registry
      uses: docker/login-action@v2
      with:
        registry: ghcr.io
        username: ${{ github.actor }}
        password: ${{ secrets.GITHUB_TOKEN }}

    - name: Build and Push SSO Manager
      uses: docker/build-push-action@v4
      with:
        context: ./sso-manager-node
        file: ./sso-manager-node/Dockerfile.openldap
        push: true
        tags: |
          ghcr.io/${{ github.repository_owner }}/sso-manager:latest
          ghcr.io/${{ github.repository_owner }}/sso-manager:${{ github.ref_name }}

    - name: Build and Push Proxy
      uses: docker/build-push-action@v4
      with:
        context: ./proxy
        file: ./proxy/Dockerfile
        push: true
        tags: |
          ghcr.io/${{ github.repository_owner }}/theta-proxy:latest
          ghcr.io/${{ github.repository_owner }}/theta-proxy:${{ github.ref_name }}
