Bump actions/checkout from 4 to 6 #232
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
| # This workflow should cleanup everything unneeded from the template project | |
| name: Template Janitor | |
| on: | |
| pull_request: | |
| release: | |
| types: [published] | |
| push: | |
| branches: | |
| - main | |
| - develop | |
| permissions: | |
| contents: write | |
| env: | |
| TEMPLATES_PATH: ".github/template" | |
| jobs: | |
| template-cleanup: | |
| name: Cleanup after create | |
| runs-on: ubuntu-latest | |
| strategy: | |
| matrix: | |
| compiler: | |
| - gcc-14 | |
| generator: | |
| - "Unix Makefiles" | |
| build_type: | |
| - Debug | |
| packaging_maintainer_mode: | |
| - OFF | |
| steps: | |
| - uses: actions/checkout@v6 | |
| - name: Setup Cache | |
| uses: ./.github/actions/setup_cache | |
| with: | |
| compiler: ${{ matrix.compiler }} | |
| build_type: ${{ matrix.build_type }} | |
| package_maintainer_mode: ${{ matrix.package_maintainer_mode }} | |
| generator: ${{ matrix.generator }} | |
| - name: Get organization and project name | |
| run: | | |
| echo "NEW_ORG=${{ github.repository_owner }}" >> $GITHUB_ENV | |
| echo "NEW_PROJECT=${{ github.event.repository.name }}" >> $GITHUB_ENV | |
| echo "NEW_URL=${{ github.repositoryUrl }}" >> $GITHUB_ENV | |
| - uses: octokit/request-action@v2.x | |
| id: get_repo_meta | |
| with: | |
| route: GET /repos/${{ env.NEW_ORG }}/${{ env.NEW_PROJECT }} | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Export description to variable | |
| # This is basically needed as we have to have the value in an env variable to further process it in a safe manner. | |
| # Also we can inject a mock-string for testing if the project is still the template. | |
| if: fromJson(steps.get_repo_meta.outputs.data).is_template == false | |
| run: | | |
| # description can contain characters that mess with the sed command, so store it in a variable first. | |
| # The content of the description will be copied as-is by the action which makes it nearly impossible to "just" use it. | |
| # But by storing it in a variable with a heredoc the sed command will accept quotes and single quotes without a problem. | |
| # The heredoc delimiter is deliberately verbose and complex to reduce the likeliness someone accidentally puts it in their | |
| # description. | |
| echo NEW_DESCRIPTION="$(cat <<'do;not(include}this[in%the$description' | |
| ${{ fromJson(steps.get_repo_meta.outputs.data).description }} | |
| do;not(include}this[in%the$description | |
| )" >> $GITHUB_ENV | |
| - name: Use testing variables if still a template | |
| if: fromJson(steps.get_repo_meta.outputs.data).is_template == true | |
| run: | | |
| # This name is unsafe because it is not a valid C++ identifier | |
| echo "NEW_PROJECT=my-unsafe.project" >> $GITHUB_ENV | |
| # This name is unsafe as the sed command later uses surrounding quotes and the pipe symbol. The other characters are generally harmful too. | |
| NEW_DESCRIPTION=$(cat <<'EOF' | |
| Unsafe because of "quotes" and unbalanced "quotes ('Also' 'unbalanced single). The sed uses | and used to have /. Variable expansion might be bad $GITHUB_ENV as well. Also \ should stay. | |
| EOF | |
| ) | |
| echo NEW_DESCRIPTION="$NEW_DESCRIPTION" >> $GITHUB_ENV | |
| - name: Add safe replacement variable versions | |
| run: | | |
| # hyphens and dots in c++ identifiers are forbidden. Use underscores instead. | |
| NEW_SAFE_PROJECT=$(echo ${{ env.NEW_PROJECT }} | sed "s/-/_/g" | sed "s/\./_/g" ) | |
| echo "NEW_SAFE_PROJECT=$NEW_SAFE_PROJECT" >> $GITHUB_ENV | |
| # The sed command uses the pipe as the delimiter so escape that to make it safe. | |
| # Also as we would remove any literal \ we have to escape those aswell and that has to | |
| # be done first as it would mess with the escape for the | otherwise. | |
| NEW_SAFE_DESCRIPTION="$(echo "$NEW_DESCRIPTION" | sed 's/\\/\\\\/g' | sed 's/|/\\|/g' )" | |
| echo "NEW_SAFE_DESCRIPTION=$NEW_SAFE_DESCRIPTION" >> $GITHUB_ENV | |
| # Rename all cpp_starter_project occurences to current repository and remove this workflow | |
| - name: Insert new org and project | |
| run: | | |
| # rename the CMake project to match the github project | |
| find src include test fuzz_test cmake -type f -exec sed -i "s/myproject/${{ env.NEW_SAFE_PROJECT }}/gi" .github/constants.env CMakeLists.txt Dependencies.cmake ProjectOptions.cmake .github/workflows/ci.yml .github/workflows/codeql-analysis.yml configured_files/config.hpp.in {} + | |
| # Update URL placeholders for project | |
| sed -i "s|%%myurl%%|${{ fromJson(steps.get_repo_meta.outputs.data).html_url }}|gi" CMakeLists.txt | |
| # fill in placeholders of readme and move it into place | |
| sed -i "s/%%myorg%%/${{ env.NEW_ORG }}/g" ${{ env.TEMPLATES_PATH }}/README.md | |
| sed -i "s/%%myproject%%/${{ env.NEW_PROJECT }}/g" ${{ env.TEMPLATES_PATH }}/README.md | |
| # Use the variable from the env directly as githubs expansion would break the sed command. | |
| sed -i "s|%%description%%|$NEW_SAFE_DESCRIPTION|g" ${{ env.TEMPLATES_PATH }}/README.md | |
| mv include/myproject include/${{ env.NEW_SAFE_PROJECT }} | |
| cp ${{ env.TEMPLATES_PATH }}/README.md README.md | |
| - name: Print diff after replacement | |
| run: | | |
| # Exclude the README as that is checked separately! | |
| git diff ':!README.md' | |
| # following should not have any diffs | |
| diff ${{ env.TEMPLATES_PATH }}/README.md README.md | |
| - name: Remove unwanted files | |
| run: | | |
| # No tests needed as this will fail if any file from the list is missing/misspelled | |
| xargs rm -r < ${{ env.TEMPLATES_PATH }}/removal-list | |
| - name: Clean up before commit and push | |
| run: | | |
| rm -r ${{ env.TEMPLATES_PATH }} | |
| # Can we get that from a variable? | |
| # Remove this workflow as it has fulfilled its purpose | |
| rm .github/workflows/template-janitor.yml | |
| - name: Setup Cpp | |
| uses: aminya/setup-cpp@v1 | |
| with: | |
| compiler: ${{ matrix.compiler }} | |
| cmake: true | |
| ninja: false | |
| vcpkg: false | |
| ccache: false | |
| clangtidy: false | |
| cppcheck: false | |
| gcovr: false | |
| opencppcoverage: false | |
| - name: Project Name | |
| uses: cardinalby/export-env-action@v2 | |
| with: | |
| envFile: '.github/constants.env' | |
| - name: Test simple configuration to make sure nothing broke | |
| run: | | |
| cmake -S . -B ./build -G "${{ matrix.generator }}" -DCMAKE_BUILD_TYPE:STRING=${{ matrix.build_type }} -D${{ env.NEW_SAFE_PROJECT }}_PACKAGING_MAINTAINER_MODE:BOOL=ON | |
| # Build it because we may have broken something in the cpp/hpp files | |
| cmake --build build | |
| - uses: EndBug/add-and-commit@v9 | |
| # only commit and push if we are not a template project anymore! | |
| if: fromJson(steps.get_repo_meta.outputs.data).is_template != true | |
| with: | |
| add: -A | |
| author_name: Template Janitor | |
| author_email: template.janitor@example.com | |
| message: 'Cleanup template and initialize repository' | |
| pathspec_error_handling: exitImmediately | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| template-rename: | |
| name: Renames template when a new name is detected | |
| runs-on: ubuntu-latest | |
| strategy: | |
| matrix: | |
| compiler: | |
| - gcc-11 | |
| generator: | |
| - "Unix Makefiles" | |
| build_type: | |
| - Debug | |
| packaging_maintainer_mode: | |
| - ON | |
| steps: | |
| - uses: actions/checkout@v6 | |
| - name: Setup Cache | |
| uses: ./.github/actions/setup_cache | |
| with: | |
| compiler: ${{ matrix.compiler }} | |
| build_type: ${{ matrix.build_type }} | |
| packaging_maintainer_mode: ${{ matrix.packaging_maintainer_mode }} | |
| generator: ${{ matrix.generator }} | |
| - name: Get organization and project name | |
| run: | | |
| echo "TEST_RUN=false" >> $GITHUB_ENV | |
| echo "NEW_ORG=${{ github.repository_owner }}" >> $GITHUB_ENV | |
| echo "NEW_PROJECT=${{ github.event.repository.name }}" >> $GITHUB_ENV | |
| echo "NEW_REPOSITORY=${{ github.repository }}" >> $GITHUB_ENV | |
| echo "TEMPLATE_NAME=`cat ${{ env.TEMPLATES_PATH }}/template_name`" >> $GITHUB_ENV | |
| echo "TEMPLATE_REPOSITORY=`cat ${{ env.TEMPLATES_PATH }}/template_repository`" >> $GITHUB_ENV | |
| - uses: octokit/request-action@v2.x | |
| id: get_repo_meta | |
| with: | |
| route: GET /repos/${{ env.NEW_ORG }}/${{ env.NEW_PROJECT }} | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Setup fake test org/project names if project didn't change | |
| if: env.TEMPLATE_NAME == env.NEW_PROJECT | |
| run: | | |
| echo "TEST_RUN=true" >> $GITHUB_ENV | |
| echo "NEW_ORG=${{ github.repository_owner }}" >> $GITHUB_ENV | |
| echo "NEW_PROJECT=TEST_PROJECT" >> $GITHUB_ENV | |
| echo "NEW_REPOSITORY=TEST_REPOSITORY" >> $GITHUB_ENV | |
| # Rename all cpp_starter_project occurrences to current repository and remove this workflow | |
| - name: Update repository to match new template information | |
| run: | | |
| # Update the README and template files to match the new org / repository names | |
| sed -i "s|${{ env.TEMPLATE_REPOSITORY }}|${{ env.NEW_REPOSITORY }}|g" README.md ${{ env.TEMPLATES_PATH }}/template_repository | |
| sed -i "s|${{ env.TEMPLATE_NAME }}|${{ env.NEW_PROJECT }}|g" README.md ${{ env.TEMPLATES_PATH }}/template_name | |
| - name: Print diff after template name replacement | |
| run: | | |
| git diff | |
| - name: Setup Cpp | |
| uses: aminya/setup-cpp@v1 | |
| with: | |
| compiler: gcc | |
| cmake: true | |
| ninja: false | |
| vcpkg: false | |
| ccache: false | |
| clangtidy: false | |
| cppcheck: false | |
| gcovr: false | |
| opencppcoverage: false | |
| - name: Test simple configuration to make sure nothing broke (default compiler,cmake,packaging_maintainer_mode OFF) | |
| run: | | |
| cmake -S . -B ./build -G "${{ matrix.generator }}" -DCMAKE_BUILD_TYPE:STRING=${{ matrix.build_type }} -D${{ env.PROJECT_NAME }}_PACKAGING_MAINTAINER_MODE:BOOL=ON | |
| - uses: EndBug/add-and-commit@v9 | |
| # only commit and push if we are a template and project name has changed | |
| if: fromJson(steps.get_repo_meta.outputs.data).is_template == true && env.TEST_RUN == 'false' | |
| with: | |
| add: -A | |
| author_name: Template Janitor | |
| author_email: template.janitor@example.com | |
| message: 'Change Template Name' | |
| pathspec_error_handling: exitImmediately | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |