From f2e636fe2113df7334a4f0cbc6356d285aded7fd Mon Sep 17 00:00:00 2001 From: Vignesh Rao Date: Wed, 11 Sep 2024 21:12:12 -0500 Subject: [PATCH] Auto update docker hub description via GH workflow Update README.md --- .github/workflows/description.yml | 91 +++++++++++++++++++++++++++++++ README.md | 9 ++- 2 files changed, 97 insertions(+), 3 deletions(-) create mode 100644 .github/workflows/description.yml diff --git a/.github/workflows/description.yml b/.github/workflows/description.yml new file mode 100644 index 0000000..7a1170d --- /dev/null +++ b/.github/workflows/description.yml @@ -0,0 +1,91 @@ +name: Update Docker Hub Description + +on: + push: + branches: + - main + paths: + - README.md + - .github/workflows/description.yml + +env: + DOCKER_REGISTRY: "https://hub.docker.com/v2" + DOCKER_REPOSITORY: "${{ github.event.repository.name }}" + DESCRIPTION_LIMIT: 100 + +jobs: + update-docker-hub: + runs-on: thevickypedia-lite + steps: + - uses: actions/checkout@v4 + + - name: Fetch API Token + run: | + payload=$(jq -n \ + --arg username "${{ secrets.DOCKER_USERNAME }}" \ + --arg password "${{ secrets.DOCKER_PASSWORD }}" \ + '{username: $username, password: $password}') + + token=$(curl -s -X POST "${{ env.DOCKER_REGISTRY }}/users/login/" \ + -H "Content-Type: application/json" \ + -d "$payload" | jq -r '.token') + + if [[ -n "${token}" ]]; then + echo "::debug title=Token Retriever::Retrieved token successfully" + echo "API_TOKEN=${token}" >> $GITHUB_ENV + else + echo "::error title=Token Retriever::Failed to get auth token" + exit 1 + fi + shell: bash + + - name: Get Description + run: | + warn="Description exceeds DockerHub's limit and has been truncated to ${{ env.DESCRIPTION_LIMIT }} characters." + description="${{ github.event.repository.description }}" + description_length=${#description} + if [[ "$description_length" -gt "${{ env.DESCRIPTION_LIMIT }}" ]]; then + echo "::warning title=Description Too Long::${warn}" + shortened_description="${description:0:97}..." + else + shortened_description="$description" + fi + echo "SHORT_DESCRIPTION=${shortened_description}" >> $GITHUB_ENV + shell: bash + + - name: Update description + run: | + full_description="$(cat README.md)" + payload=$(jq -n \ + --arg description "${{ env.SHORT_DESCRIPTION }}" \ + --arg full_description "$full_description" \ + '{description: $description, full_description: $full_description}') + + response=$(curl -s -o /tmp/desc -w "%{http_code}" -X PATCH \ + "${{ env.DOCKER_REGISTRY }}/repositories/${{ secrets.DOCKER_USERNAME }}/${{ env.DOCKER_REPOSITORY }}/" \ + -H "Authorization: Bearer ${{ env.API_TOKEN }}" \ + -H "Content-Type: application/json" \ + -d "$payload") + + status_code="${response: -3}" + if [[ "${status_code}" -eq 200 ]]; then + echo "::notice title=Updater::Updated description successfully" + exit 0 + elif [[ -f "/tmp/desc" ]]; then + echo "::error title=Updater::Failed to update description" + response_payload="$(cat /tmp/desc)" + reason=$(echo "${response_payload}" | jq '.message') + info=$(echo "${response_payload}" | jq '.errinfo') + if [[ "$reason" != "null" ]]; then + echo "::error title=Updater::[${status_code}]: $reason" + else + echo "::error title=Updater::[${status_code}]: $(cat /tmp/desc)" + fi + if [[ "$info" != "null" ]]; then + echo "::error title=Updater::${info}" + fi + else + echo "::error title=Updater::Failed to update description - ${status_code}" + fi + exit 1 + shell: bash diff --git a/README.md b/README.md index 24fc79e..f86f901 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,7 @@ # GitHub Runner Linux [![Test Runner][badges-test]][links-test] +
[![Build and Publish][badges-build]][links-build] Spin up an on-demand self-hosted GitHub action runner with multi-arch supported Ubuntu image. @@ -31,6 +32,7 @@ docker compose up -d - **REUSE_EXISTING** - Re-use existing configuration. Defaults to `false` > [!NOTE] +> > `REUSE_EXISTING` flag can be useful when a container restarts due to an issue or > when a container is reused after being terminated without shutting down gracefully. >
@@ -55,9 +57,10 @@ docker compose up -d > ``` >
-> [!WARNING] -Using this image **without** the env var `GIT_REPOSITORY` will create an organization level runner. -Using self-hosted runners in public repositories pose some considerable security threats. +> [!WARNING] +> +> Using this image **without** the env var `GIT_REPOSITORY` will create an organization level runner.
+> Using self-hosted runners in public repositories pose some considerable security threats. > - [#self-hosted-runner-security] > - [#restricting-the-use-of-self-hosted-runners] > - [#configuring-required-approval-for-workflows-from-public-forks]