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', '${_GIT_BRANCH}', '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: Ejecutar el contenedor maven-compiler con docker run, montando el código en /opt/src
  - name: 'us-central1-docker.pkg.dev/siges-kb-dev/siges-kb-dev-registry/docker'
    args: [
      'run',
      '-e', 'TIMEZONE=America/Mexico_City',
      '-e', 'NEXUS_MIRROR_URL=http://nexus.siges.sv/repository/openshift',
      '-v', '/workspace/${_APP_NAME}:/opt/src',
      'us-central1-docker.pkg.dev/siges-kb-dev/siges-kb-dev-registry/maven-compiler'
    ]

  # Step 4: 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/wildfly-thorntail" > /workspace/${_APP_NAME}/temp/app/Dockerfile && echo "COPY app.jar /opt/app-root/app.jar" >> /workspace/${_APP_NAME}/temp/app/Dockerfile']

  # Step 5: Construir una nueva imagen Docker basada en wildfly-thorntail con el JAR copiado a /opt/app-root
  - name: 'us-central1-docker.pkg.dev/siges-kb-dev/siges-kb-dev-registry/docker'
    args: [
      'build',
      '-t',
      'us-central1-docker.pkg.dev/siges-kb-dev/siges-kb-dev-registry/${_APP_NAME}:latest',
      '-f', '/workspace/${_APP_NAME}/temp/app/Dockerfile',
      '/workspace/${_APP_NAME}/temp/app'
    ]

  # Step 6: 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']
    
  # Step 7: 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'
