You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

build.py 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #!/usr/bin/env python3
  2. # Docker API documentation : https://docker-py.readthedocs.io
  3. # Requirements
  4. # apt install libffi-dev python3-dev
  5. # pip3 install docker[tls]
  6. # pip3 installshutil.rmtree('src')
  7. import docker
  8. import os
  9. import platform
  10. import pprint
  11. import shutil
  12. import sys
  13. from dulwich import porcelain
  14. from dulwich.repo import Repo
  15. # Prep source dir, checkout sources, figure out latest version
  16. git_src = 'src'
  17. git_url = 'https://github.com/pixelfed/pixelfed.git'
  18. git_refs = None
  19. repo = None
  20. if not os.path.isdir(git_src):
  21. repo = porcelain.clone(git_url, git_src)
  22. porcelain.pull(git_src, git_url, git_refs)
  23. git_repo = Repo(git_src)
  24. latest_release = str(git_repo.head().decode('utf-8'))
  25. # Client to connect to docker on localhost
  26. client = docker.from_env()
  27. processor = platform.machine()
  28. if processor == 'aarch64':
  29. processor = 'arm64v8'
  30. elif processor == 'x86 64' or processor == 'x86_64':
  31. processor = 'x86_64'
  32. elif processor == 'armv7l':
  33. processor = 'arm32v7'
  34. else:
  35. sys.exit(1)
  36. docker_repository = processor + '/' + 'pixelfed'
  37. print('Building ' + docker_repository + ':' + latest_release)
  38. build_args = None
  39. for line in docker.APIClient(base_url='unix://var/run/docker.sock').build(path=git_src,
  40. dockerfile=os.path.join('contrib','docker','Dockerfile.fpm'),
  41. tag=docker_repository + ':' + latest_release,
  42. labels={'os': 'linux', 'arch': processor},
  43. pull=True,
  44. buildargs=build_args
  45. ):
  46. print(line)
  47. image = client.images.get(docker_repository + ':' + latest_release)
  48. # Ensure latest tag can be created and fail build otherwise
  49. if not image.tag(repository=docker_repository, tag='latest'):
  50. if not image.tag(repository=docker_repository, tag='latest', force=True):
  51. print('Could not create "latest" tag!')
  52. sys.exit(1)
  53. # Reload the image tags/data from the server now that the build is done *and* we've tagged the lastest release
  54. image.reload()
  55. print('Build complete')
  56. # TODO Figure out why this keeps failing...
  57. #print('Saving image')
  58. #if not os.path.isdir('out'):
  59. # os.makedirs('out')
  60. #most_recent_history = 0
  61. #for history in image.history():
  62. # if history['Created'] > most_recent_history:
  63. # most_recent_history = history['Created']
  64. #local_image_file = os.path.join('out', '-'.join([processor,
  65. # 'pixelfed',
  66. # str(most_recent_history),
  67. # latest_release
  68. # ]) + '.tar')
  69. #with open(local_image_file, 'wb') as f:
  70. # for chunk in image.save():
  71. # f.write(chunk)
  72. #print('Save complete')
  73. print('Pushing build to registry.lollipopcloud.solutions')
  74. LOLLIPOP_REGISTRY = 'registry.lollipopcloud.solutions'
  75. LOLLIPOP_REGISTRY_URL = 'https://' + LOLLIPOP_REGISTRY
  76. client.login(username='build-server',
  77. registry=LOLLIPOP_REGISTRY_URL,
  78. reauth=True)
  79. upstream_repo = LOLLIPOP_REGISTRY + '/' + processor + '/' + 'pixelfed'
  80. for image_tag in image.tags:
  81. print(' Tag: ' + image_tag)
  82. lc_tag = image_tag.split(':')[1]
  83. image.tag(repository=upstream_repo, tag=lc_tag, force=True)
  84. image.tag(repository='registry.lollipopcloud.tech' + '/' + processor + '/' + 'pixelfed', tag=lc_tag, force=True)
  85. #Don't need to check upstream as it's smart enough not to re-transfer existing files
  86. # TODO: Update this to pre-load the upstream images and not waste the HTTP requests / responses telling Docker it was already pushed
  87. client.images.push(upstream_repo, tag=lc_tag)
  88. client.images.push('registry.lollipopcloud.tech' + '/' + processor + '/' + 'pixelfed', tag=lc_tag)
  89. print('Build complete')