|
| 1 | +name: 'Commit and push if the version file has changed' |
| 2 | + |
| 3 | +inputs: |
| 4 | + name: |
| 5 | + description: 'The name of the commiter' |
| 6 | + required: true |
| 7 | + default: 'github-actions[bot]' |
| 8 | + email: |
| 9 | + description: 'The email of the commiter' |
| 10 | + required: true |
| 11 | + default: 'github-actions[bot]@users.noreply.github.com' |
| 12 | + message: |
| 13 | + description: 'The commit message' |
| 14 | + required: true |
| 15 | + default: 'New release version(s)' |
| 16 | + files: |
| 17 | + description: 'The file(s) to add in the commit' |
| 18 | + required: true |
| 19 | + default: '*' |
| 20 | + directory: |
| 21 | + description: 'The directory in which the action will be performed' |
| 22 | + required: true |
| 23 | + default: '.' |
| 24 | + branch: |
| 25 | + description: 'Checkout (or create) on a specific branch before commit/push' |
| 26 | + required: true |
| 27 | + default: 'master' |
| 28 | + secret: |
| 29 | + description: 'A token allowing to push the commit on the repository' |
| 30 | + required: true |
| 31 | + default: '.' |
| 32 | + repository: |
| 33 | + description: 'The repository where to push' |
| 34 | + required: true |
| 35 | + default: '' |
| 36 | + |
| 37 | +runs: |
| 38 | + using: 'composite' |
| 39 | + steps: |
| 40 | + - name: Commit the changes |
| 41 | + id: commit |
| 42 | + run: | |
| 43 | + git config --global user.name ${{ inputs.name }} |
| 44 | + ORIGIN="$(pwd)" |
| 45 | + cd ${{ inputs.directory }} |
| 46 | + git switch ${{ inputs.branch }} 2>/dev/null || git switch -c ${{ inputs.branch }} |
| 47 | + echo "-----------------------------------------------------------" |
| 48 | + echo "Initial repo status" |
| 49 | + git status |
| 50 | + CHANGES="$(git status --porcelain ${{ inputs.files }})" |
| 51 | + if [ -z "${CHANGES}" ]; \ |
| 52 | + then \ |
| 53 | + echo "-----------------------------------------------------------"; \ |
| 54 | + echo "No changes, stopping now"; \ |
| 55 | + echo "COMMIT=NO" > $GITHUB_ENV; \ |
| 56 | + cd "${ORIGIN}"; \ |
| 57 | + exit 0; \ |
| 58 | + fi |
| 59 | + echo -e "Changes:\n${CHANGES}" |
| 60 | + git add ${{ inputs.files }} |
| 61 | + echo "-----------------------------------------------------------" |
| 62 | + echo "Repo status before commit" |
| 63 | + git status |
| 64 | + git commit -am "${{ inputs.message }}" |
| 65 | + echo "COMMIT=YES" > $GITHUB_ENV |
| 66 | + git log -n 2 |
| 67 | + cd "${ORIGIN}" |
| 68 | + shell: bash |
| 69 | + |
| 70 | + - run: echo "${{ env.COMMIT }}" |
| 71 | + shell: bash |
| 72 | + |
| 73 | + - name: Push commit |
| 74 | + if: ${{ env.COMMIT == 'YES' }} |
| 75 | + uses: ad-m/github-push-action@master |
| 76 | + with: |
| 77 | + github_token: ${{ inputs.secret }} |
| 78 | + branch: ${{ inputs.branch }} |
| 79 | + directory: ${{ inputs.directory }} |
| 80 | + repository: ${{ inputs.repository }} |
0 commit comments