Skip to content

update build action #102

update build action

update build action #102

Workflow file for this run

# Continuous integration testing for ChRIS Plugin.
# https://github.com/FNNDSC/python-chrisapp-template/wiki/Continuous-Integration
#
# - on push and PR: run pytest
# - on push to main: build and push container images as ":latest"
# - on push to semver tag: build and push container image with tag and
# upload plugin description to https://cube.chrisproject.org/api/v1/
name: build
on:
push:
branches: [ main ]
tags:
- "v?[0-9]+.[0-9]+.[0-9]+*"
pull_request:
branches: [ main ]
jobs:
test:
name: Unit tests
if: False
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v4
- uses: docker/setup-buildx-action@v3
- name: Build
uses: docker/build-push-action@v5
with:
build-args: extras_require=dev
context: .
load: true
push: false
tags: "localhost/local/app:dev"
cache-from: type=gha
cache-to: type=gha,mode=max
- name: Run pytest
run: |
docker run -v "$GITHUB_WORKSPACE:/app:ro" -w /app localhost/local/app:dev \
pytest -o cache_dir=/tmp/pytest
publish:
if: github.event_name == 'push' || github.event_name == 'release'
runs-on: ubuntu-22.04
# we want to both push the build to DockerHub, but also
# keep a local copy so that we can run
#
# docker run fnndsc/pl-app app --json > App.json
#
# buildx currently does not support multiple output locations,
# neither can multi-architectural builds be loaded into docker.
# Here we use a local registry to cache the build.
services:
registry:
image: registry:2
ports:
- 5000:5000
steps:
- name: Stop docker
run: sudo systemctl stop docker
- name: Clean docker data
run: |
sudo rm -rf /var/lib/docker
sudo mkdir /var/lib/docker
- name: Maximize build space
uses: easimon/maximize-build-space@6ae56c86ea8db291ae39f62352a412c36ab8179b
with:
root-reserve-mb: 8192 # space needed for logs
swap-size-mb: 1024 # must be >0
build-mount-path: /var/lib/docker
remove-dotnet: 'true'
remove-android: 'true'
remove-haskell: 'true'
remove-codeql: 'true'
remove-docker-images: 'false'
- name: Start docker
run: sudo systemctl start docker
- name: Decide image tags
id: info
shell: python
run: |
import os
import itertools
def join_tag(t):
registry, repo, tag = t
return f'{registry}/{repo}:{tag}'.lower()
registries = ['docker.io', 'ghcr.io']
repos = ['${{ github.repository }}']
if '${{ github.ref_type }}' == 'branch':
tags = ['latest']
elif '${{ github.ref_type }}' == 'tag':
tag = '${{ github.ref_name }}'
version = tag[1:] if tag.startswith('v') else tag
tags = ['latest', version]
else:
tags = []
if '${{ github.ref_type }}' == 'tag':
local_tag = join_tag(('ghcr.io', '${{ github.repository }}', version))
else:
local_tag = join_tag(('localhost', '${{ github.repository }}', 'latest'))
product = itertools.product(registries, repos, tags)
tags_csv = ','.join(map(join_tag, product))
outputs = {
'tags_csv' : tags_csv,
'push' : 'true' if tags_csv else 'false',
'local_tag': local_tag
}
with open(os.environ['GITHUB_OUTPUT'], 'a') as out:
for k, v in outputs.items():
out.write(f'{k}={v}\n')
- uses: actions/checkout@v2
# QEMU is for emulating non-x86_64 platforms
- uses: docker/setup-qemu-action@v1
# buildx is the next-generation docker image builder
- uses: docker/setup-buildx-action@v1
with:
driver-opts: network=host
# save some time during rebuilds
- name: Cache Docker layers
uses: actions/cache@v2
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-
- name: Login to DockerHub
id: dockerhub_login
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_PASSWORD }}
- name: Login to GitHub Container Registry
uses: docker/login-action@v1
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build and push
uses: docker/build-push-action@v5
if: (github.event_name == 'push' || github.event_name == 'release')
with:
context: .
file: ./Dockerfile
tags: ${{ steps.info.outputs.tags_csv }}
# if non-x86_84 architectures are supported, add them here
platforms: linux/amd64 #,linux/arm64,linux/ppc64le
push: ${{ steps.info.outputs.push }}
cache-to: type=gha,mode=max
- name: Get plugin meta
id: pluginmeta
run: |
dock_image: ${{ steps.info.outputs.local_tag }}
docker pull localhost:5000/$dock_image
docker tag localhost:5000/$dock_image $dock_image
script=$(docker inspect --format '{{ (index .Config.Cmd 0) }}' $dock_image)
docker run --rm $dock_image $script --json \
| jq '. += {"name":"pl-lld_inference", "dock_image": "'$dock_image'", "public_repo": "'${{ github.server_url }}/${{ github.repository }}'" }' \
> /tmp/description.json
- name: Upload ChRIS Plugin
id: upload
if: github.ref_type == 'tag'
uses: FNNDSC/upload-chris-plugin@main
with:
description_file: /tmp/description.json
username: ${{ secrets.CHRISPROJECT_USERNAME }}
password: ${{ secrets.CHRISPROJECT_PASSWORD }}
chris_url: https://cube.chrisproject.org/api/v1/
compute_names: NERC
- name: Update DockerHub description
if: steps.upload.outcome == 'success'
uses: peter-evans/dockerhub-description@v3
continue-on-error: true # it is not crucial that this works
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_PASSWORD }}
short-description: ${{ steps.upload.outputs.title }}
readme-filepath: ./README.rst