Build geoip files #198
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
| name: Build geoip files | |
| on: | |
| workflow_dispatch: | |
| schedule: | |
| - cron: "0 */8 * * *" | |
| push: | |
| branches: | |
| - main | |
| paths-ignore: | |
| - ".gitignore" | |
| - "config-example.json" | |
| - "LICENSE" | |
| - "README.md" | |
| - ".github/dependabot.yml" | |
| - ".github/workflows/pr-check.yml" | |
| concurrency: | |
| group: geoip-release | |
| cancel-in-progress: false | |
| jobs: | |
| build: | |
| name: Build & Release | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: write | |
| steps: | |
| - name: Checkout codebase | |
| uses: actions/checkout@v6 | |
| with: | |
| fetch-depth: 0 | |
| - name: Set up Go | |
| uses: actions/setup-go@v6 | |
| with: | |
| go-version-file: ./go.mod | |
| - name: Set variables | |
| run: | | |
| echo "TAG_NAME=$(date +%Y%m%d%H%M)" >> $GITHUB_ENV | |
| echo "RELEASE_NAME=$(date +%Y%m%d%H%M)" >> $GITHUB_ENV | |
| shell: bash | |
| - name: Download GeoLite2 databases | |
| run: | | |
| mkdir -p output | |
| curl -L "https://raw.githubusercontent.com/Loyalsoldier/geoip/refs/heads/release/GeoLite2-ASN.tar.gz" -o ./output/GeoLite2-ASN.tar.gz | |
| curl -L "https://raw.githubusercontent.com/Loyalsoldier/geoip/refs/heads/release/GeoLite2-ASN-CSV.zip" -o ./output/GeoLite2-ASN-CSV.zip | |
| curl -L "https://raw.githubusercontent.com/Loyalsoldier/geoip/refs/heads/release/GeoLite2-Country.tar.gz" -o ./output/GeoLite2-Country.tar.gz | |
| curl -L "https://raw.githubusercontent.com/Loyalsoldier/geoip/refs/heads/release/GeoLite2-Country-CSV.zip" -o ./output/GeoLite2-Country-CSV.zip | |
| - name: Prepare GeoLite2 databases | |
| run: | | |
| cp ./output/{*.gz,*.zip} ./ | |
| unzip GeoLite2-Country-CSV.zip | |
| unzip GeoLite2-ASN-CSV.zip | |
| tar -xvzf GeoLite2-Country.tar.gz | |
| tar -xvzf GeoLite2-ASN.tar.gz | |
| cp GeoLite2-Country_*/*.mmdb ./output/ | |
| cp GeoLite2-ASN_*/*.mmdb ./output/ | |
| cp GeoLite2-Country-CSV_*/{GeoLite2-Country-Blocks-*,GeoLite2-Country-Locations-en,GeoLite2-Country-Locations-zh-CN}.csv ./output/ | |
| cp GeoLite2-ASN-CSV_*/*.csv ./output/ | |
| mkdir -p geolite2 | |
| cp GeoLite2-Country-CSV_*/*.csv ./geolite2/ | |
| cp GeoLite2-ASN-CSV_*/*.csv ./geolite2/ | |
| - name: Build geoip files | |
| run: | | |
| go build ./ | |
| ./geoip convert -c ./config.json | |
| - name: Verify mmdb files | |
| run: | | |
| cd ./output || exit 1 | |
| go install -v github.com/maxmind/mmdbverify@latest | |
| for name in $(ls *.mmdb); do | |
| $(go env GOPATH)/bin/mmdbverify -file ${name} | |
| done | |
| - name: Generate sha256 checksum for dat files | |
| run: | | |
| cd ./output/dat || exit 1 | |
| for name in $(ls *.dat); do | |
| sha256sum ${name} > ./${name}.sha256sum | |
| done | |
| - name: Generate sha256 checksum for mmdb files | |
| run: | | |
| cd ./output || exit 1 | |
| for name in $(ls *.mmdb); do | |
| sha256sum ${name} > ./${name}.sha256sum | |
| done | |
| - name: Move files to publish directory | |
| run: | | |
| mkdir -p publish | |
| cp -a output/. publish/ | |
| - name: Git push assets to "release" branch | |
| run: | | |
| mkdir -p release_repo | |
| cd release_repo || exit 1 | |
| git init | |
| git remote add geoip "https://${{ github.actor }}:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}" | |
| git fetch geoip release || true | |
| git checkout -b release geoip/release || git checkout -b release | |
| git config --local user.name "github-actions[bot]" | |
| git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com" | |
| # 覆盖文件内容 | |
| diff -r ../publish . || true | |
| rm -rf * | |
| cp -r ../publish/* . | |
| git add -A | |
| git commit -m "${{ env.RELEASE_NAME }}" || echo "No changes to commit" | |
| git push geoip release | |
| - name: Purge jsdelivr CDN | |
| run: | | |
| cd publish || exit 1 | |
| for file in $(ls); do | |
| curl -i "https://purge.jsdelivr.net/gh/${{ github.repository }}@release/${file}" | |
| done | |
| - name: Remove some files to avoid publishing to GitHub release | |
| run: | | |
| rm -rf ./publish/*.{gz,zip} | |
| rm -rf ./publish/GeoLite2-*.csv | |
| rm -rf ./publish/GeoLite2-*.mmdb | |
| rm -rf ./publish/GeoLite2-*.sha256sum | |
| rm -rf ./publish/{clash,dat,mrs,nginx,srs,surge,text} | |
| - name: Upload files to GitHub release | |
| uses: svenstaro/upload-release-action@v2 | |
| with: | |
| repo_token: ${{ secrets.GITHUB_TOKEN }} | |
| file_glob: true | |
| file: ./publish/* | |
| tag: ${{ env.TAG_NAME }} | |
| - name: Remove old Releases | |
| uses: dev-drprasad/delete-older-releases@v0.3.4 | |
| with: | |
| keep_latest: 3 | |
| delete_tags: true | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |