Automated Update of Blocked Hosts File #177
This file contains 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
# Name of the workflow | |
name: Automated Update of Blocked Hosts File | |
# Define the events that trigger the workflow | |
on: | |
# Trigger the workflow on a schedule (every day at midnight) | |
schedule: | |
- cron: "0 0 * * *" | |
# Allow manual triggering of the workflow | |
workflow_dispatch: | |
# Define the jobs in the workflow | |
jobs: | |
# Define a job named "build" | |
build: | |
# Name of the job that will be displayed on GitHub | |
name: Automated Hosts File Update | |
# The type of runner that the job will run on | |
runs-on: ubuntu-latest | |
# Environment variables available to all steps in the job | |
env: | |
CONTENT_BLOCKER_URL: https://raw.githubusercontent.com/complexorganizations/content-blocker/main/assets/hosts | |
CONTENT_BLOCKER_PATH: assets/hosts | |
# Define the steps in the job | |
steps: | |
# Check out the repository code | |
- name: Check out code into the Go module directory | |
uses: actions/checkout@v4 | |
# Get the current SHA-3-512 hash of the file | |
- name: Get the current SHA-3-512 hash of file | |
run: echo "CONTENT_BLOCKER_CURRENT_HASH=$(openssl dgst -sha3-512 "${{ env.CONTENT_BLOCKER_PATH }}" | awk '{print $2}')" >> $GITHUB_ENV | |
# Get the future SHA-3-512 hash of the file | |
- name: Get the future SHA-3-512 hash of file | |
run: echo "CONTENT_BLOCKER_FUTURE_HASH=$(curl --silent "${{ env.CONTENT_BLOCKER_URL }}" | openssl dgst -sha3-512 | awk '{print $2}')" >> $GITHUB_ENV | |
# Check if an update is needed | |
- name: Check if update is needed | |
run: | | |
if [ "${{ env.CONTENT_BLOCKER_CURRENT_HASH }}" != "${{ env.CONTENT_BLOCKER_FUTURE_HASH }}" ]; then | |
curl "${{ env.CONTENT_BLOCKER_URL }}" -o "${{ env.CONTENT_BLOCKER_PATH }}" | |
else | |
echo "No update needed" | |
exit 0 | |
fi | |
# Push the updated file to GitHub | |
- name: Push the updated hosts file to GitHub | |
run: | | |
git config user.name github-actions | |
git config user.email github-actions@github.com | |
if git diff --exit-code --quiet -- "${{ env.CONTENT_BLOCKER_PATH }}"; then | |
echo "No changes to commit" | |
exit 0 | |
else | |
git add "${{ env.CONTENT_BLOCKER_PATH }}" | |
git commit -m "Updated hosts $(date)" | |
git push | |
fi |