refactor: update image to execute action with node

This commit is contained in:
Peter Evans 2020-09-23 17:23:10 +09:00
parent 65f339015f
commit 33cc80a90a
2 changed files with 18 additions and 60 deletions

View file

@ -1,16 +1,18 @@
FROM peterevans/curl-jq:1.0.1 FROM node:12-alpine
LABEL maintainer="Peter Evans <mail@peterevans.dev>" LABEL \
LABEL repository="https://github.com/peter-evans/dockerhub-description" maintainer="Peter Evans <mail@peterevans.dev>" \
LABEL homepage="https://github.com/peter-evans/dockerhub-description" org.opencontainers.image.title="dockerhub-description" \
org.opencontainers.image.description="An action to update a Docker Hub repository description from README.md" \
LABEL com.github.actions.name="Docker Hub Description" org.opencontainers.image.authors="Peter Evans <mail@peterevans.dev>" \
LABEL com.github.actions.description="An action to update a Docker Hub repository description from README.md" org.opencontainers.image.url="https://github.com/peter-evans/dockerhub-description" \
LABEL com.github.actions.icon="upload" org.opencontainers.image.vendor="https://peterevans.dev" \
LABEL com.github.actions.color="blue" org.opencontainers.image.licenses="MIT"
COPY LICENSE README.md / COPY LICENSE README.md /
COPY dist/index.js /index.js
COPY entrypoint.sh /entrypoint.sh COPY entrypoint.sh /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"] ENTRYPOINT ["/entrypoint.sh"]

View file

@ -2,56 +2,12 @@
set -eo pipefail set -eo pipefail
IFS=$'\n\t' IFS=$'\n\t'
# Allow DOCKERHUB_* variables to be set from their DOCKER_* variant # Execute the action code and output to file
DOCKERHUB_USERNAME=${DOCKERHUB_USERNAME:-${DOCKER_USERNAME}} node index.js > action.log 2>&1
DOCKERHUB_PASSWORD=${DOCKERHUB_PASSWORD:-${DOCKER_PASSWORD}}
DOCKERHUB_REPOSITORY=${DOCKERHUB_REPOSITORY:-${DOCKER_REPOSITORY}}
# If the repository isn't explicitly defined, infer it from GitHub if possible # Remove lines containing sensitive information from the log
DOCKERHUB_REPOSITORY=${DOCKERHUB_REPOSITORY:-${GITHUB_REPOSITORY}} sed -i '/::debug::/d' ./action.log
sed -i '/::add-mask::/d' ./action.log
# Validate we can authenticate # Output the log
if [ -z "$DOCKERHUB_USERNAME" ] || [ -z "$DOCKERHUB_PASSWORD" ]; then cat action.log
echo 'Unable to authenticate with Docker Hub, set a valid $DOCKERHUB_USERNAME and $DOCKERHUB_PASSWORD'
exit 1
fi
# Validate we have the repository name
if [ -z "$DOCKERHUB_REPOSITORY" ]; then
echo 'Unable to determine Docker Hub repository name, set with $DOCKERHUB_REPOSITORY'
exit 1
fi
# Set the default path to README.md
README_FILEPATH=${README_FILEPATH:="./README.md"}
# Check the file exists
if [ ! -f ${README_FILEPATH} ]; then
echo "Readme file not found"
exit 1
fi
# Check the file size
if [ $(wc -c <${README_FILEPATH}) -gt 25000 ]; then
echo "File size exceeds the maximum allowed 25000 bytes"
exit 1
fi
# Acquire a token for the Docker Hub API
echo "Acquiring token"
LOGIN_PAYLOAD="{\"username\": \"${DOCKERHUB_USERNAME}\", \"password\": \"${DOCKERHUB_PASSWORD}\"}"
TOKEN=$(curl -s -H "Content-Type: application/json" -X POST -d ${LOGIN_PAYLOAD} https://hub.docker.com/v2/users/login/ | jq -r .token)
# Send a PATCH request to update the description of the repository
echo "Sending PATCH request"
REPO_URL="https://hub.docker.com/v2/repositories/${DOCKERHUB_REPOSITORY}/"
RESPONSE_CODE=$(curl -s --write-out %{response_code} --output /dev/null -H "Authorization: JWT ${TOKEN}" -X PATCH --data-urlencode full_description@${README_FILEPATH} ${REPO_URL})
echo "Received response code: $RESPONSE_CODE"
if [ $RESPONSE_CODE -eq 200 ]; then
echo "Request successful"
exit 0
else
echo "Request failed"
exit 1
fi