-
-
Save oriolrius/963795c149e11c084db763a578abc258 to your computer and use it in GitHub Desktop.
readm2notion pre-push hook
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # Get the path of the current repository | |
| REPOS_PATH=$(git rev-parse --show-toplevel) | |
| if [ $? -ne 0 ]; then | |
| echo "Error: Not a Git repository" | |
| exit 1 | |
| fi | |
| # Getting the CHCR_PAT (Github Container Registry Personal Access Token) from the UCI | |
| echo "$(date): Getting the GHCR_PAT from the UCI" | tee -a $LOG_FILE | |
| if [ -z "$GHCR_PAT" ]; then | |
| GHCR_PAT=$(uci get credentials.ghcr.ghcr_pat 2>/dev/null) | |
| if [ -z "$GHCR_PAT" ]; then | |
| echo "$(date): GHCR_PAT is not set" | tee -a $LOG_FILE | |
| exit 1 | |
| fi | |
| fi | |
| # Extract the repository name from the repository path | |
| REPO_NAME=$(basename $REPOS_PATH) | |
| echo "Repository name is $REPO_NAME" | |
| if [ ${REPO_NAME} = "readme2notion" ]; then | |
| echo "Updating GIST..." | |
| # https://gist.githubusercontent.com/oriolrius/963795c149e11c084db763a578abc258/raw/iot-gw_stacks_pre-push | |
| # Read the content of the file | |
| GIST_ID="963795c149e11c084db763a578abc258" | |
| FILE_CONTENT=$(cat "$0") | |
| GIST_DESCRIPTION="readm2notion pre-push hook" | |
| FILE_NAME="iot-gw_stacks_pre-push" | |
| # Create the JSON payload | |
| echo "Creating JSON payload from current pre-push file..." | |
| json_payload=$(jq -n --arg description "$GIST_DESCRIPTION" --arg file_name "$FILE_NAME" --arg content "$FILE_CONTENT" '{ | |
| description: $description, | |
| files: { | |
| ($file_name): { | |
| content: $content | |
| } | |
| } | |
| }') | |
| # Update the gist | |
| echo "Updating the gist..." | |
| curl -qs \ | |
| -X PATCH \ | |
| -H "Accept: application/vnd.github+json" \ | |
| -H "Authorization: Bearer ${GHCR_PAT}" \ | |
| -H "X-GitHub-Api-Version: 2022-11-28" \ | |
| -d "$json_payload" \ | |
| -o /tmp/curl.log \ | |
| https://api.github.com/gists/${GIST_ID} | |
| echo "Gist updated successfully" | |
| exit 0 | |
| fi | |
| # Construct the log file path | |
| LOG_FILE="/tmp/${HOSTNAME}_${REPO_NAME}.log" | |
| echo "Git hook logs are available at: $LOG_FILE" | |
| # Log start of the hook execution | |
| echo "$(date): Starting hook execution" | tee -a $LOG_FILE | |
| # Get the latest commit hash to be pushed | |
| LATEST_COMMIT=$(git rev-parse HEAD) | |
| # Log the latest commit hash | |
| echo "$(date): Latest commit hash to be pushed is $LATEST_COMMIT" | tee -a $LOG_FILE | |
| # Capture parameters and environment variables to the log file | |
| # echo "---" >> $LOG_FILE | |
| # echo "Parameters: $@" >> $LOG_FILE | |
| # echo "---" >> $LOG_FILE | |
| # echo "Environment:" >> $LOG_FILE | |
| # env >> $LOG_FILE | |
| # echo "---" >> $LOG_FILE | |
| # echo "Set:" >> $LOG_FILE | |
| # set >> $LOG_FILE | |
| # echo "---" >> $LOG_FILE | |
| # Github Container Registry login | |
| echo "$(date): Logging into GitHub Container Registry" | tee -a $LOG_FILE | |
| echo $GHCR_PAT | docker login ghcr.io -u oriolrius --password-stdin 2>&1 | tee -a $LOG_FILE | |
| # Pull the latest Docker image | |
| echo "$(date): Pulling Docker image" | tee -a $LOG_FILE | |
| docker pull ghcr.io/oriolrius/readme2notion 2>&1 | tee -a $LOG_FILE | |
| # Check if the Docker pull was successful | |
| if [ $? -ne 0 ]; then | |
| echo "$(date): Docker pull failed" | tee -a $LOG_FILE | |
| exit 1 | |
| fi | |
| echo "---" | tee -a $LOG_FILE | |
| # Getting the Notion token from the UCI | |
| echo "$(date): Getting the Notion token from the UCI" | tee -a $LOG_FILE | |
| if [ -z "$NOTION_API_TOKEN" ]; then | |
| NOTION_API_TOKEN=$(uci get credentials.notion.api_key 2>/dev/null) | |
| if [ -z "$NOTION_API_TOKEN" ]; then | |
| echo "$(date): Notion API token is not set" | tee -a $LOG_FILE | |
| exit 1 | |
| fi | |
| fi | |
| echo "---" | tee -a $LOG_FILE | |
| # Run the Docker container with the specified command | |
| echo "$(date): Running Docker container" | tee -a $LOG_FILE | |
| docker run --rm --network host -v ${REPOS_PATH}:/repos ghcr.io/oriolrius/readme2notion \ | |
| --notionToken ${NOTION_API_TOKEN} \ | |
| --commit ${LATEST_COMMIT} \ | |
| /repos/README.md 2>&1 | tee -a $LOG_FILE | |
| # Check if the Docker run was successful | |
| if [ $? -ne 0 ]; then | |
| echo "$(date): Docker run failed" | tee -a $LOG_FILE | |
| exit 1 | |
| fi | |
| echo "---" | tee -a $LOG_FILE | |
| # Log end of the hook execution | |
| echo "$(date): Hook execution finished" | tee -a $LOG_FILE |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment