Skip to content

Commit

Permalink
refactor: extract scripts from pull, push and update-descriptions (#120)
Browse files Browse the repository at this point in the history
* refactor: extract scripts from pull, push and update-descriptions

* ci: remove RC009
  • Loading branch information
EricRibeiro authored May 3, 2022
1 parent 2e1e4e4 commit 031f7bf
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 22 deletions.
1 change: 0 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ workflows:
tags:
only: /.*/
- orb-tools/review:
exclude: RC009
filters:
tags:
only: /.*/
Expand Down
9 changes: 4 additions & 5 deletions src/commands/pull.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ steps:
steps:
- run:
name: Docker pull
command: |
echo "<<parameters.images>>" | sed -n 1'p' | tr ',' '\n' | while read image; do
echo "Pulling ${image}";
docker pull ${image} <<#parameters.ignore-docker-pull-error>>|| true<</parameters.ignore-docker-pull-error>>
done
environment:
PARAM_IMAGES: <<parameters.images>>
PARAM_IGNORE_DOCKER_PULL_ERROR: <<parameters.ignore-docker-pull-error>>
command: << include(scripts/pull.sh) >>
5 changes: 5 additions & 0 deletions src/commands/push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ parameters:
steps:
- deploy:
name: <<parameters.step-name>>
environment:
PARAM_REGISTRY: <<parameters.registry>>
PARAM_IMAGE: <<parameters.image>>
PARAM_TAG: <<parameters.tag>>
PARAM_DIGEST_PATH: <<parameters.digest-path>>
command: |
IFS="," read -ra DOCKER_TAGS \<<< "<< parameters.tag >>"
for tag in "${DOCKER_TAGS[@]}"; do
Expand Down
24 changes: 8 additions & 16 deletions src/commands/update-description.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,11 @@ parameters:
steps:
- deploy:
name: Update description
command: |
if [ "<<parameters.registry>>" != "docker.io" ]; then
echo "Registry is not set to Docker Hub. Exiting"
exit 1
fi
DESCRIPTION="<<parameters.path>>/<<parameters.readme>>"
PAYLOAD="username=$<<parameters.docker-username>>&password=$<<parameters.docker-password>>"
JWT=$(curl -s -d $PAYLOAD https://hub.docker.com/v2/users/login/ | jq -r .token)
HEADER="Authorization: JWT $JWT"
URL="https://hub.docker.com/v2/repositories/<<parameters.image>>/"
STATUS=$(curl -s -o /dev/null -w %{http_code} -X PATCH -H "$HEADER" --data-urlencode full_description@$DESCRIPTION $URL)
if [ $STATUS -ne 200 ]; then
echo "Could not update image description"
echo "Error code: $STATUS"
exit 1
fi
environment:
PARAM_README: <<parameters.readme>>
PARAM_PATH: <<parameters.path>>
PARAM_REGISTRY: <<parameters.registry>>
PARAM_IMAGE: <<parameters.image>>
PARAM_DOCKER_USERNAME: <<parameters.docker-username>>
PARAM_DOCKER_PASSWORD: <<parameters.docker-password>>
command: << include(scripts/update-description.sh) >>
11 changes: 11 additions & 0 deletions src/scripts/pull.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env bash

echo "$PARAM_IMAGES" | sed -n 1'p' | tr ',' '\n' | while read -r image; do
echo "Pulling ${image}";

if [ "$PARAM_IGNORE_DOCKER_PULL_ERROR" -eq 1 ]; then
docker pull "${image}" || true;
else
docker pull "${image}";
fi
done
13 changes: 13 additions & 0 deletions src/scripts/push.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env bash

IFS="," read -ra DOCKER_TAGS <<< "$PARAM_TAG"

for tag in "${DOCKER_TAGS[@]}"; do
docker push "$PARAM_REGISTRY"/"$PARAM_IMAGE":${tag}
done

if [ -n "$PARAM_DIGEST_PATH" ]; then
mkdir -p "$(dirname "$PARAM_DIGEST_PATH")"
IFS="," read -ra DOCKER_TAGS <<< "$PARAM_TAG"
docker image inspect --format="{{index .RepoDigests 0}}" "$PARAM_REGISTRY"/"$PARAM_IMAGE":"${DOCKER_TAGS[0]}" > "$PARAM_DIGEST_PATH"
fi
22 changes: 22 additions & 0 deletions src/scripts/update-description.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env bash

if [ "$PARAM_REGISTRY" != "docker.io" ]; then
echo "Registry is not set to Docker Hub. Exiting"
exit 1
fi

USERNAME=${!PARAM_DOCKER_USERNAME}
PASSWORD=${!PARAM_DOCKER_PASSWORD}

DESCRIPTION="$PARAM_PATH/$PARAM_README"
PAYLOAD="username=$USERNAME&password=$PASSWORD"
JWT=$(curl -s -d "$PAYLOAD" https://hub.docker.com/v2/users/login/ | jq -r .token)
HEADER="Authorization: JWT $JWT"
URL="https://hub.docker.com/v2/repositories/$PARAM_IMAGE/"
STATUS=$(curl -s -o /dev/null -w '%{http_code}' -X PATCH -H "$HEADER" --data-urlencode full_description@$DESCRIPTION $URL)

if [ $STATUS -ne 200 ]; then
echo "Could not update image description"
echo "Error code: $STATUS"
exit 1
fi

0 comments on commit 031f7bf

Please sign in to comment.