diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..1a8dc45 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,43 @@ +name: Test action + +on: push + +jobs: + upload: + runs-on: self-hosted + steps: + - name: Checkout repo + uses: actions/checkout@v4 + + - name: Generate test files + run: | + mkdir -p files + echo "Minio Test 1" > files/test1.txt + echo "Minio Test 2" > files/test2.txt + + - name: Test single file upload + uses: ./ + with: + url: https://s3.nubificus.com + access-key: ${{ secrets.AWS_ACCESS_KEY }} + secret-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + local-path: "./files/test1.txt" + remote-path: "nbfc-assets/github/minio/" + + - name: Test wildcard with extension upload + uses: ./ + with: + url: https://s3.nubificus.com + access-key: ${{ secrets.AWS_ACCESS_KEY }} + secret-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + local-path: "./files/*.txt" + remote-path: "nbfc-assets/github/minio/" + + - name: Test wildcard upload + uses: ./ + with: + url: https://s3.nubificus.com + access-key: ${{ secrets.AWS_ACCESS_KEY }} + secret-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + local-path: "./files/*" + remote-path: "nbfc-assets/github/minio/" diff --git a/Dockerfile b/Dockerfile deleted file mode 100644 index 3b91a1e..0000000 --- a/Dockerfile +++ /dev/null @@ -1,5 +0,0 @@ -FROM minio/mc - -COPY entrypoint.sh /entrypoint.sh - -ENTRYPOINT ["/entrypoint.sh"] diff --git a/action.yml b/action.yml index 096ca91..aa98739 100644 --- a/action.yml +++ b/action.yml @@ -24,12 +24,82 @@ inputs: required: false runs: - using: 'docker' - image: 'Dockerfile' - args: - - ${{ inputs.url }} - - ${{ inputs.access-key }} - - ${{ inputs.secret-key }} - - ${{ inputs.local-path }} - - ${{ inputs.remote-path }} - - ${{ inputs.policy }} + using: composite + steps: + - name: Check if mc is installed + id: check-mc + run: | + if [[ -n "$(which mc)" ]]; then + echo "exists=true" >> "$GITHUB_OUTPUT" + else + echo "exists=false" >> "$GITHUB_OUTPUT" + fi + shell: bash + + - name: Download latest mc SHA256 + id: mc-sha + if: ${{ steps.check-mc.outputs.exists == 'false' }} + run: | + arch=$(dpkg --print-architecture | sed 's/armhf/arm/g') + sha=$(wget -qO- \ + https://dl.min.io/client/mc/release/linux-"${arch}"/mc.sha256sum \ + | awk '{print $1}') + echo "sha256=$sha" >> "$GITHUB_OUTPUT" + shell: bash + + - name: Cache mc binary + id: cache-mc + if: ${{ steps.check-mc.outputs.exists == 'false' }} + uses: actions/cache@v4 + with: + path: /usr/local/bin/mc + key: mc-${{ steps.mc-sha.outputs.sha256 }} + + - name: Setup mc + working-directory: /usr/local/bin + if: >- + ${{ steps.check-mc.outputs.exists == 'false' + && steps.cache-mc.outputs.cache-hit != 'true' }} + run: | + arch=$(dpkg --print-architecture | sed 's/armhf/arm/g') + sudo wget --progres=dot:binary \ + "https://dl.min.io/client/mc/release/linux-$%7Barch%7D/mc" + sudo chmod +x mc + shell: bash + + - name: Setup s3 alias + run: | + mc alias set s3 "${{ inputs.url }}" \ + "${{ inputs.access-key }}" "${{ inputs.secret-key }}" + shell: bash + + - name: Upload objects + run: | + echo "Will upload ${{ inputs.local-path }} to ${{ inputs.remote-path }}" + local_path=${{ inputs.local-path }} + if [ "${local_path#*'*'}" != "$local_path" ]; then + # Handle local_files with wildcards + local_dir=$(dirname "$local_path") + local_files=$(basename "$local_path") + IFS=$'\n' + for p in $(find "$local_dir" -maxdepth 1 -name "$local_files" | \ + sort -u); do + [ "$p" = "$local_dir" ] && continue + mc cp -r "$p" "s3/${{ inputs.remote-path }}" + done + unset IFS + else + mc cp -r "$local_path" "s3/${{ inputs.remote-path }}" + fi + shell: bash + + - name: Set policy + run: | + if [ "${{ inputs.policy }}" = 1 ] ; then + echo "Will make ${{ inputs.remote-path }} public" + mc anonymous -r set download "s3/${{ inputs.remote-path }}" + else + echo "Will make ${{ inputs.remote-path }} private" + mc anonymous -r set private "s3/${{ inputs.remote-path }}" || true + fi + shell: bash diff --git a/entrypoint.sh b/entrypoint.sh deleted file mode 100755 index 1406bbd..0000000 --- a/entrypoint.sh +++ /dev/null @@ -1,54 +0,0 @@ -#!/bin/bash - -LOG_NAME="minio" - -info() { - [ -t 1 ] && [ -n "$TERM" ] \ - && echo "$(tput setaf 2)[$LOG_NAME]$(tput sgr0) $*" \ - || echo "[$LOG_NAME] $*" -} - -err() { - [ -t 2 ] && [ -n "$TERM" ] \ - && echo -e "$(tput setaf 1)[$LOG_NAME]$(tput sgr0) $*" 1>&2 \ - || echo -e "[$LOG_NAME] $*" 1>&2 -} - -die() { - err "$@" - exit 1 -} - -ok_or_die() { - if [ $? -ne 0 ]; then - die $1 - fi -} - -if [[ $# -lt 5 ]] ; then - die "Usage: $0 url access_key secret_key local_path remote_path" -fi - -url=$1 -access_key=$2 -secret_key=$3 -local_path=$4 -remote_path=$5 - -info "Will upload $local_path to $remote_path" - -mc alias set s3 $url $access_key $secret_key -ok_or_die "Could not set mc alias" - -mc cp -r $local_path s3/$remote_path -ok_or_die "Could not upload object" - -if [[ $# -eq 6 ]] ; then - if [[ $6 -eq 1 ]] ; then - info "Will make $remote_path public" - mc anonymous -r set download s3/$remote_path - else - info "Will make $remote_path private" - mc anonymous -r set private s3/$remote_path || true - fi -fi