|
| 1 | +name: Build Notebook Images |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [ master ] |
| 6 | + tags: |
| 7 | + - 'v*.*.*' |
| 8 | + pull_request: |
| 9 | + branches: |
| 10 | + - 'master' |
| 11 | + |
| 12 | +jobs: |
| 13 | + build-matrix: |
| 14 | + name: Create Build Matrix |
| 15 | + runs-on: ubuntu-latest |
| 16 | + steps: |
| 17 | + - |
| 18 | + name: Checkout |
| 19 | + id: set-matrix |
| 20 | + uses: actions/checkout@v2 |
| 21 | + with: |
| 22 | + fetch-depth: 0 # No shallow clone, we need all history |
| 23 | + - |
| 24 | + name: Bump version and push tag |
| 25 | + if: github.event_name != 'pull_request' |
| 26 | + id: tag_version |
| 27 | + uses: mathieudutour/[email protected] |
| 28 | + with: |
| 29 | + github_token: ${{ secrets.GITHUB_TOKEN }} |
| 30 | + - |
| 31 | + name: Create a GitHub release |
| 32 | + if: github.event_name != 'pull_request' |
| 33 | + uses: actions/create-release@v1 |
| 34 | + env: |
| 35 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 36 | + with: |
| 37 | + tag_name: ${{ steps.tag_version.outputs.new_tag }} |
| 38 | + release_name: Release ${{ steps.tag_version.outputs.new_tag }} |
| 39 | + body: ${{ steps.tag_version.outputs.changelog }} |
| 40 | + - name: generate matrix |
| 41 | + id: generate-matrix |
| 42 | + env: |
| 43 | + RELEASE: ${{ steps.tag_version.outputs.new_tag }} |
| 44 | + # run: echo "::set-output name=matrix::{\"include\":[$(for changed_folder in $(dirname $(git diff --name-only ${{ github.event.before }}..${{ github.event.after }}) | sort -u); do find $changed_folder -name "*Dockerfile"; done | sed 's/^\|$/"/g'|paste -sd, -)]}" |
| 45 | + run: | |
| 46 | + if [ -z "${RELEASE}" ]; |
| 47 | + then |
| 48 | + RELEASE="pr" |
| 49 | + CHANGED_DIRS=$(git diff-tree --no-commit-id --name-only -r ${{ github.event.pull_request.head.sha }} | xargs -I {} dirname {}) |
| 50 | + else |
| 51 | + CHANGED_DIRS=$(git diff-tree --no-commit-id --name-only -r ${{ github.sha }} | xargs -I {} dirname {}) |
| 52 | + fi |
| 53 | + if [ -z "${CHANGED_DIRS}" ]; |
| 54 | + then |
| 55 | + CHANGED_DIRS=$(dirname $(git diff --name-only ${{ github.event.before }}..${{ github.event.after }})) |
| 56 | + if [ "${CHANGED_DIRS}" == "." ]; |
| 57 | + then |
| 58 | + CHANGED_DIRS= |
| 59 | + fi |
| 60 | + fi |
| 61 | + echo "${CHANGED_DIRS}" |
| 62 | + DOCKERFILES=$(for CHANGED_DIR in ${CHANGED_DIRS}; do find ${CHANGED_DIR} -name "*Dockerfile"; done | sort -u) |
| 63 | + echo "${DOCKERFILES}" |
| 64 | + MATRIX_PROJECTS_JSON="[" |
| 65 | + MATRIX_INCLUDE_JSON="[" |
| 66 | + for DOCKERFILE in ${DOCKERFILES}; do |
| 67 | + DIR=$(dirname ${DOCKERFILE}) |
| 68 | + if [[ "$(basename ${DOCKERFILE})" == *"cuda"* ]] |
| 69 | + then |
| 70 | + MATRIX_PROJECTS_JSON+=$(sed 's/^/"/;s/$/"/' <<< "${DIR}"-cuda) |
| 71 | + PROJECT="${DIR}"-cuda |
| 72 | + else |
| 73 | + MATRIX_PROJECTS_JSON+=$(sed 's/^/"/;s/$/"/' <<< "${DIR}") |
| 74 | + PROJECT="${DIR}" |
| 75 | + fi |
| 76 | + echo "{$MATRIX_PROJECTS_JSON}" |
| 77 | + MATRIX_INCLUDE_JSON+="{\"path\": \"${DIR}\", \"project\": \"${PROJECT}\", \"dockerfile\": \"${DOCKERFILE}\", \"version\": \"${RELEASE}\"}" |
| 78 | + echo "${MATRIC_INCLUDE_JSON}" |
| 79 | + done |
| 80 | + echo "{$MATRIX_PROJECTS_JSON}" |
| 81 | + echo "${MATRIC_INCLUDE_JSON}" |
| 82 | + MATRIX_INCLUDE_JSON="${MATRIX_INCLUDE_JSON//\}\{/\}, \{}" |
| 83 | + MATRIX_INCLUDE_JSON+="]" |
| 84 | + MATRIX_PROJECTS_JSON="${MATRIX_PROJECTS_JSON//\"\"/\", \"}" |
| 85 | + MATRIX_PROJECTS_JSON+="]" |
| 86 | + MATRIX_JSON="{\"include\": ${MATRIX_INCLUDE_JSON}}" |
| 87 | + echo "${MATRIX_JSON}" |
| 88 | + |
| 89 | + CONTINUE_DOCKER_JOB="no" |
| 90 | + if [[ "${MATRIX_PROJECTS_JSON}" != "[]" ]]; then |
| 91 | + CONTINUE_DOCKER_JOB="yes" |
| 92 | + fi |
| 93 | +
|
| 94 | + echo "${CONTINUE_DOCKER_JOB}" |
| 95 | +
|
| 96 | + echo "::set-output name=continue::${CONTINUE_DOCKER_JOB}" |
| 97 | + echo "::set-output name=matrix::${MATRIX_JSON}" |
| 98 | + outputs: |
| 99 | + matrix: ${{ steps.generate-matrix.outputs.matrix }} |
| 100 | + continue: ${{ steps.generate-matrix.outputs.continue }} |
| 101 | + build-images: |
| 102 | + if: needs.build-matrix.outputs.continue == 'yes' |
| 103 | + name: Build and push notebook images |
| 104 | + runs-on: ubuntu-latest |
| 105 | + needs: build-matrix |
| 106 | + strategy: |
| 107 | + matrix: ${{ fromJson(needs.build-matrix.outputs.matrix) }} |
| 108 | + steps: |
| 109 | + - |
| 110 | + name: Checkout |
| 111 | + uses: actions/checkout@v2 |
| 112 | + - |
| 113 | + name: Get latest tag |
| 114 | + id: latest_tag |
| 115 | + |
| 116 | + with: |
| 117 | + img: 'ghcr.io/pluralsh/kubeflow-notebooks-${{ matrix.project }}' |
| 118 | + - |
| 119 | + name: Docker meta |
| 120 | + id: meta |
| 121 | + uses: crazy-max/ghaction-docker-meta@v3 |
| 122 | + with: |
| 123 | + # list of Docker images to use as base name for tags |
| 124 | + images: | |
| 125 | + ghcr.io/pluralsh/kubeflow-notebooks-${{ matrix.project }} |
| 126 | + # generate Docker tags based on the following events/attributes |
| 127 | + tags: | |
| 128 | + type=ref,event=branch |
| 129 | + type=ref,event=pr |
| 130 | + type=semver,pattern={{raw}},value=${{ matrix.project }} |
| 131 | + type=sha |
| 132 | + - |
| 133 | + name: Set up QEMU |
| 134 | + uses: docker/setup-qemu-action@v1 |
| 135 | + - |
| 136 | + name: Set up Docker Buildx |
| 137 | + uses: docker/setup-buildx-action@v1 |
| 138 | + - |
| 139 | + name: Login to GHCR |
| 140 | + if: github.event_name != 'pull_request' |
| 141 | + uses: docker/login-action@v1 |
| 142 | + with: |
| 143 | + registry: ghcr.io |
| 144 | + username: ${{ github.repository_owner }} |
| 145 | + password: ${{ secrets.MY_GITHUB_TOKEN }} |
| 146 | + - |
| 147 | + name: Build and push |
| 148 | + uses: docker/build-push-action@v2 |
| 149 | + with: |
| 150 | + context: ${{ matrix.path }} |
| 151 | + file: ${{ matrix.dockerfile }} |
| 152 | + push: ${{ github.event_name != 'pull_request' }} |
| 153 | + tags: ${{ steps.meta.outputs.tags }} |
| 154 | + labels: ${{ steps.meta.outputs.labels }} |
0 commit comments