version: 1
kind: pipeline
spec:
  stages:
  - name: build
    type: ci
    spec:
      volumes:
      - name: dockersock
        spec: {}
        type: temp
      steps:
      - name: debug
        type: run
        spec:
          name: debug
          container: alpine/git
          shell: sh
          script: |
            echo build.event = ${{ build.event }}
            echo build.target = ${{ build.target }}
            echo build.before = ${{ build.before }}
            echo build.after = ${{ build.after }}
      - name: dind
        type: background
        spec:
          container:
            image: registry-app.inhome.blapointe.com:5001/docker:latest
            privileged: true
          mount:
          - name: dockersock
            path: /var/run
      - name: docker
        type: run
        spec:
          name: docker
          container: registry-app.inhome.blapointe.com:5001/docker:latest
          mount:
          - name: dockersock
            path: /var/run
          shell: sh
          script: |
            set -ueo pipefail
            apk add --no-cache git
            t=registry-app.inhome.blapointe.com:5001/portfolio/resume/builder:latest
            if git diff --name-only ${{ build.before }} ${{ build.after }} | grep Dockerfile.cicd; then
              until docker ps; do
                sleep 5
              done
              docker build -t "$t" -f ./Dockerfile.cicd .
              docker push "$t"
            fi
      - name: build
        type: run
        when: build.event == "push" && build.target == "main"
        spec:
          name: build
          container:
            image: registry-app.inhome.blapointe.com:5001/portfolio/resume/builder:latest
            user: root
          envs:
            FIREBASE_CI_TOKEN: ${{ secrets.get("resume-firebase-squeaky2x3-gmail-token") }}
            GITLAB_PAT: ${{ secrets.get("resume-gitlab-gitlab-breel-dev-token") }}
          shell: sh
          script: |
            bash ./deploy.firebase.sh
