From ee147803ef33ac1be20ebab4aaaeba7822c730dc Mon Sep 17 00:00:00 2001 From: Diego Forziati Date: Thu, 11 Apr 2024 13:02:23 -0300 Subject: [PATCH] adding template directory --- .github/templates/docker-publish/action.yml | 53 +++++++++++++++++ .github/templates/watch-exec/action.yml | 65 +++++++++++++++++++++ 2 files changed, 118 insertions(+) create mode 100644 .github/templates/docker-publish/action.yml create mode 100644 .github/templates/watch-exec/action.yml diff --git a/.github/templates/docker-publish/action.yml b/.github/templates/docker-publish/action.yml new file mode 100644 index 0000000..f0147bb --- /dev/null +++ b/.github/templates/docker-publish/action.yml @@ -0,0 +1,53 @@ +name: docker-publish +description: Builds a docker image using nix and publish it to Docker hub. + +inputs: + image_path: + description: The path of the built image that should be loaded + required: true + username: + description: "Docker hub username" + required: true + password: + description: "Docker hub password" + required: true + name: + description: "The name of the container to be published" + required: true + artifact: + description: "The image and tag produced by the nix expression" + required: true + tag: + required: false + default: "" + +runs: + using: "composite" + steps: + - uses: docker/login-action@v1 + with: + username: ${{ inputs.username }} + password: ${{ inputs.password }} + - name: Docker load, tag, and push + run: | + # See https://github.com/actions/checkout/issues/760 + git config --global --add safe.directory /__w/composable/composable + + docker load --input ${{ inputs.image_path }} + + SHA256=$(sha256sum ${{ inputs.image_path }} | cut --delimiter " " --fields 1) + COMMIT_SHA=$(git rev-parse HEAD) + CONTAINER_NAME=${{ inputs.name }} + TEMP_CONTAINER_NAME=${{ inputs.artifact }} + + docker tag "${TEMP_CONTAINER_NAME}" "composablefi/${CONTAINER_NAME}:${SHA256}" + docker tag "${TEMP_CONTAINER_NAME}" "composablefi/${CONTAINER_NAME}:${COMMIT_SHA}" + if [[ -n "${{ inputs.tag }}" ]]; then + docker tag "${TEMP_CONTAINER_NAME}" "composablefi/${CONTAINER_NAME}:${{ inputs.tag }}" + fi + + if [[ $(git symbolic-ref HEAD) = "refs/heads/main" ]]; then + docker tag "${TEMP_CONTAINER_NAME}" "composablefi/${CONTAINER_NAME}:latest" + fi; + docker push --all-tags "composablefi/${CONTAINER_NAME}" + shell: bash diff --git a/.github/templates/watch-exec/action.yml b/.github/templates/watch-exec/action.yml new file mode 100644 index 0000000..d081573 --- /dev/null +++ b/.github/templates/watch-exec/action.yml @@ -0,0 +1,65 @@ +name: watch-exec +description: Calls commands within a cachix watch-exec wrapper. + +inputs: + command: + description: "The shell command to execute within a watch-exec context" + required: true + working-directory: + description: "Directory to execute command" + required: false + +runs: + using: "composite" + steps: + - run: | + # Marker status code in case of network failure. + STATUS_TRANSIENT_FAILURE=200 + # Maximum retries in case of network failures. + RETRIES=5 + INPUTS_COMMAND="${{ inputs.command }}" + CACHIX="cachix watch-exec --compression-level ${CACHIX_COMPRESSION_LEVEL:-16} --compression-method ${CACHIX_COMPRESSION_METHOD:-"zstd"} --jobs ${CACHIX_JOBS:-16} composable" + NIX_DEBUG_COMMAND="" && [[ $ACTIONS_RUNNER_DEBUG = "true" ]] && NIX_DEBUG_COMMAND='--print-build-logs --debug --show-trace --verbose --keep-going' + HAPPY_CMD="${CACHIX?} ${INPUTS_COMMAND?} --no-update-lock-file --accept-flake-config ${NIX_DEBUG_COMMAND} -L 2> >(tee --append $LOG_FILE >&2)" + SLOW_CMD="${CACHIX?} ${INPUTS_COMMAND?} --no-update-lock-file --accept-flake-config --show-trace --fallback -L 2 ${NIX_DEBUG_COMMAND} > >(tee --append $LOG_FILE >&2)" + + printf "will try execute '${HAPPY_CMD?}'" + + # Marker that a network error occurred. + BAD="HTTP error 200 ('')" + LOG_FILE=/tmp/out.log + + function run() { + eval "${HAPPY_CMD?}" + STATUS_CODE=$? + if [[ $STATUS_CODE -ne 0 ]] ; then + C=$(grep -c "HTTP error 200" $LOG_FILE) + if [[ $C -gt 0 ]]; then + STATUS_CODE=$STATUS_TRANSIENT_FAILURE + fi + fi + } + + try=0 + while [[ "${try?}" -le "${RETRIES?}" ]]; do + if [[ "${try?}" -eq "${RETRIES?}" ]] ; then + eval "${SLOW_CMD?}" + STATUS_CODE=$? + break + fi + + run + if [[ $STATUS_CODE -eq 0 ]] ; then + break + fi + + if [[ $STATUS_CODE -ne 0 ]] && [[ $STATUS_CODE -ne $STATUS_TRANSIENT_FAILURE ]] ; then + break + fi + + ((try=try+1)) + done + + exit $((STATUS_CODE)) + shell: "bash" + working-directory: ${{ inputs.working-directory }}