include:
  - project: "devops/pena-continuous-integration"
    file: "/templates/docker/build-template.gitlab-ci.yml"
  - project: "devops/pena-continuous-integration"
    file: "/templates/docker/deploy-template.gitlab-ci.yml"
stages:
  - build
  - deploy

build-app:
  stage: build
  tags:
    - gobuild
  variables:
    DOCKER_BUILD_PATH: "./Dockerfile"
    STAGING_BRANCH: "staging"
    PRODUCTION_BRANCH: "main"
  rules:
    - if: $CI_COMMIT_BRANCH == $PRODUCTION_BRANCH || $CI_COMMIT_BRANCH == $STAGING_BRANCH
  when: on_success
  before_script:
    - echo PRODUCTION_BRANCH = $PRODUCTION_BRANCH
    - echo STAGING_BRANCH = $STAGING_BRANCH
    - echo CI_REGISTRY = $CI_REGISTRY
    - echo CI_REGISTRY_USER = $CI_REGISTRY_USER
    - echo CI_PROJECT_DIR = $CI_PROJECT_DIR
    - echo CI_REGISTRY_IMAGE = $CI_REGISTRY_IMAGE
    - echo CI_COMMIT_REF_SLUG = $CI_COMMIT_REF_SLUG
    - echo DOCKER_BUILD_PATH = $DOCKER_BUILD_PATH
    - echo CI_PIPELINE_ID = $CI_PIPELINE_ID
    - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
  script:
    - docker build -t $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG.$CI_PIPELINE_ID --build-arg  GITLAB_TOKEN=$GITLAB_TOKEN $CI_PROJECT_DIR
    - docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG.$CI_PIPELINE_ID




deploy-to-staging:
  stage: deploy
  extends: .deploy_template
  variables:
    DEPLOY_TO: "staging"
    BRANCH: "staging"
  rules:
  - if: "$CI_COMMIT_BRANCH == $BRANCH"
  after_script:
    - ls

