substitutions:
  _APP_NAME: 'siges-manual'
  _WEBHOOK_USER: 'webhooksigesdev'
  _WEBHOOK_PASS: 'Passw0rdKeelSigesDev'
  _WEBHOOK_IP: "34.55.25.203"
  _GIT_BRANCH: "develop"


steps:
  # Step 0: Limpieza de workspace
  - name: 'us-central1-docker.pkg.dev/siges-kb-dev/siges-kb-dev-registry/git'
    entrypoint: 'bash'
    args:
      - '-c'
      - |
        rm -rf /workspace/${_APP_NAME}

  # Step 1: Clonar el código desde Secure Source Manager (SSM) y usar la rama develop
  - name: 'us-central1-docker.pkg.dev/siges-kb-dev/siges-kb-dev-registry/git'
    args: ['clone', '-b', 'develop', 'https://ssm-siges-kb-dev-4648829245-git.us-central1.sourcemanager.dev/siges-kb-dev/${_APP_NAME}.git', '/workspace/${_APP_NAME}']

  # Step 2: Cambiar el owner de manera recursiva a 1001:1001 en /workspace/${_APP_NAME} usando la imagen busybox
  - name: 'us-central1-docker.pkg.dev/siges-kb-dev/siges-kb-dev-registry/busybox'
    args: ['chown', '-R', '1001:1001', '/workspace/${_APP_NAME}']

  # Step 3: Crear un Dockerfile dinámico en el directorio /workspace/${_APP_NAME}/temp/app
  - name: 'us-central1-docker.pkg.dev/siges-kb-dev/siges-kb-dev-registry/busybox'
    args: ['sh', '-c', 'echo "FROM us-central1-docker.pkg.dev/siges-kb-dev/siges-kb-dev-registry/httpd" > /workspace/${_APP_NAME}/Dockerfile && echo "COPY --chown=1001:1001 * /tmp/src/" >> /workspace/${_APP_NAME}/Dockerfile && echo "RUN rm -f /tmp/src/Dockerfile" >> /workspace/${_APP_NAME}/Dockerfile']

  # Step 4: Construir una nueva imagen Docker basada en httpd alpine copiando todo el contenido a /tmp/src
  - name: 'gcr.io/cloud-builders/docker'
    args: [
      'build',
      '-t',
      'us-central1-docker.pkg.dev/siges-kb-dev/siges-kb-dev-registry/${_APP_NAME}:latest', 
      '-f', '/workspace/${_APP_NAME}/Dockerfile',
      '/workspace/${_APP_NAME}'
    ]

  # Step 5: Hacer push de la nueva imagen a Google Container Registry
  - name: 'us-central1-docker.pkg.dev/siges-kb-dev/siges-kb-dev-registry/docker'
    args: ['push', 'us-central1-docker.pkg.dev/siges-kb-dev/siges-kb-dev-registry/${_APP_NAME}:latest']

images:
  - 'us-central1-docker.pkg.dev/siges-kb-dev/siges-kb-dev-registry/${_APP_NAME}:latest'
    
  # Step 6: Publicar Imagen
  - name: "us-central1-docker.pkg.dev/siges-kb-dev/siges-kb-dev-registry/curl"
    args:
      - "-X"
      - "POST"
      - "-H"
      - "Content-Type: application/json"
      - "-u"
      - "$_WEBHOOK_USER:$_WEBHOOK_PASS"
      - "-d"
      - '{
            "name": "us-central1-docker.pkg.dev/siges-kb-dev/siges-kb-dev-registry/$_APP_NAME",
            "tag": "latest"
          }'
      - "http://$_WEBHOOK_IP:9300/v1/webhooks/native"

images:
  - 'us-central1-docker.pkg.dev/siges-kb-dev/siges-kb-dev-registry/${_APP_NAME}:latest'
