name: $(BuildDefinitionName)_[$(Date:ddMMyyyy)]

pool:
  vmImage: 'Ubuntu 20.04'

trigger:
  batch: true
  branches:
    include:
    - master
  paths:
    include:
    - /devops/build_docker_image.yml
    - /docker/*

schedules:
- cron: "0 0 * * *"
  displayName: Daily midnight build
  branches:
    include:
    - master
  always: true
  
variables:
  imageName: 'devops-agent'
  imageversion: 'latest'
  registryLogin: 'gr00vysky'
  # define imageName, registryLogin, registryPassword variables in the build pipeline using UI

steps:

- bash: docker pull ghcr.io/groovy-sky/ubuntu:focal; docker tag ghcr.io/groovy-sky/ubuntu:focal ubuntu:focal; docker build -f $(system.defaultWorkingDirectory)/docker/Dockerfile -t $(registryLogin)/$(imageName):$(imageversion) --build-arg ANSIBLE_VERSION=$(imageversion) $(system.defaultWorkingDirectory)
  displayName: 'docker build'

- bash: docker login -u $(registryLogin) -p $(registryPassword) 
  displayName: 'docker login'
  condition: and(succeeded(), eq(variables['build.sourceBranch'], 'refs/heads/master'))

- bash: |
    docker push $(registryLogin)/$(imageName):$(imageversion)
  displayName: 'docker push'
  condition: and(succeeded(), eq(variables['build.sourceBranch'], 'refs/heads/master'))