Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Notify upon successful deployment #39

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions .github/workflows/notify.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#!/bin/bash

set -e

API_URL="https://api.github.com"

extract_issue_number() {
local commit_message="$1"
local issue_number
local pr_number

# Try to extract from "Merge pull request ... from branch/123"
issue_number=$(echo "$commit_message" | grep -oP 'from [a-zA-Z-_0-9]+/\K([0-9]+)')

if [ -z "$issue_number" ]; then
# Try to extract from "... (#123)"
pr_number=$(echo "$commit_message" | grep -oP '\(#\K([0-9]+)(?=\))')

if [ -n "$pr_number" ]; then
# Fetch PR description
pr_description=$(curl -s -H "Authorization: token $GH_TOKEN" "$API_URL/repos/$GITHUB_REPOSITORY/pulls/$pr_number" | jq -r .body)

if [ -n "$pr_description" ]; then
# Extract issue numbers from PR description
issue_numbers=($(echo "$pr_description" | grep -oP '#\K([0-9]+)(?=\s)'))

if [ ${#issue_numbers[@]} -gt 0 ]; then
issue_number=${issue_numbers[0]}
fi
fi
fi
fi

echo "$issue_number $pr_number"
}

print_debug_info() {
echo "Debug Information:"
echo "COMMIT_MESSAGE: $COMMIT_MESSAGE"
echo "ISSUE_NUMBER: $ISSUE_NUMBER"
echo "PR_NUMBER: $PR_NUMBER"
echo "GITHUB_REPOSITORY: $GITHUB_REPOSITORY"
echo "URL_QA: $URL_QA"
echo "API_URL: $API_URL"
echo "Environment Variables:"
env
}

COMMIT_MESSAGE=$(git log -1 --pretty=format:"%s")

# Extract the issue number and PR number
read ISSUE_NUMBER PR_NUMBER <<< $(extract_issue_number "$COMMIT_MESSAGE")

if [ -z "$ISSUE_NUMBER" ]; then
echo "Could not determine the issue number from the commit message."
exit 0
fi

if [ -n "$PR_NUMBER" ]; then
COMMENT_BODY="The latest changes (#$PR_NUMBER) have been deployed successfully to [$URL_QA]($URL_QA)"
else
COMMENT_BODY="The latest changes have been deployed successfully to [$URL_QA]($URL_QA)"
fi

# Make the API call and capture both the HTTP status code and the response body
RESPONSE=$(curl -s -w "\n%{http_code}" -X POST \
-H "Authorization: token $GH_TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
"$API_URL/repos/$GITHUB_REPOSITORY/issues/$ISSUE_NUMBER/comments" \
-d "{\"body\":\"$COMMENT_BODY\"}")

# Extract the response body and status code
RESPONSE_BODY=$(echo "$RESPONSE" | sed '$d')
STATUS_CODE=$(echo "$RESPONSE" | tail -n1)

if [ "$STATUS_CODE" -ne 201 ]; then
echo "Failed to post comment to issue #$ISSUE_NUMBER. HTTP status code: $STATUS_CODE"
echo "Response body:"
echo "$RESPONSE_BODY"
print_debug_info
exit 1
fi

echo "Comment posted to issue #$ISSUE_NUMBER"
3 changes: 3 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,8 @@ jobs:
direnvVersion: 2.32.3

- name: Deploy
env:
GITHUB_REPOSITORY: ${{ github.repository }}
run: |
deploy-to-nixos ${{ env.PROJECT_NAME}}-${{ env.ENV }}
.github/workflows/notify.sh
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ To use the GitHub Actions workflow in this project:
- `SSH_HOST`: The hostname or IP address of your deployment server
- `SSH_USER`: The username for SSH access to the deployment server
- `SSH_PRIVATE_KEY`: The private SSH key for authentication
- Optionally `GH_TOKEN`, a GitHub API token that is able to post comments to issues under the given project.

2. Modify the `env` section in `.github/workflows/test.yml` if needed:
- Update `PROJECT_NAME` to match your project
Expand All @@ -68,4 +69,4 @@ Feel free to customize the workflow file to fit your specific project needs. You

For issues related to IHP or this project's setup, please refer to the [IHP documentation](https://ihp.digitallyinduced.com/Guide/) or seek help on the [IHP Forum](https://ihp.digitallyinduced.com/community/).

For project-specific issues, please open an issue in this repository.
For project-specific issues, please open an issue in this repository.