Skip to content

Commit

Permalink
Log into Dockerhub when credentials are provided
Browse files Browse the repository at this point in the history
What?
- Add 2 new optional inputs to the Github action:
  - `dockerhub-username`: the "docker ID" (aka username) for the
    Dockerhub account the action should log in as
  - `dockerhub-password`: the account password or a personal
    access token used to authorize the account login.
- Update the `./start-mongodb.sh` script to log into Dockerhub
  when these two inputs are provided.

Why?
- To allow users to authenticate with Dockerhub, and increase their
  rate limits.
  Otherwise, unauthorized requests can be rate limited, see:
  - https://www.docker.com/increase-rate-limits/
  - supercharge#62
  • Loading branch information
oliver-hohn committed Aug 6, 2024
1 parent 5a87bd8 commit ebe17b8
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 7 deletions.
4 changes: 4 additions & 0 deletions action-types.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# See https://github.com/krzema12/github-actions-typing
inputs:
dockerhub-username:
type: string
dockerhub-password:
type: string
mongodb-version:
type: string
mongodb-replica-set:
Expand Down
12 changes: 12 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ branding:
color: 'green'

inputs:
dockerhub-username:
description: 'Docker username to use to log into Dockerhub'
required: false
default: ''

dockerhub-password:
description: 'Docker password or personal access token to use to log into Dockerhub'
required: false
default: ''

mongodb-version:
description: 'MongoDB version to use (default "latest")'
required: false
Expand Down Expand Up @@ -45,6 +55,8 @@ runs:
using: 'docker'
image: 'Dockerfile'
args:
- ${{ inputs.dockerhub-username }}
- ${{ inputs.dockerhub-password }}
- ${{ inputs.mongodb-version }}
- ${{ inputs.mongodb-replica-set }}
- ${{ inputs.mongodb-port }}
Expand Down
34 changes: 27 additions & 7 deletions start-mongodb.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
#!/bin/sh

# Map input values from the GitHub Actions workflow to shell variables
MONGODB_VERSION=$1
MONGODB_REPLICA_SET=$2
MONGODB_PORT=$3
MONGODB_DB=$4
MONGODB_USERNAME=$5
MONGODB_PASSWORD=$6
MONGODB_CONTAINER_NAME=$7
DOCKERHUB_USERNAME=$1
DOCKERHUB_PASSWORD=$2
MONGODB_VERSION=$3
MONGODB_REPLICA_SET=$4
MONGODB_PORT=$5
MONGODB_DB=$6
MONGODB_USERNAME=$7
MONGODB_PASSWORD=$8
MONGODB_CONTAINER_NAME=$9

# `mongosh` is used starting from MongoDB 5.x
MONGODB_CLIENT="mongosh --quiet"
Expand Down Expand Up @@ -73,6 +75,24 @@ wait_for_mongodb () {
# docker rm -f $MONGODB_CONTAINER_NAME
# fi

login_to_dockerhub () {
echo "::group::Logging in to Docker Hub"
echo "Logging in as [$DOCKERHUB_USERNAME]"
echo ""

echo $DOCKERHUB_PASSWORD | docker login --username $DOCKERHUB_USERNAME --password-stdin

if [ $? -ne 0 ]; then
echo "Error logging in to Docker Hub"
exit 2
fi
echo "::endgroup::"
}

if [[ -n "$DOCKERHUB_USERNAME" && -n "$DOCKERHUB_PASSWORD" ]]; then
login_to_dockerhub
fi


if [ -z "$MONGODB_REPLICA_SET" ]; then
echo "::group::Starting single-node instance, no replica set"
Expand Down

0 comments on commit ebe17b8

Please sign in to comment.