Skip to content

Commit

Permalink
Merge pull request #25 from deadsnakes/buildx-cache
Browse files Browse the repository at this point in the history
try out buildx cache
  • Loading branch information
asottile authored Aug 30, 2023
2 parents fda90ff + 534851e commit 922b511
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 39 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
on:
pull_request:
paths: [dockerfiles/*]
paths: [dockerfiles/*, .github/workflows/main.yml]
push:
branches: [main]
paths: [dockerfiles/*]
paths: [dockerfiles/*, .github/workflows/main.yml]

jobs:
main:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- run: bin/docker-build
- run: bin/docker-push
- run: docker login --user '${{ github.actor }}' --password-stdin ghcr.io <<< "$GHCR_TOKEN"
env:
GHCR_USER: ${{ github.actor }}
GHCR_TOKEN: ${{ secrets.GITHUB_TOKEN }}
if: github.event_name != 'pull_request'
- run: docker buildx create --driver docker-container --use
- run: python3 -uS bin/docker-build --event ${{ github.event_name }}
17 changes: 13 additions & 4 deletions bin/docker-build
Original file line number Diff line number Diff line change
@@ -1,25 +1,34 @@
#!/usr/bin/env python3
from __future__ import annotations

import argparse
import os.path
import subprocess


def main() -> int:
parser = argparse.ArgumentParser()
parser.add_argument('--event', default='pull_request')
args = parser.parse_args()

for filename in os.listdir('dockerfiles'):
_, name = filename.split('.')
tag = f'ghcr.io/deadsnakes/{name}:latest'
tag_cache = f'{tag}-cache'
print(f' {tag} '.center(79, '='), flush=True)

# allow pre-pull to fail
pre_pull_ret = subprocess.call(('docker', 'pull', tag))
if args.event == 'push':
extra: tuple[str, ...] = ('--cache-to', tag_cache, '--push')
else:
extra = ()

with open(os.path.join('dockerfiles', filename), 'rb') as f:
cmd = (
'docker', 'build',
'docker', 'buildx', 'build',
'--pull',
*(('--cache-from', tag) if pre_pull_ret == 0 else ()),
'--cache-from', tag_cache,
'--tag', tag,
*extra,
'-',
)
subprocess.check_call(cmd, stdin=f)
Expand Down
30 changes: 0 additions & 30 deletions bin/docker-push

This file was deleted.

0 comments on commit 922b511

Please sign in to comment.