Skip to content

Commit

Permalink
Update ci.yml
Browse files Browse the repository at this point in the history
  • Loading branch information
jennydaman committed Oct 16, 2023
1 parent ebc7762 commit 0bf0b7c
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 176 deletions.
196 changes: 81 additions & 115 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,151 +14,117 @@ name: ci
on:
push:
# we have to guess what the name of the default branch is
branches: [ master, main, trunk ]
tags: [ '**' ]
branches: [ master ]
tags: [ "[0-9]+.[0-9]+.[0-9]+*" ]
pull_request:
branches: [ master, main, trunk ]
release:
types: [ created ]
branches: [ master ]

jobs:
test:
runs-on: ubuntu-20.04
build:
name: Build
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v2
- name: build
run: docker build -t "${GITHUB_REPOSITORY,,}" .
- name: nose tests
run: docker run "${GITHUB_REPOSITORY,,}" nosetests

publish:
if: github.event_name == 'push' || github.event_name == 'release'
runs-on: ubuntu-20.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: Get git tag
id: git_info
if: startsWith(github.ref, 'refs/tags/')
run: echo "::set-output name=tag::${GITHUB_REF##*/}"
- name: Decide image tag name
id: determine
env:
git_tag: ${{ steps.git_info.outputs.tag }}
- name: Decide image tags
id: info
shell: python
run: |
repo="${GITHUB_REPOSITORY,,}" # to lower case
# if build triggered by tag, use tag name
tag="${git_tag:-latest}"
dock_image=$repo:$tag
echo $dock_image
echo "::set-output name=dock_image::$dock_image"
echo "::set-output name=repo::$repo"
import os
import itertools
registries = ['docker.io', 'ghcr.io']
repos = ['${{ github.repository }}']
if '${{ github.ref_type }}' == 'branch':
version = 'unknown'
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 = []
- uses: actions/checkout@v2
def join_tag(t):
registry, repo, tag = t
return f'{registry}/{repo}:{tag}'.lower()
# 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
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': join_tag(('localhost', '${{ github.repository }}', 'latest')),
'one_tag': join_tag(('ghcr.io', repos[0], version))
}
with open(os.environ['GITHUB_OUTPUT'], 'a') as out:
for k, v in outputs.items():
out.write(f'{k}={v}\n')
- uses: actions/checkout@v3
# QEMU is used for non-x86_64 builds
- uses: docker/setup-qemu-action@v3
# buildx adds additional features to docker build
- uses: docker/setup-buildx-action@v3
with:
driver-opts: network=host
# save some time during rebuilds
- name: Cache Docker layers
uses: actions/cache@v2

# Here, we want to do the docker build twice:
# The first build pushes to our local registry for testing.
# The second build pushes to Docker Hub and ghcr.io
- name: Build (local only)
uses: docker/build-push-action@v3
id: docker_build
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-
context: .
file: ./Dockerfile
tags: ${{ steps.info.outputs.local_tag }}
load: true
cache-from: type=gha

- name: Login to DockerHub
id: dockerhub_login
uses: docker/login-action@v1
if: (github.event_name == 'push' || github.event_name == 'release') && contains(steps.info.outputs.tags_csv, 'docker.io')
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_PASSWORD }}

- name: Login to GitHub Container Registry
uses: docker/login-action@v1
if: (github.event_name == 'push' || github.event_name == 'release') && contains(steps.info.outputs.tags_csv, 'ghcr.io')
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Build and push
uses: docker/build-push-action@v2
id: docker_build
uses: docker/build-push-action@v3
if: (github.event_name == 'push' || github.event_name == 'release')
with:
context: .
file: ./Dockerfile
tags: |
${{ steps.determine.outputs.dock_image }}
localhost:5000/${{ steps.determine.outputs.dock_image }}
ghcr.io/${{ steps.determine.outputs.dock_image }}
platforms: linux/amd64
push: true
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache
tags: ${{ steps.info.outputs.tags_csv }}
platforms: linux/amd64,linux/arm64,linux/ppc64le
push: ${{ steps.info.outputs.push }}
cache-to: type=gha,mode=max

- name: Get plugin meta
id: pluginmeta
- name: Create plugin description
run: |
repo=${{ steps.determine.outputs.repo }}
dock_image=${{ steps.determine.outputs.dock_image }}
docker pull localhost:5000/$dock_image
docker tag localhost:5000/$dock_image $dock_image
script=$(docker inspect --format '{{ (index .Config.Cmd 0) }}' $dock_image)
json="$(docker run --rm $dock_image $script --json)"
jq <<< "$json" # pretty print in log
echo "::set-output name=json::$json"
echo "::set-output name=title::$(jq -r '.title' <<< "$json")"
docker run --rm ${{ steps.info.outputs.local_tag }} dircopy --json \
| jq '. + {"name": "pl-dircopy", "public_repo": "${{ github.server_url }}/${{ github.repository }}", "dock_image": "${{ steps.info.outputs.one_tag }}"}' > ./Dircopy.json
- name: Upload ChRIS Plugin
id: upload
if: github.ref_type == 'tag'
uses: FNNDSC/upload-chris-plugin@v1
with:
description_file: ./Dircopy.json
username: ${{ secrets.CHRISPROJECT_USERNAME }}
password: ${{ secrets.CHRISPROJECT_PASSWORD }}
chris_url: https://cube.chrisproject.org/api/v1/
compute_names: host

- name: Update DockerHub description
uses: peter-evans/dockerhub-description@v2
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.pluginmeta.outputs.title }}
short-description: ${{ steps.upload.outputs.title }}
readme-filepath: ./README.rst
repository: ${{ steps.determine.outputs.repo }}

- name: Upload to ChRIS Store
if: "!endsWith(steps.determine.outputs.dock_image, ':latest')"
run: |
repo=${{ steps.determine.outputs.repo }}
dock_image=${{ steps.determine.outputs.dock_image }}
descriptor_file=$(mktemp --suffix .json)
cat > $descriptor_file <<< '${{ steps.pluginmeta.outputs.json }}'
res=$(
curl -s -u "${{ secrets.CHRIS_STORE_USER }}" "https://chrisstore.co/api/v1/plugins/" \
-F "name=$(sed 's/^.*\///' <<< $repo)" \
-F "dock_image=$dock_image" \
-F "descriptor_file=@$descriptor_file" \
-F "public_repo=https://github.com/${{ github.repository }}"
)
success=$?
if [ "$success" = "0" ]; then
href="$(jq -r '.collection.items[0].href' <<< "$res")"
echo $href
echo "::set-output name=pluginurl::$href"
else
echo "::error ::Failed upload to ChRIS Store"
echo "$res"
exit $success
fi
61 changes: 0 additions & 61 deletions release.sh

This file was deleted.

0 comments on commit 0bf0b7c

Please sign in to comment.