def main(ctx):
    archs = ["amd64", "arm64"]   ## arm
    alpine_version = "3.19"

    out = []
    for arch in archs:
        out += onpush(ctx, alpine_version, arch)

    for arch in archs:
        out += build_publish(ctx, alpine_version, arch)

    out += manifest_publish(ctx, archs)

    return out

def onpush(ctx, alpine_version, arch):
    return [{
        "kind": "pipeline",
        "type": "docker",
        "name": "build-onpush-%s" % (arch),
        "platform": {
            "arch": arch,
            "os": "linux",
        },
        "steps": [{
            "name": "build_on_push",
            "image": "plugins/docker:linux-%s" % (arch),
            "settings": {
                "context": ".",
                "dockerfile": "Dockerfile",
                "dry_run": True,
                "repo": "docker.asperti.com/paspo/dnscache", 
                "build_args": [
                    "ALPINE_VERSION=%s" % (alpine_version)
                ],
            },
        }],
        "trigger": {
            "event": ['push'],
        }
    }]

def build_publish(ctx, alpine_version, arch):
    return [{
        "kind": "pipeline",
        "type": "docker",
        "name": "build-publish-%s" % (arch),
        "platform": {
            "arch": arch,
            "os": "linux",
        },
        "steps": [{
            "name": "build_on_push",
            "image": "plugins/docker:linux-%s" % (arch),
            "settings": {
                "context": ".",
                "dockerfile": "Dockerfile",
                "build_args": [
                    "ALPINE_VERSION=%s" % (alpine_version)
                ],
                "username": {
                    "from_secret": "docker_username",
                },
                "password": {
                    "from_secret": "docker_password",
                },
                "registry": "docker.asperti.com",
                "repo": "docker.asperti.com/paspo/dnscache",    
                "tags": [ 
                    "latest-%s" % (arch)
                ],
                "auto_tag": False,
                "force_tag": True,
                "daemon_off": False,            
            },
        }],
        "trigger": {
            "ref": [
                "refs/heads/master",
                "refs/tags/**",
            ],
        },
    }]

def manifest_publish(ctx, archs):
    return [{
        "kind": "pipeline",
        "type": "docker",
        "name": "manifest",
        "platform": {
            "arch": archs[0],
            "os": "linux",
        },
        "steps": [{
            "name": "manifest",
            "image": "plugins/manifest",
            "settings": {
                "spec": "manifest.tmpl",
                "username": {
                    "from_secret": "docker_username",
                },
                "password": {
                    "from_secret": "docker_password",
                }, 
                "tags": [ 
                    "latest"
                ],
                "ignore_missing": True,
                "force_tag": True,
            },
        }],
        "trigger": {
            "ref": [
                "refs/heads/master",
                "refs/tags/**",
            ],
        },
        "depends_on": ["build-publish-%s" % (arch) for arch in archs]
    }]