node {
    def app
    stage('Utworzenie obrazu Dockera') {
        checkout scm
        docker.withServer('tcp://dockerhost:2376', 'docker-swarm') {
            app = docker.build('dockerhp/jenkins-app:latest')
        }
    }
    
    stage('Przekazanie obrazu do Docker Hub') {
        docker.withServer('tcp://dockerhost:2376', 'docker-swarm') {
            docker.withRegistry("https://index.docker.io/v1/", "dockerhub") {
               app.push('latest')
            }
        }
    }

    stage('Wdrożenie obrazu w środowisku produkcyjnym') {
        docker.withServer('tcp://dockerhost:2376', 'docker-swarm') {
            sh "docker service update app --force --image ${app.id}"
        }
    }
}
