Compare commits
56 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 8e833a04b5 | |||
| 10e171369c | |||
| 1ba91a85c7 | |||
| f00364177f | |||
| 79ef9a5d2d | |||
| a7ee8446cb | |||
| bbb9cb8d3d | |||
| 3b21bb2447 | |||
| fe4be4dd25 | |||
| d63050c1c5 | |||
| 33c30e47d7 | |||
| 8345924a36 | |||
| 2fa59436a2 | |||
| 4daf73bcb8 | |||
| bd48217553 | |||
| 4f68eb9cd8 | |||
| e0159d3c55 | |||
| 1494376085 | |||
| 94a301bdb2 | |||
| 5b04672447 | |||
| dd3e15f046 | |||
| 6dd9032f10 | |||
| d6da5c8df6 | |||
| 8ea218fb69 | |||
| 257808f9f6 | |||
| a8d18c5ea7 | |||
| de33b00708 | |||
| 629627c932 | |||
| 38c5ff32db | |||
| 312fa12d04 | |||
| 88132ab9a5 | |||
| d722c1f28f | |||
| 8c565255d8 | |||
| 25da569881 | |||
| b1acc80b72 | |||
| 711c8c3215 | |||
| 468560068c | |||
| 5cd265f7b9 | |||
| 17bad16719 | |||
| 0daa6da70f | |||
| 2417ee8687 | |||
| bdf803c1f6 | |||
| f77e33e8b0 | |||
| a65d010e73 | |||
| ca677224c2 | |||
| a9e328e00c | |||
| 42dacd7a2a | |||
| 859ea03824 | |||
| e1590f94d4 | |||
| 9d2e763cbe | |||
| 884ebcd21e | |||
| 3823ddf8a6 | |||
| 7c01b16e88 | |||
| 07925fd895 | |||
| 9dde049945 | |||
| 74800fee55 |
24
.woodpecker/build-dev.yml
Normal file
24
.woodpecker/build-dev.yml
Normal file
@@ -0,0 +1,24 @@
|
||||
when:
|
||||
branch: dev
|
||||
path:
|
||||
include: [ '.woodpecker/build-dev.yml', 'Dockerfile', 'check.sh' ]
|
||||
|
||||
pipeline:
|
||||
build_and_publish_gitea:
|
||||
image: woodpeckerci/plugin-docker-buildx
|
||||
registry: git.narvas.tech
|
||||
settings:
|
||||
repo: vistanarvas/container-update-checker,git.narvas.tech/vista/container-update-checker
|
||||
tag: dev
|
||||
auto_tag: false
|
||||
logins:
|
||||
# Default DockerHub login
|
||||
- registry: https://index.docker.io/v1/
|
||||
username: vistanarvas
|
||||
password:
|
||||
from_secret: DOCKERHUB_TOKEN
|
||||
# Additional Gitea login
|
||||
- registry: https://git.narvas.tech
|
||||
username: vista
|
||||
password:
|
||||
from_secret: GITEA_TOKEN
|
||||
@@ -1,3 +1,6 @@
|
||||
when:
|
||||
branch: master
|
||||
|
||||
pipeline:
|
||||
check_self:
|
||||
# failure: ignore # this doesnt work
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
FROM alpine:latest
|
||||
|
||||
RUN apk add curl jq
|
||||
RUN apk add --no-cache curl jq dateutils
|
||||
|
||||
COPY check.sh /usr/local/bin/check
|
||||
RUN chmod +x /usr/local/bin/check
|
||||
|
||||
29
README.md
29
README.md
@@ -1,4 +1,31 @@
|
||||
# container-update-checker
|
||||
[](https://woodpecker.narvas.tech/vista/container-update-checker)
|
||||
|
||||
check if a container needs rebuilding
|
||||
[](https://woodpecker.narvas.tech/vista/container-update-checker)
|
||||
|
||||
## Environment variables
|
||||
| Name | Comment
|
||||
|-------------------|-----------
|
||||
| BASE | the image the Dockerfile is based on
|
||||
| TARGET | the image thats based of `BASE`
|
||||
| REGISTRY_USERNAME | the username for logging in into the registry<br>this is only requered of private repos
|
||||
| REGISTRY_PASSWORD | the password for logging in into the registry<br>(*you probaly want to use a token here*)
|
||||
| REGISTRY_API | the type of registry valid options are `docker` and `gitea`<br>Default: `docker`
|
||||
| REGISTRY_URL | the url to the registry<br>Default: `https://hub.docker.com`
|
||||
| BASE_REGISTRY_USERNAME | if left empty `REGISTRY_USERNAME` is used
|
||||
| BASE_REGISTRY_PASSWORD | if left empty `REGISTRY_PASSWORD` is used
|
||||
| BASE_REGISTRY_API | if left empty `REGISTRY_API` is used
|
||||
| BASE_REGISTRY_URL | if left empty `REGISTRY_URL` is used
|
||||
| TARGET_REGISTRY_USERNAME | if left empty `REGISTRY_USERNAME` is used
|
||||
| TARGET_REGISTRY_PASSWORD | if left empty `REGISTRY_PASSWORD` is used
|
||||
| TARGET_REGISTRY_API | if left empty `REGISTRY_API` is used
|
||||
| TARGET_REGISTRY_URL | if left empty `REGISTRY_URL` is used
|
||||
|
||||
|
||||
## Exit codes
|
||||
|
||||
| State | Exit code | Comment
|
||||
|-------------------------------------|-----------|--------
|
||||
| `BASE` is more recent than `TARGET` | 0 | An update is needed
|
||||
| `TARGET` is more recent than `BASE` | 1 | No update is needed<br>(*the pipeline wil "fail" but that is wat we want* )
|
||||
| An error occured | 0 | So the pipeline can continue
|
||||
126
check.sh
126
check.sh
@@ -23,42 +23,128 @@ if [ -z "${TARGET}" ]; then
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [ -z "${REGISTRY_USERNAME}" ] && [ -n "${DOCKER_USERNAME}" ]; then
|
||||
REGISTRY_USERNAME="${DOCKER_USERNAME}"
|
||||
echo "WARNING: DOCKER_USERNAME is deprecated and will be removed in the next version"
|
||||
echo "you should switch to using REGISTRY_USERNAME"
|
||||
echo "see the documentation for more info https://git.narvas.tech/vista/container-update-checker"
|
||||
fi
|
||||
|
||||
# add "library/" if its a "official" docker image (it has no username)
|
||||
if [ -z "${REGISTRY_PASSWORD}" ] && [ -n "${DOCKER_PASSWORD}" ]; then
|
||||
REGISTRY_PASSWORD="${DOCKER_PASSWORD}"
|
||||
echo "WARNING: DOCKER_PASSWORD is deprecated and will be removed in the next version"
|
||||
echo "you should switch to using REGISTRY_PASSWORD"
|
||||
echo "see the documentation for more info https://git.narvas.tech/vista/container-update-checker"
|
||||
fi
|
||||
|
||||
if [ -z "${BASE_REGISTRY_USERNAME}" ] && [ -n "${REGISTRY_USERNAME}" ]; then
|
||||
BASE_REGISTRY_USERNAME="${REPO_USERNAME}"
|
||||
fi
|
||||
|
||||
if [ -z "${BASE_REGISTRY_PASSWORD}" ] && [ -n "${REGISTRY_PASSWORD}" ]; then
|
||||
BASE_REGISTRY_PASSWORD="${REPO_PASSWORD}"
|
||||
fi
|
||||
|
||||
if [ -z "${TARGET_REGISTRY_USERNAME}" ] && [ -n "${REGISTRY_USERNAME}" ]; then
|
||||
TARGET_REGISTRY_USERNAME="${REPO_USERNAME}"
|
||||
fi
|
||||
|
||||
if [ -z "${TARGET_REGISTRY_PASSWORD}" ] && [ -n "${REGISTRY_PASSWORD}" ]; then
|
||||
TARGET_REGISTRY_PASSWORD="${REPO_PASSWORD}"
|
||||
fi
|
||||
|
||||
if [ -z "${REGISTRY_API}" ]; then
|
||||
REGISTRY_API='docker'
|
||||
fi
|
||||
|
||||
if [ -z "${BASE_REGISTRY_API}" ]; then
|
||||
BASE_REGISTRY_API="${REGISTRY_API}"
|
||||
fi
|
||||
|
||||
if [ -z "${TARGET_REGISTRY_API}" ]; then
|
||||
TARGET_REGISTRY_API="${REGISTRY_API}"
|
||||
fi
|
||||
|
||||
if [ -z "${REGISTRY_URL}" ]; then
|
||||
REGISTRY_URL="https://hub.docker.com"
|
||||
fi
|
||||
|
||||
if [ -z "${BASE_REGISTRY_URL}" ]; then
|
||||
BASE_REGISTRY_URL="${REGISTRY_URL}"
|
||||
fi
|
||||
|
||||
if [ -z "${TARGET_REGISTRY_URL}" ]; then
|
||||
TARGET_REGISTRY_URL="${REGISTRY_URL}"
|
||||
fi
|
||||
|
||||
# remove trailing /
|
||||
BASE_REGISTRY_URL="${BASE_REGISTRY_URL%/}"
|
||||
TARGET_REGISTRY_URL="${TARGET_REGISTRY_URL%/}"
|
||||
|
||||
# add "library/" if its a "official" docker image (no username is provided)
|
||||
echo "${BASE}" | grep -q "/" || BASE="library/${BASE}"
|
||||
echo "${TARGET}" | grep -q "/" || TARGET="library/${TARGET}"
|
||||
|
||||
# if no tag is give default to latest
|
||||
# if no tag is given default to latest
|
||||
echo "${BASE}" | grep -q ":" || BASE="${BASE}:latest"
|
||||
echo "${TARGET}" | grep -q ":" || TARGET="${TARGET}:latest"
|
||||
|
||||
# split repo into user reponame and tag
|
||||
BASE_REPO_USER=$(echo "$BASE" | cut -d'/' -f1)
|
||||
BASE_REPO_NAME=$(echo "$BASE" | cut -d'/' -f2 | cut -d':' -f1)
|
||||
BASE_REPO_TAG=$(echo "$BASE" | cut -d':' -f2)
|
||||
|
||||
# move the tag to its own var and remove it from the repo name
|
||||
BASE_TAG=$(echo "${BASE}" | sed 's/[^:]*://')
|
||||
TARGET_TAG=$(echo "${TARGET}" | sed 's/[^:]*://')
|
||||
TARGET_REPO_USER=$(echo "$TARGET" | cut -d'/' -f1)
|
||||
TARGET_REPO_NAME=$(echo "$TARGET" | cut -d'/' -f2 | cut -d':' -f1)
|
||||
TARGET_REPO_TAG=$(echo "$TARGET" | cut -d':' -f2)
|
||||
|
||||
BASE_REPO=$(echo "${BASE}" | sed 's/:.*//')
|
||||
TARGET_REPO=$(echo "${TARGET}" | sed 's/:.*//')
|
||||
BASE_HEADER=""
|
||||
if [ "${BASE_REGISTRY_API}" = 'docker' ];then
|
||||
if [ -n "${BASE_REGISTRY_USERNAME}" ] && [ -n "${BASE_REGISTRY_PASSWORD}" ]; then
|
||||
TOKEN=$(curl -s -H "Content-Type: application/json" -X POST -d "{\"username\": \"${BASE_REGISTRY_USERNAME}\", \"password\": \"${BASE_REGISTRY_PASSWORD}\"}" "${BASE_REGISTRY_URL}/users/login/" | jq -r .token)
|
||||
BASE_HEADER="Authorization: JWT ${TOKEN}"
|
||||
fi
|
||||
BASE_URL="${BASE_REGISTRY_URL}/v2/repositories/${BASE_REPO_USER}/${BASE_REPO_NAME}/tags/${BASE_REPO_TAG}"
|
||||
|
||||
elif [ "${BASE_REGISTRY_API}" = 'gitea' ];then
|
||||
if [ -n "${BASE_REGISTRY_PASSWORD}" ]; then
|
||||
BASE_HEADER="Authorization: token ${BASE_REGISTRY_PASSWORD}"
|
||||
fi
|
||||
BASE_URL="${BASE_REGISTRY_URL}/api/v1/packages/${BASE_REPO_USER}/container/${BASE_REPO_NAME}/${BASE_REPO_TAG}"
|
||||
fi
|
||||
|
||||
|
||||
# build the url to the repo
|
||||
BASE_URL="https://hub.docker.com/v2/repositories/${BASE_REPO}/tags/${BASE_TAG}"
|
||||
TARGET_URL="https://hub.docker.com/v2/repositories/${TARGET_REPO}/tags/${TARGET_TAG}"
|
||||
TARGET_HEADER=""
|
||||
if [ "${TARGET_REGISTRY_API}" = 'docker' ];then
|
||||
if [ -n "${TARGET_REGISTRY_USERNAME}" ] && [ -n "${TARGET_REGISTRY_PASSWORD}" ]; then
|
||||
TOKEN=$(curl -s -H "Content-Type: application/json" -X POST -d "{\"username\": \"${TARGET_REGISTRY_USERNAME}\", \"password\": \"${TARGET_REGISTRY_PASSWORD}\"}" "${TARGET_REGISTRY_URL}/users/login/" | jq -r .token)
|
||||
TARGET_HEADER="Authorization: JWT ${TOKEN}"
|
||||
fi
|
||||
TARGET_URL="${TARGET_REGISTRY_URL}/v2/repositories/${TARGET_REPO_USER}/${TARGET_REPO_NAME}/tags/${TARGET_REPO_TAG}"
|
||||
|
||||
# login if credentias are passed
|
||||
HEADER=""
|
||||
if [ -n "${DOCKER_USERNAME}" ] && [ -n "${DOCKER_PASSWORD}" ]; then
|
||||
TOKEN=$(curl -s -H "Content-Type: application/json" -X POST -d '{"username": "'"${DOCKER_USERNAME}"'", "password": "'"${DOCKER_PASSWORD}"'"}' https://hub.docker.com/v2/users/login/ | jq -r .token)
|
||||
HEADER="Authorization: JWT ${TOKEN}"
|
||||
elif [ "${TARGET_REGISTRY_API}" = 'gitea' ];then
|
||||
if [ -n "${TARGET_REGISTRY_PASSWORD}" ]; then
|
||||
TARGET_HEADER="Authorization: token ${TARGET_REGISTRY_PASSWORD}"
|
||||
fi
|
||||
TARGET_URL="${TARGET_REGISTRY_URL}/api/v1/packages/${TARGET_REPO_USER}/container/${TARGET_REPO_NAME}/${TARGET_REPO_TAG}"
|
||||
fi
|
||||
|
||||
|
||||
# compare the update time of the BASE and TARGET images
|
||||
BASE_DATE=$(curl -s -H "${HEADER}" "${BASE_URL}" | jq -r .last_updated | sed 's/T/ /' | sed 's/\..*//' )
|
||||
TARGET_DATE=$(curl -s -H "${HEADER}" "${TARGET_URL}" | jq -r .last_updated | sed 's/T/ /' | sed 's/\..*//' )
|
||||
if [ "${BASE_REGISTRY_API}" = 'docker' ];then
|
||||
BASE_DATE=$(curl -s -H "${BASE_HEADER}" "${BASE_URL}" | jq -r .last_updated)
|
||||
elif [ "${BASE_REGISTRY_API}" = 'gitea' ];then
|
||||
BASE_DATE=$(curl -s -H "${BASE_HEADER}" "${BASE_URL}" | jq -r .created_at)
|
||||
fi
|
||||
|
||||
BASE_TIMESTAMP=$(date -d "${BASE_DATE}" +%s)
|
||||
TARGET_TIMESTAMP=$(date -d "${TARGET_DATE}" +%s)
|
||||
if [ "${TARGET_REGISTRY_API}" = 'docker' ];then
|
||||
TARGET_DATE=$(curl -s -H "${TARGET_HEADER}" "${TARGET_URL}" | jq -r .last_updated)
|
||||
elif [ "${TARGET_REGISTRY_API}" = 'gitea' ];then
|
||||
TARGET_DATE=$(curl -s -H "${TARGET_HEADER}" "${TARGET_URL}" | jq -r .created_at)
|
||||
fi
|
||||
|
||||
BASE_TIMESTAMP=$(dateconv --format="%s" "${BASE_DATE}")
|
||||
TARGET_TIMESTAMP=$(dateconv --format="%s" "${TARGET_DATE}")
|
||||
|
||||
CLEAN_EXIT=true
|
||||
|
||||
@@ -68,4 +154,4 @@ if [ "$BASE_TIMESTAMP" -le "$TARGET_TIMESTAMP" ]; then
|
||||
fi
|
||||
|
||||
echo "$TARGET needs updating"
|
||||
exit 0
|
||||
exit 0
|
||||
|
||||
Reference in New Issue
Block a user