Skip to content

Commit

Permalink
Add autopush and mane
Browse files Browse the repository at this point in the history
  • Loading branch information
cjreed121 committed Nov 13, 2023
1 parent b106692 commit 58656ad
Show file tree
Hide file tree
Showing 5 changed files with 186 additions and 0 deletions.
43 changes: 43 additions & 0 deletions .github/workflows/docker-build-all.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: Docker Build All and Push

on:
workflow_dispatch:

jobs:
generate-matrix:
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.set-matrix.outputs.matrix }}
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Set Matrix
id: set-matrix
run: |
echo "matrix=$(python3 bin/generate_matrix.py ${{ secrets.DOCKER_USERNAME_SUBMITTY }} true)" >> $GITHUB_OUTPUT
- name: List Matrix
run: |
echo ${{ steps.set-matrix.outputs.matrix }}
docker:
needs:
- generate-matrix
runs-on: ubuntu-latest
strategy:
fail-fast: true
matrix: ${{ fromJson(needs.generate-matrix.outputs.matrix) }}
steps:
- name: Check out repo
uses: actions/checkout@v3
- name: Docker Hub login
uses: docker/login-action@releases/v1
with:
username: ${{ secrets.DOCKER_USERNAME_SUBMITTYRPI }}
password: ${{ secrets.DOCKER_PASSWORD_SUBMITTYRPI }}
- name: Build and push docker
uses: docker/build-push-action@v4
with:
context: ${{ matrix.context }}
push: true
tags: ${{ matrix.tags }}
platforms: linux/amd64,linux/arm64

47 changes: 47 additions & 0 deletions .github/workflows/docker-build-push.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: Docker Build and Push

on:
# push: temporarily disable
# branches:
# - "main"

jobs:
generate-matrix:
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.set-matrix.outputs.matrix }}
steps:
- name: Checkout code
uses: actions/checkout@v3
with:
fetch-depth: ${{ github.event_name == 'pull_request' && 2 || 0 }}
- name: Set Matrix
id: set-matrix
run: |
echo "matrix=$(python3 bin/generate_matrix.py ${{ secrets.DOCKER_USERNAME_SUBMITTY }} false ${{ github.event.before }} ${{ github.event.after }})" >> $GITHUB_OUTPUT
- name: List Matrix
run: |
echo ${{ steps.set-matrix.outputs.matrix }}
docker:
needs:
- generate-matrix
runs-on: ubuntu-latest
strategy:
fail-fast: true
matrix: ${{ fromJson(needs.generate-matrix.outputs.matrix) }}
steps:
- name: Check out repo
uses: actions/checkout@v3
- name: Docker Hub login
uses: docker/login-action@releases/v1
with:
username: ${{ secrets.DOCKER_USERNAME_SUBMITTYRPI }}
password: ${{ secrets.DOCKER_PASSWORD_SUBMITTYRPI }}
- name: Build and push docker
uses: docker/build-push-action@v4
with:
context: ${{ matrix.context }}
push: true
tags: ${{ matrix.tags }}
platforms: linux/amd64,linux/arm64

87 changes: 87 additions & 0 deletions bin/generate_matrix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import json
import os
import subprocess
import sys
from pathlib import Path

if len(sys.argv) > 5:
print("Too many arguments!", file=sys.stderr)
exit(1)
elif len(sys.argv) < 3:
print("Not enough arguments!", file=sys.argv)
exit(1)

if not os.path.isdir("dockerfiles"):
print("dockerfiles missing!", file=sys.stderr)
exit(1)

username = sys.argv[1]
build_all = sys.argv[2]

to_build = []

if build_all == "false":
hash_before = sys.argv[3]
hash_after = sys.argv[4]

# Get list of all changed files between 2 commits
output = subprocess.check_output(["git", "--no-pager", "diff", "--name-only", hash_before, hash_after])
paths_updated = output.decode("utf-8").splitlines()

for path in paths_updated:
parts = Path(path).parts
# Only rebuild if modified files were in dockerfiles
if parts[0] != "dockerfiles":
continue

metadata = json.loads(open(Path(parts[0]) / parts[1] / "metadata.json").read())

push_latest = False

if metadata["pushLatest"]:
if metadata["latestImage"] == parts[2]:
push_latest = True

tags = f"{username}/{parts[1]}:{parts[2]}"
if push_latest:
tags += f",{username}/{parts[1]}:latest"
to_build.append(
{
"tags": tags,
"context": str(os.path.dirname(path))
}
)
elif build_all == "true":
images = os.listdir("dockerfiles")
for image in images:
path = Path("dockerfiles") / image
if not path.is_dir():
continue

metadata = json.loads(open(path / "metadata.json").read())

tags = os.listdir(path)
for tag in tags:
newpath = path / tag
if not newpath.is_dir():
continue

push_latest = False
if metadata["pushLatest"]:
if metadata["latestImage"] == tag:
push_latest = True

tags = f"{username}/{image}:{tag}"
if push_latest:
tags += f",{username}/{image}:latest"
to_build.append(
{
"tags": tags,
"context": str(newpath)
}
)


finobj = {"include": to_build}

print(json.dumps(finobj))
3 changes: 3 additions & 0 deletions dockerfiles/mane2110/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"pushLatest": false
}
6 changes: 6 additions & 0 deletions dockerfiles/mane2110/scipy/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM submitty/python

RUN pip3 install scipy

# Will eventually be removed
RUN ln -s /usr/local/bin/python3 /usr/bin/python3

0 comments on commit 58656ad

Please sign in to comment.