|
| 1 | +name: Docker |
| 2 | + |
| 3 | +# This will run when: |
| 4 | +# - a new release is created, to make sure the right tags of the |
| 5 | +# docker images are pushed (expects tags to be v1.8.4). |
| 6 | +# - when new code is pushed to main/develop to push the tags |
| 7 | +# latest and develop |
| 8 | +# - when a pull request is created and updated to make sure the |
| 9 | +# Dockerfile is still valid. |
| 10 | +# To be able to push to dockerhub, this execpts the following |
| 11 | +# secrets to be set in the project: |
| 12 | +# - DOCKERHUB_USERNAME : username that can push to the org |
| 13 | +# - DOCKERHUB_PASSWORD : password asscoaited with the username |
| 14 | +on: |
| 15 | + push: |
| 16 | + branches: |
| 17 | + - master |
| 18 | + |
| 19 | + pull_request: |
| 20 | + |
| 21 | +# Certain actions will only run when this is the main repo. |
| 22 | +env: |
| 23 | + MAIN_REPO: clowder-framework/pyclowder |
| 24 | + DOCKERHUB_ORG: clowder |
| 25 | + |
| 26 | +jobs: |
| 27 | + docker: |
| 28 | + runs-on: ubuntu-latest |
| 29 | + permissions: |
| 30 | + packages: write |
| 31 | + |
| 32 | + strategy: |
| 33 | + fail-fast: false |
| 34 | + matrix: |
| 35 | + name: |
| 36 | + - wordcount |
| 37 | + include: |
| 38 | + - name: wordcount |
| 39 | + FOLDER: sample-extractors/wordcount |
| 40 | + PLATFORM: "linux/amd64,linux/arm64" |
| 41 | + steps: |
| 42 | + - uses: actions/checkout@v2 |
| 43 | + |
| 44 | + # set environment variables |
| 45 | + - name: Extractor Version |
| 46 | + run: | |
| 47 | + # find out what the BRANCH is, in case of a PR we will use the PR-<number> |
| 48 | + if [ "${{ github.event.release.target_commitish }}" != "" ]; then |
| 49 | + BRANCH="${{ github.event.release.target_commitish }}" |
| 50 | + elif [[ $GITHUB_REF =~ pull ]]; then |
| 51 | + BRANCH="$(echo $GITHUB_REF | sed 's#refs/pull/\([0-9]*\)/merge#PR-\1#')" |
| 52 | + else |
| 53 | + BRANCH=${GITHUB_REF##*/} |
| 54 | + fi |
| 55 | +
|
| 56 | + # should we push to dockerhub, and is there a README |
| 57 | + DOCKERHUB_PUSH="false" |
| 58 | + DOCKERHUB_README="false" |
| 59 | + if [ "$BRANCH" == "main" -a "${{ github.repository }}" == "${{ env.MAIN_REPO }}" ]; then |
| 60 | + if [ "${{ secrets.DOCKERHUB_USERNAME }}" != "" -a "${{ secrets.DOCKERHUB_PASSWORD }}" != "" ]; then |
| 61 | + DOCKERHUB_PUSH="true" |
| 62 | + if [ -e "${{ matrix.FOLDER }}/README.md" ]; then |
| 63 | + DOCKERHUB_README="true" |
| 64 | + fi |
| 65 | + fi |
| 66 | + fi |
| 67 | +
|
| 68 | + # calculate the version and all tags |
| 69 | + if [ "$BRANCH" == "main" ]; then |
| 70 | + VERSION="$(awk '/"version":/ { print $2 }' ${{ matrix.FOLDER }}/extractor_info.json | sed 's/^.*"\([0-9\.]*\)".*$/\1/')" |
| 71 | + tags="latest" |
| 72 | + oldversion="" |
| 73 | + tmpversion="${VERSION}" |
| 74 | + while [ "${oldversion}" != "${tmpversion}" ]; do |
| 75 | + oldversion="${tmpversion}" |
| 76 | + tags="${tags} ${tmpversion}" |
| 77 | + tmpversion=${tmpversion%.*} |
| 78 | + done |
| 79 | + else |
| 80 | + VERSION="test" |
| 81 | + tags="$BRANCH" |
| 82 | + fi |
| 83 | +
|
| 84 | + # create a list of all images to be pushed |
| 85 | + IMAGE="extractors-${{ matrix.name }}" |
| 86 | + IMAGES="" |
| 87 | + for tag in ${tags}; do |
| 88 | + if [ "$DOCKERHUB_PUSH" == "true" ]; then |
| 89 | + IMAGES="${IMAGES}${{ env.DOCKERHUB_ORG }}/${IMAGE}:${tag}," |
| 90 | + fi |
| 91 | + IMAGES="${IMAGES}ghcr.io/${{ github.repository_owner }}/${IMAGE}:${tag}," |
| 92 | + done |
| 93 | + IMAGES="${IMAGES%,*}" |
| 94 | +
|
| 95 | + # save the results in env |
| 96 | + echo "BRANCH=${BRANCH}" |
| 97 | + echo "VERSION=${VERSION}" |
| 98 | + echo "DOCKERHUB_README=${DOCKERHUB_README}" |
| 99 | + echo "DOCKERHUB_PUSH=${DOCKERHUB_PUSH}" |
| 100 | + echo "IMAGES=${IMAGES}" |
| 101 | +
|
| 102 | + echo "BRANCH=${BRANCH}" >> $GITHUB_ENV |
| 103 | + echo "VERSION=${VERSION}" >> $GITHUB_ENV |
| 104 | + echo "DOCKERHUB_README=${DOCKERHUB_README}" >> $GITHUB_ENV |
| 105 | + echo "DOCKERHUB_PUSH=${DOCKERHUB_PUSH}" >> $GITHUB_ENV |
| 106 | + echo "IMAGES=${IMAGES}" >> $GITHUB_ENV |
| 107 | +
|
| 108 | + # setup docker build |
| 109 | + - name: Set up QEMU |
| 110 | + uses: docker/setup-qemu-action@v2 |
| 111 | + |
| 112 | + - name: Set up Docker Buildx |
| 113 | + id: buildx |
| 114 | + uses: docker/setup-buildx-action@v2 |
| 115 | + |
| 116 | + - name: Inspect Builder |
| 117 | + run: | |
| 118 | + echo "Name: ${{ steps.buildx.outputs.name }}" |
| 119 | + echo "Endpoint: ${{ steps.buildx.outputs.endpoint }}" |
| 120 | + echo "Status: ${{ steps.buildx.outputs.status }}" |
| 121 | + echo "Flags: ${{ steps.buildx.outputs.flags }}" |
| 122 | + echo "Platforms: ${{ steps.buildx.outputs.platforms }}" |
| 123 | +
|
| 124 | + # login to registries |
| 125 | + - name: Login to DockerHub |
| 126 | + if: env.DOCKERHUB_PUSH == 'true' |
| 127 | + uses: docker/login-action@v2 |
| 128 | + with: |
| 129 | + username: ${{ secrets.DOCKERHUB_USERNAME }} |
| 130 | + password: ${{ secrets.DOCKERHUB_PASSWORD }} |
| 131 | + |
| 132 | + - name: Login to GitHub Container Registry |
| 133 | + uses: docker/login-action@v2 |
| 134 | + with: |
| 135 | + registry: ghcr.io |
| 136 | + username: ${{ github.actor }} |
| 137 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 138 | + |
| 139 | + # build the docker images |
| 140 | + - name: Build and push ${{ matrix.name }} |
| 141 | + uses: docker/build-push-action@v2 |
| 142 | + with: |
| 143 | + push: true |
| 144 | + context: ${{ matrix.FOLDER }} |
| 145 | + platforms: ${{ matrix.PLATFORM }} |
| 146 | + cache-from: type=gha,scope=${{ matrix.name }} |
| 147 | + cache-to: type=gha,mode=max,scope=${{ matrix.name }} |
| 148 | + tags: ${{ env.IMAGES }} |
| 149 | + build-args: | |
| 150 | + VERSION=${{ env.VERSION }} |
| 151 | + BUILDNUMBER=${{ github.run_number }} |
| 152 | + GITSHA1=${{ github.sha }} |
| 153 | +
|
| 154 | + # this will update the README of the dockerhub repo |
| 155 | + - name: Docker Hub Description |
| 156 | + if: env.DOCKERHUB_README == 'true' |
| 157 | + uses: peter-evans/dockerhub-description@v2 |
| 158 | + env: |
| 159 | + DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }} |
| 160 | + DOCKERHUB_PASSWORD: ${{ secrets.DOCKERHUB_PASSWORD }} |
| 161 | + DOCKERHUB_REPOSITORY: ${{ env.DOCKERHUB_ORG }}/extractors-${{ matrix.NAME }} |
| 162 | + README_FILEPATH: ${{ matrix.FOLDER }}/README.md |
0 commit comments