|
| 1 | +name: CI |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + push: |
| 6 | + branches: |
| 7 | + - main |
| 8 | + pull_request: |
| 9 | + types: |
| 10 | + - opened |
| 11 | + - reopened |
| 12 | + schedule: |
| 13 | + - cron: '30 * * * *' |
| 14 | + |
| 15 | +jobs: |
| 16 | + build: |
| 17 | + runs-on: ubuntu-latest |
| 18 | + if: "!contains(github.event.head_commit.message, '[ci-skip]')" |
| 19 | + steps: |
| 20 | + - name: Checkout |
| 21 | + uses: actions/checkout@v2 |
| 22 | + |
| 23 | + - name: Check if we should rebuild |
| 24 | + id: build_check |
| 25 | + run: | |
| 26 | + if [ "${{github.event_name}}" != "schedule" ]; then |
| 27 | + echo "**** event was not cron, rebuilding ****" |
| 28 | + echo ::set-output name=update_available::true |
| 29 | + exit 0 |
| 30 | + fi |
| 31 | +
|
| 32 | + echo "**** Retrieving external version ****" |
| 33 | + EXT_RELEASE=$(curl -sL https://www.tp-link.com/uk/support/download/omada-software-controller/ | egrep -m 1 -o 'https?://[^ ]+.tar.gz' | awk -F "/" '{print $NF}' | awk -F '_' '{print $4}') |
| 34 | + if [ -z "${EXT_RELEASE}" ] || [ "${EXT_RELEASE}" == "null" ]; then |
| 35 | + echo "**** Can't retrieve external version, exiting ****" |
| 36 | + exit 1 |
| 37 | + fi |
| 38 | +
|
| 39 | + EXT_RELEASE=$(echo ${EXT_RELEASE} | sed 's/[~,%@+;:/]//g') |
| 40 | +
|
| 41 | + echo "**** External version: ${EXT_RELEASE} ****" |
| 42 | +
|
| 43 | + echo "**** Retrieving last pushed version ****" |
| 44 | +
|
| 45 | + image="linuxserver-labs/omada-controller" |
| 46 | + tag="latest" |
| 47 | + token=$(curl -sX GET \ |
| 48 | + "https://ghcr.io/token?scope=repository%3Alinuxserver-labs%2Fomada-controller%3Apull" \ |
| 49 | + | jq -r '.token') |
| 50 | + multidigest=$(curl -s \ |
| 51 | + --header "Accept: application/vnd.docker.distribution.manifest.v2+json" \ |
| 52 | + --header "Authorization: Bearer ${token}" \ |
| 53 | + "https://ghcr.io/v2/${image}/manifests/${tag}" \ |
| 54 | + | jq -r 'first(.manifests[].digest)') |
| 55 | + digest=$(curl -s \ |
| 56 | + --header "Accept: application/vnd.docker.distribution.manifest.v2+json" \ |
| 57 | + --header "Authorization: Bearer ${token}" \ |
| 58 | + "https://ghcr.io/v2/${image}/manifests/${multidigest}" \ |
| 59 | + | jq -r '.config.digest') |
| 60 | + image_info=$(curl -sL \ |
| 61 | + --header "Authorization: Bearer ${token}" \ |
| 62 | + "https://ghcr.io/v2/${image}/blobs/${digest}" \ |
| 63 | + | jq -r '.config') |
| 64 | +
|
| 65 | + IMAGE_RELEASE=$(echo ${image_info} | jq -r '.Labels.build_version' | awk '{print $3}') |
| 66 | + IMAGE_VERSION=$(echo ${IMAGE_RELEASE} | awk -F'-ls' '{print $1}') |
| 67 | +
|
| 68 | + if [ -z "${IMAGE_VERSION}" ]; then |
| 69 | + echo "**** Can't retrieve last pushed version, exiting ****" |
| 70 | + exit 1 |
| 71 | + fi |
| 72 | +
|
| 73 | + echo "**** Last pushed version: ${IMAGE_VERSION} ****" |
| 74 | +
|
| 75 | + if [ "${EXT_RELEASE}" == "${IMAGE_VERSION}" ]; then |
| 76 | + echo "**** Version ${EXT_RELEASE} already pushed, not rebuilding ****" |
| 77 | + echo ::set-output name=update_available::false |
| 78 | + exit 0 |
| 79 | + fi |
| 80 | +
|
| 81 | + echo ::set-output name=update_available::true |
| 82 | +
|
| 83 | + - name: Prepare |
| 84 | + id: prep |
| 85 | + run: | |
| 86 | + REGISTRY_IMAGE=$(echo "ghcr.io/$GITHUB_REPOSITORY" | sed 's/docker-//') |
| 87 | + VERSION=$(curl -sL https://www.tp-link.com/uk/support/download/omada-software-controller/ | egrep -m 1 -o 'https?://[^ ]+.tar.gz' | awk -F "/" '{print $NF}' | awk -F '_' '{print $4}') |
| 88 | + if [[ $GITHUB_REF == refs/tags/v* ]]; then |
| 89 | + VERSION=${GITHUB_REF#refs/tags/} |
| 90 | + fi |
| 91 | +
|
| 92 | + SEMVER=$(date '+%Y.%m.%d') |
| 93 | + semver_regex='([0-9]+)\.([0-9]+)\.([0-9]+)$' |
| 94 | + if [[ $VERSION =~ $semver_regex ]]; then |
| 95 | + SEMVER="${BASH_REMATCH[1]}.${BASH_REMATCH[2]}.${BASH_REMATCH[3]}" |
| 96 | + else |
| 97 | + semver_regex='([0-9]+)\.([0-9]+)(\.([0-9]+))?.*' |
| 98 | + if [[ $VERSION =~ $semver_regex ]]; then |
| 99 | + echo 'here' |
| 100 | + if [[ ${BASH_REMATCH[4]} ]]; then |
| 101 | + SEMVER="${BASH_REMATCH[1]}.${BASH_REMATCH[2]}.${BASH_REMATCH[4]}" |
| 102 | + else |
| 103 | + SEMVER="${BASH_REMATCH[1]}.${BASH_REMATCH[2]}.$(date '+%Y%m%d')" |
| 104 | + fi |
| 105 | + fi |
| 106 | + fi |
| 107 | +
|
| 108 | + TAGS="${REGISTRY_IMAGE}:${VERSION},${REGISTRY_IMAGE}:latest,${REGISTRY_IMAGE}:${SEMVER}" |
| 109 | + echo ::set-output name=tags::${TAGS} |
| 110 | +
|
| 111 | + if [ "${{github.event_name}}" == "pull_request" ]; then |
| 112 | + echo ::set-output name=push::false |
| 113 | + echo ::set-output name=cache_from::"type=registry,ref=${REGISTRY_IMAGE}:buildcache" |
| 114 | + echo ::set-output name=cache_to::"" |
| 115 | + else |
| 116 | + echo ::set-output name=push::true |
| 117 | + echo ::set-output name=cache_from::"type=registry,ref=${REGISTRY_IMAGE}:buildcache" |
| 118 | + echo ::set-output name=cache_to::"type=registry,ref=${REGISTRY_IMAGE}:buildcache,mode=max" |
| 119 | + fi |
| 120 | +
|
| 121 | + echo ::set-output name=github_server_url::"${GITHUB_SERVER_URL}" |
| 122 | + echo ::set-output name=BUILD_DATE::$(date '+%Y-%m-%dT%H:%M:%S%:z') |
| 123 | + echo ::set-output name=VERSION::${VERSION} |
| 124 | +
|
| 125 | + - name: Set up QEMU |
| 126 | + uses: docker/setup-qemu-action@v1 |
| 127 | + with: |
| 128 | + platforms: all |
| 129 | + |
| 130 | + - name: Set up Docker Buildx |
| 131 | + id: buildx |
| 132 | + uses: docker/setup-buildx-action@v1 |
| 133 | + with: |
| 134 | + install: true |
| 135 | + version: latest |
| 136 | + driver-opts: image=moby/buildkit:master |
| 137 | + |
| 138 | + - name: Login to GitHub Container Registry |
| 139 | + if: github.event_name != 'pull_request' |
| 140 | + uses: docker/login-action@v1 |
| 141 | + with: |
| 142 | + registry: ghcr.io |
| 143 | + username: linuxserver-labs |
| 144 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 145 | + |
| 146 | + - name: Build and Push |
| 147 | + id: docker_build |
| 148 | + if: ${{ steps.build_check.outputs.update_available == 'true' }} |
| 149 | + uses: docker/build-push-action@v2 |
| 150 | + with: |
| 151 | + builder: ${{ steps.buildx.outputs.name }} |
| 152 | + context: . |
| 153 | + file: ./Dockerfile |
| 154 | + platforms: linux/amd64,linux/arm64 |
| 155 | + push: ${{ steps.prep.outputs.push }} |
| 156 | + tags: ${{ steps.prep.outputs.tags }} |
| 157 | + build-args: | |
| 158 | + IMAGE_SOURCE=${{ steps.prep.outputs.github_server_url }}/${{ github.repository }} |
| 159 | + BUILD_DATE=${{ steps.prep.outputs.BUILD_DATE }} |
| 160 | + VERSION=${{ steps.prep.outputs.VERSION }} |
| 161 | + cache-from: ${{ steps.prep.outputs.cache_from }} |
| 162 | + cache-to: ${{ steps.prep.outputs.cache_to }} |
| 163 | + |
| 164 | + - name: Image digest |
| 165 | + run: echo ${{ steps.docker_build.outputs.digest }} |
0 commit comments