Check if Build Needed #589
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: Check if Build Needed | |
| on: | |
| schedule: | |
| - cron: '12 2 * * *' | |
| workflow_dispatch: | |
| inputs: | |
| oxce-ref: | |
| description: 'OXCE ref' | |
| required: true | |
| default: 'oxce-plus' | |
| force-build: | |
| description: 'Force build even if the version is not newer' | |
| required: false | |
| default: 'false' | |
| permissions: | |
| contents: write | |
| jobs: | |
| check-build: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Set default OXCE ref | |
| if: ${{ github.event_name == 'schedule' }} | |
| run: echo "oxce-ref=oxce-plus" >> $GITHUB_ENV | |
| - name: Checkout current repository | |
| uses: actions/checkout@v4 | |
| with: | |
| token: ${{ secrets.GITHUB_TOKEN }} | |
| fetch-depth: 0 # Fetch all history for all tags | |
| - name: Clone OpenXcom repository | |
| run: | | |
| git clone --depth 100 https://github.com/MeridianOXC/OpenXcom.git | |
| cd OpenXcom | |
| git checkout ${{ github.event.inputs.oxce-ref || env.oxce-ref }} | |
| - name: Extract version from src/version.h | |
| id: get_version | |
| run: | | |
| cd OpenXcom | |
| version_short_line=$(grep '#define OPENXCOM_VERSION_SHORT' src/version.h) | |
| version_short=$(echo $version_short_line | grep -oP '(?<=").*(?=")' | sed 's/Extended //') | |
| version_git_line=$(grep '#define OPENXCOM_VERSION_GIT' src/version.h) | |
| version_git=$(echo $version_git_line | grep -oP '(v[^"]*)' | sed 's/v//; s/)//') | |
| combined_version="${version_short}-${version_git}" | |
| echo "::set-output name=version::$combined_version" | |
| - name: Get newest tag of current repository | |
| id: get_tag | |
| run: | | |
| latest_tag=$(git describe --tags `git rev-list --tags --max-count=1`) | |
| echo "::set-output name=latest_tag::$latest_tag" | |
| - name: Install semver | |
| run: npm install -g semver | |
| - name: Compare versions | |
| id: compare_versions | |
| shell: bash | |
| run: | | |
| version="${{ steps.get_version.outputs.version }}" | |
| latest_tag="${{ steps.get_tag.outputs.latest_tag }}" | |
| force_build="${{ github.event.inputs.force-build }}" | |
| # Extract numeric and date parts | |
| IFS='-' read -r version_numeric version_date <<< "$version" | |
| IFS='-' read -r latest_tag_numeric latest_tag_date <<< "$latest_tag" | |
| # Normalize versions to have 3 parts (major.minor.patch) | |
| version_numeric=$(echo "$version_numeric" | awk -F. '{printf "%d.%d.%d", $1, $2, $3+0}') | |
| latest_tag_numeric=$(echo "$latest_tag_numeric" | awk -F. '{printf "%d.%d.%d", $1, $2, $3+0}') | |
| echo "our version numeric: $version_numeric" | |
| echo "our version date: $version_date" | |
| echo "latest tag numeric: $latest_tag_numeric" | |
| echo "latest tag date: $latest_tag_date" | |
| # Compare versions | |
| if [ "$force_build" == "true" ]; then | |
| echo "Force build is enabled." | |
| echo "SHOULD_TAG=true" >> $GITHUB_ENV | |
| elif semver "$version_numeric" -r ">$latest_tag_numeric"; then | |
| echo "src/version.h version ($version) is newer than the newest tag ($latest_tag, version number)." | |
| echo "SHOULD_TAG=true" >> $GITHUB_ENV | |
| elif semver "$version_numeric" -r "=$latest_tag_numeric" && [[ "$version_date" > "$latest_tag_date" ]]; then | |
| echo "src/version.h version ($version) is newer than the newest tag ($latest_tag, date part)." | |
| echo "SHOULD_TAG=true" >> $GITHUB_ENV | |
| else | |
| echo "src/version.h version ($version) is not newer than the newest tag ($latest_tag)." | |
| echo "SHOULD_TAG=false" >> $GITHUB_ENV | |
| fi | |
| - name: Commit version to version.txt | |
| if: env.SHOULD_TAG == 'true' | |
| run: | | |
| version="${{ steps.get_version.outputs.version }}" | |
| random_string=$(openssl rand -hex 8) | |
| echo "$version-$random_string" > version.txt | |
| git config --global user.name "github-actions[bot]" | |
| git config --global user.email "github-actions[bot]@users.noreply.github.com" | |
| git add version.txt | |
| git commit -m "Update version to $version-$random_string" | |
| git push origin main | |
| - name: Create and push new tag | |
| if: env.SHOULD_TAG == 'true' | |
| run: | | |
| version="${{ steps.get_version.outputs.version }}" | |
| git tag "$version" | |
| git push origin "$version" | |
| - name: Trigger Build and Release Workflow | |
| if: env.SHOULD_TAG == 'true' | |
| run: | | |
| version="${{ steps.get_version.outputs.version }}" | |
| curl -X POST -H "Accept: application/vnd.github.v3+json" \ | |
| -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ | |
| https://api.github.com/repos/${{ github.repository }}/dispatches \ | |
| -d '{"event_type":"build-and-release","client_payload":{"tag":"'"$version"'", "oxce-ref":"'"${{ github.event.inputs.oxce-ref || env.oxce-ref }}"'"}}' |