pipeline {
  agent any
  post {
    failure {
      updateGitlabCommitStatus name: 'build', state: 'failed'
    }
    success {
      updateGitlabCommitStatus name: 'build', state: 'success'
    }
    aborted {
      updateGitlabCommitStatus name: 'build', state: 'canceled'
    }
  }
  options {
    gitLabConnection('GitLab')
  }
  triggers {
    gitlab(triggerOnPush: true, triggerOnMergeRequest: true, branchFilterType: 'All')
  }
  stages {
    stage("build") {
      steps {
        updateGitlabCommitStatus name: 'build', state: 'running'
        podTemplate(
          containers: [
            containerTemplate(name: 'docker-image-build', image: 'mgoltzsche/podman', ttyEnabled: true, command: 'cat', privileged: true),
          ]) {
          node(POD_LABEL) {
            updateGitlabCommitStatus name: 'build', state: 'pending'
            checkout([$class: 'GitSCM', branches: [
              [name: '*/main']
            ], userRemoteConfigs: [
              [url: 'https://gitlab.com/luca_lutz/fabaccess.git']
            ]])
            container('docker-image-build') {
              withCredentials([
                usernamePassword(credentialsId: 'docker.credentials',
                  usernameVariable: 'DOCKER_USERNAME',
                  passwordVariable: 'DOCKER_PASSWORD')
              ]) {
                sh 'echo $(date +%s) > /time'
                sh 'echo "nameserver 10.12.42.2" > /etc/resolv.conf'
                sh 'podman build --tag docker.sfz-aalen.space/hackwerk/fabaccess:$(cat /time) .'
                sh 'podman login -u ${DOCKER_USERNAME} -p ${DOCKER_PASSWORD} docker.sfz-aalen.space'
                sh 'podman push docker.sfz-aalen.space/hackwerk/fabaccess:$(cat /time)'
              }
            }
          }
        }
      }
    }
  }
}