From 33cc80a90a22243bf5e8d690c6e7cd9a78b0500e Mon Sep 17 00:00:00 2001 From: Peter Evans Date: Wed, 23 Sep 2020 17:23:10 +0900 Subject: [PATCH] refactor: update image to execute action with node --- Dockerfile | 20 ++++++++++-------- entrypoint.sh | 58 +++++++-------------------------------------------- 2 files changed, 18 insertions(+), 60 deletions(-) diff --git a/Dockerfile b/Dockerfile index b153d1d..8ce17ff 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,16 +1,18 @@ -FROM peterevans/curl-jq:1.0.1 +FROM node:12-alpine -LABEL maintainer="Peter Evans " -LABEL repository="https://github.com/peter-evans/dockerhub-description" -LABEL homepage="https://github.com/peter-evans/dockerhub-description" - -LABEL com.github.actions.name="Docker Hub Description" -LABEL com.github.actions.description="An action to update a Docker Hub repository description from README.md" -LABEL com.github.actions.icon="upload" -LABEL com.github.actions.color="blue" +LABEL \ + maintainer="Peter Evans " \ + org.opencontainers.image.title="dockerhub-description" \ + org.opencontainers.image.description="An action to update a Docker Hub repository description from README.md" \ + org.opencontainers.image.authors="Peter Evans " \ + org.opencontainers.image.url="https://github.com/peter-evans/dockerhub-description" \ + org.opencontainers.image.vendor="https://peterevans.dev" \ + org.opencontainers.image.licenses="MIT" COPY LICENSE README.md / +COPY dist/index.js /index.js + COPY entrypoint.sh /entrypoint.sh ENTRYPOINT ["/entrypoint.sh"] diff --git a/entrypoint.sh b/entrypoint.sh index 8803d1a..be4838e 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -2,56 +2,12 @@ set -eo pipefail IFS=$'\n\t' -# Allow DOCKERHUB_* variables to be set from their DOCKER_* variant -DOCKERHUB_USERNAME=${DOCKERHUB_USERNAME:-${DOCKER_USERNAME}} -DOCKERHUB_PASSWORD=${DOCKERHUB_PASSWORD:-${DOCKER_PASSWORD}} -DOCKERHUB_REPOSITORY=${DOCKERHUB_REPOSITORY:-${DOCKER_REPOSITORY}} +# Execute the action code and output to file +node index.js > action.log 2>&1 -# If the repository isn't explicitly defined, infer it from GitHub if possible -DOCKERHUB_REPOSITORY=${DOCKERHUB_REPOSITORY:-${GITHUB_REPOSITORY}} +# Remove lines containing sensitive information from the log +sed -i '/::debug::/d' ./action.log +sed -i '/::add-mask::/d' ./action.log -# Validate we can authenticate -if [ -z "$DOCKERHUB_USERNAME" ] || [ -z "$DOCKERHUB_PASSWORD" ]; then - 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 +# Output the log +cat action.log