|
| 1 | +name: External Trigger Main |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + |
| 6 | +jobs: |
| 7 | + external-trigger-master: |
| 8 | + runs-on: ubuntu-latest |
| 9 | + steps: |
| 10 | + |
| 11 | + |
| 12 | + - name: External Trigger |
| 13 | + if: github.ref == 'refs/heads/master' |
| 14 | + run: | |
| 15 | + if [ -n "${{ secrets.PAUSE_EXTERNAL_TRIGGER_HOMEASSISTANT_MASTER }}" ]; then |
| 16 | + echo "**** Github secret PAUSE_EXTERNAL_TRIGGER_HOMEASSISTANT_MASTER is set; skipping trigger. ****" |
| 17 | + exit 0 |
| 18 | + fi |
| 19 | + echo "**** External trigger running off of master branch. To disable this trigger, set a Github secret named \"PAUSE_EXTERNAL_TRIGGER_HOMEASSISTANT_MASTER\". ****" |
| 20 | + echo "**** Retrieving external version ****" |
| 21 | + EXT_RELEASE=$(curl -sL "https://pypi.python.org/pypi/homeassistant/json" |jq -r '. | .info.version') |
| 22 | + if [ -z "${EXT_RELEASE}" ] || [ "${EXT_RELEASE}" == "null" ]; then |
| 23 | + echo "**** Can't retrieve external version, exiting ****" |
| 24 | + FAILURE_REASON="Can't retrieve external version for homeassistant branch master" |
| 25 | + GHA_TRIGGER_URL="https://github.com/linuxserver/docker-homeassistant/actions/runs/${{ github.run_id }}" |
| 26 | + curl -X POST -H "Content-Type: application/json" --data '{"avatar_url": "https://cdn.discordapp.com/avatars/354986384542662657/df91181b3f1cf0ef1592fbe18e0962d7.png","embeds": [{"color": 16711680, |
| 27 | + "description": "**Trigger Failed** \n**Reason:** '"${FAILURE_REASON}"' \n**Trigger URL:** '"${GHA_TRIGGER_URL}"' \n"}], |
| 28 | + "username": "Github Actions"}' ${{ secrets.DISCORD_WEBHOOK }} |
| 29 | + exit 1 |
| 30 | + fi |
| 31 | + EXT_RELEASE=$(echo ${EXT_RELEASE} | sed 's/[~,%@+;:/]//g') |
| 32 | + echo "**** External version: ${EXT_RELEASE} ****" |
| 33 | + echo "**** Retrieving last pushed version ****" |
| 34 | + image="linuxserver/homeassistant" |
| 35 | + tag="latest" |
| 36 | + token=$(curl -sX GET \ |
| 37 | + "https://ghcr.io/token?scope=repository%3Alinuxserver%2Fhomeassistant%3Apull" \ |
| 38 | + | jq -r '.token') |
| 39 | + multidigest=$(curl -s \ |
| 40 | + --header "Accept: application/vnd.docker.distribution.manifest.v2+json" \ |
| 41 | + --header "Authorization: Bearer ${token}" \ |
| 42 | + "https://ghcr.io/v2/${image}/manifests/${tag}" \ |
| 43 | + | jq -r 'first(.manifests[].digest)') |
| 44 | + digest=$(curl -s \ |
| 45 | + --header "Accept: application/vnd.docker.distribution.manifest.v2+json" \ |
| 46 | + --header "Authorization: Bearer ${token}" \ |
| 47 | + "https://ghcr.io/v2/${image}/manifests/${multidigest}" \ |
| 48 | + | jq -r '.config.digest') |
| 49 | + image_info=$(curl -sL \ |
| 50 | + --header "Authorization: Bearer ${token}" \ |
| 51 | + "https://ghcr.io/v2/${image}/blobs/${digest}" \ |
| 52 | + | jq -r '.container_config') |
| 53 | + IMAGE_RELEASE=$(echo ${image_info} | jq -r '.Labels.build_version' | awk '{print $3}') |
| 54 | + IMAGE_VERSION=$(echo ${IMAGE_RELEASE} | awk -F'-ls' '{print $1}') |
| 55 | + if [ -z "${IMAGE_VERSION}" ]; then |
| 56 | + echo "**** Can't retrieve last pushed version, exiting ****" |
| 57 | + FAILURE_REASON="Can't retrieve last pushed version for homeassistant tag latest" |
| 58 | + curl -X POST -H "Content-Type: application/json" --data '{"avatar_url": "https://cdn.discordapp.com/avatars/354986384542662657/df91181b3f1cf0ef1592fbe18e0962d7.png","embeds": [{"color": 16711680, |
| 59 | + "description": "**Trigger Failed** \n**Reason:** '"${FAILURE_REASON}"' \n"}], |
| 60 | + "username": "Github Actions"}' ${{ secrets.DISCORD_WEBHOOK }} |
| 61 | + exit 1 |
| 62 | + fi |
| 63 | + echo "**** Last pushed version: ${IMAGE_VERSION} ****" |
| 64 | + if [ "${EXT_RELEASE}" == "${IMAGE_VERSION}" ]; then |
| 65 | + echo "**** Version ${EXT_RELEASE} already pushed, exiting ****" |
| 66 | + exit 0 |
| 67 | + elif [ $(curl -s https://ci.linuxserver.io/job/Docker-Pipeline-Builders/job/docker-homeassistant/job/master/lastBuild/api/json | jq -r '.building') == "true" ]; then |
| 68 | + echo "**** New version ${EXT_RELEASE} found; but there already seems to be an active build on Jenkins; exiting ****" |
| 69 | + exit 0 |
| 70 | + else |
| 71 | + echo "**** New version ${EXT_RELEASE} found; old version was ${IMAGE_VERSION}. Triggering new build ****" |
| 72 | + response=$(curl -iX POST \ |
| 73 | + https://ci.linuxserver.io/job/Docker-Pipeline-Builders/job/docker-homeassistant/job/master/buildWithParameters?PACKAGE_CHECK=false \ |
| 74 | + --user ${{ secrets.JENKINS_USER }}:${{ secrets.JENKINS_TOKEN }} | grep -i location | sed "s|^[L|l]ocation: \(.*\)|\1|") |
| 75 | + echo "**** Jenkins job queue url: ${response%$'\r'} ****" |
| 76 | + echo "**** Sleeping 10 seconds until job starts ****" |
| 77 | + sleep 10 |
| 78 | + buildurl=$(curl -s "${response%$'\r'}api/json" | jq -r '.executable.url') |
| 79 | + buildurl="${buildurl%$'\r'}" |
| 80 | + echo "**** Jenkins job build url: ${buildurl} ****" |
| 81 | + echo "**** Attempting to change the Jenkins job description ****" |
| 82 | + curl -iX POST \ |
| 83 | + "${buildurl}submitDescription" \ |
| 84 | + --user ${{ secrets.JENKINS_USER }}:${{ secrets.JENKINS_TOKEN }} \ |
| 85 | + --data-urlencode "description=GHA external trigger https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}" \ |
| 86 | + --data-urlencode "Submit=Submit" |
| 87 | + echo "**** Notifying Discord ****" |
| 88 | + TRIGGER_REASON="A version change was detected for homeassistant tag latest. Old version:${IMAGE_VERSION} New version:${EXT_RELEASE}" |
| 89 | + curl -X POST -H "Content-Type: application/json" --data '{"avatar_url": "https://cdn.discordapp.com/avatars/354986384542662657/df91181b3f1cf0ef1592fbe18e0962d7.png","embeds": [{"color": 9802903, |
| 90 | + "description": "**Build Triggered** \n**Reason:** '"${TRIGGER_REASON}"' \n**Build URL:** '"${buildurl}display/redirect"' \n"}], |
| 91 | + "username": "Github Actions"}' ${{ secrets.DISCORD_WEBHOOK }} |
| 92 | + fi |
0 commit comments