|
| 1 | +author: AVM |
| 2 | +name: Module version check |
| 3 | +description: Checks that module version has been updated on PR |
| 4 | +inputs: |
| 5 | + version-file-name: |
| 6 | + description: Terraform JSON file containing module version |
| 7 | + required: false |
| 8 | + default: locals.version.tf.json |
| 9 | + jq-query: |
| 10 | + description: jq query to extract module version |
| 11 | + required: false |
| 12 | + default: .locals.module_version |
| 13 | + github_token: |
| 14 | + description: GitHub token |
| 15 | + required: true |
| 16 | +runs: |
| 17 | + using: composite |
| 18 | + steps: |
| 19 | + - name: semver regex |
| 20 | + shell: bash |
| 21 | + run: | |
| 22 | + echo SEMVER_REGEX="^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$" >> "$GITHUB_ENV" |
| 23 | +
|
| 24 | + - name: Get latest release version |
| 25 | + shell: bash |
| 26 | + run: | |
| 27 | + VER=$(curl --silent -L -H "Authorization: Bearer ${{ inputs.github_token }}" -H "Accept: application/vnd.github+json" -H "X-GitHub-Api-Version: 2022-11-28" https://api.github.com/repos/${{ github.repository }}/releases/latest | jq -r .name | sed s/^v//) |
| 28 | + if [ "$VER" == "null" ]; then |
| 29 | + echo "No releases found" |
| 30 | + echo LATEST_RELEASE="0.0.0" >> "$GITHUB_ENV" |
| 31 | + exit 0 |
| 32 | + fi |
| 33 | + if echo "$VER" | grep -P -qv "$SEMVER_REGEX"; then |
| 34 | + echo "Release version $VER is not a valid semantic version" |
| 35 | + exit 1 |
| 36 | + fi |
| 37 | + echo LATEST_RELEASE="$VER" >> "$GITHUB_ENV" |
| 38 | +
|
| 39 | + - name: Get current module version |
| 40 | + shell: bash |
| 41 | + run: | |
| 42 | + VER=$(jq -r '${{ inputs.jq-query }}' < ${{ inputs.version-file-name }}) |
| 43 | + if echo "$VER" | grep -P -qv "$SEMVER_REGEX"; then |
| 44 | + echo "Module version $VER is not a valid semantic version" |
| 45 | + exit 1 |
| 46 | + fi |
| 47 | + echo MODULE_VERSION="$VER" >> "$GITHUB_ENV" |
| 48 | +
|
| 49 | + - name: Check module version is greater than latest release |
| 50 | + shell: bash |
| 51 | + run: | |
| 52 | + MODVERMAJOR=$(echo ${{ env.MODULE_VERSION }} | cut -d. -f1) |
| 53 | + MODVERMINOR=$(echo ${{ env.MODULE_VERSION }} | cut -d. -f2) |
| 54 | + MODVERPATCH=$(echo ${{ env.MODULE_VERSION }} | cut -d. -f3) |
| 55 | +
|
| 56 | + RELVERMAJOR=$(echo ${{ env.LATEST_RELEASE }} | cut -d. -f1) |
| 57 | + RELVERMINOR=$(echo ${{ env.LATEST_RELEASE }} | cut -d. -f2) |
| 58 | + RELVERPATCH=$(echo ${{ env.LATEST_RELEASE }} | cut -d. -f3) |
| 59 | +
|
| 60 | + if [ "$MODVERMAJOR" -lt "$RELVERMAJOR" ]; then |
| 61 | + echo "Module version ${{ env.MODULE_VERSION }} is less than latest release ${{ env.LATEST_RELEASE }}" |
| 62 | + exit 1 |
| 63 | + fi |
| 64 | +
|
| 65 | + if [ "$MODVERMAJOR" -eq "$RELVERMAJOR" ] && [ "$MODVERMINOR" -lt "$RELVERMINOR" ]; then |
| 66 | + echo "Module version ${{ env.MODULE_VERSION }} is less than latest release ${{ env.LATEST_RELEASE }}" |
| 67 | + exit 1 |
| 68 | + fi |
| 69 | +
|
| 70 | + if [ "$MODVERMAJOR" -eq "$RELVERMAJOR" ] && [ "$MODVERMINOR" -eq "$RELVERMINOR" ] && [ "$MODVERPATCH" -lt "$RELVERPATCH" ]; then |
| 71 | + echo "Module version ${{ env.MODULE_VERSION }} is less than latest release ${{ env.LATEST_RELEASE }}" |
| 72 | + exit 1 |
| 73 | + fi |
0 commit comments