|
| 1 | +name: 'Generate a version-with-buildnum.txt file' |
| 2 | +description: | |
| 3 | + Generate a version string that includes a generated build number and build |
| 4 | + variant appended. The version.txt file is used to obtain the branch |
| 5 | + invariant portion of the full version. The generated build number will |
| 6 | + be unique (within reason) and increasing so that within a lineage the full |
| 7 | + version number encodes relative position of any build. The generated build |
| 8 | + number is not guaranteed to be sequential. |
| 9 | + |
| 10 | + The current implementation uses a time based build number. Sequence numbers |
| 11 | + from the CI/CD system were also considered, and can work just as well. |
| 12 | + Repository hashes were not used since they neither encode the relative |
| 13 | + build position nor are they unique upon rebuild. |
| 14 | + |
| 15 | + The generated version is output both as a job output string and as a |
| 16 | + build artifact file that contains the same. |
| 17 | + |
| 18 | + This action assumes it is working with a Semantic Versioning version |
| 19 | + number. The form of the final, full version number is |
| 20 | + <branch invariant version>.<build number>-<build variant> |
| 21 | + |
| 22 | + References: |
| 23 | + https://semver.org/ |
| 24 | + https://packaging.python.org/en/latest/specifications/version-specifiers/ |
| 25 | +
|
| 26 | +# TODO: Detect branch for consideration of constructing a build_variant automatically? |
| 27 | +# Unless we have a strict branching/release policy, this may never be possible. |
| 28 | +# TODO: add support for epochs (e.g. "<epoch>!" prefix) |
| 29 | +# TODO: add support for local variants (e.g. "+<local variant>" suffix) |
| 30 | +# TODO: Can/should we *also* encode the SHA? While I dislike its lack of sequence |
| 31 | +# or uniqueness, it *does* encode useful information. |
| 32 | + |
| 33 | +inputs: |
| 34 | + build-variant: |
| 35 | + description: | |
| 36 | + Build variant. E.g. "dev", "beta", "rc", etc. Use the keyword "release" for |
| 37 | + production releases that should not specify a variant. |
| 38 | + required: true |
| 39 | + |
| 40 | +outputs: |
| 41 | + version-with-buildnum: |
| 42 | + description: 'Full version with variant build number appended' |
| 43 | + value: ${{ steps.generate-version-with-buildnum.outputs.version-with-buildnum }} |
| 44 | + version: |
| 45 | + description: 'Just the simple branch invariant version' |
| 46 | + value: ${{ steps.generate-version-with-buildnum.outputs.version }} |
| 47 | + buildnum: |
| 48 | + description: 'Just the generated build number' |
| 49 | + value: ${{ steps.generate-version-with-buildnum.outputs.buildnum }} |
| 50 | + variant-normal: |
| 51 | + description: 'Normalized build variant string' |
| 52 | + value: ${{ steps.generate-version-with-buildnum.outputs.variant-normal }} |
| 53 | + variant-raw: |
| 54 | + description: 'Raw build variant string (same as input)' |
| 55 | + value: ${{ steps.generate-version-with-buildnum.outputs.variant-raw }} |
| 56 | + |
| 57 | +runs: |
| 58 | + using: "composite" |
| 59 | + steps: |
| 60 | + - id: generate-version-with-buildnum |
| 61 | + name: "Prepare build version file" |
| 62 | + shell: bash |
| 63 | + run: | |
| 64 | + # Should be closely synonymous with SCM branch |
| 65 | + version_invariant=$(head -1 version.txt | tr -d '\n') |
| 66 | +
|
| 67 | + # "pre-release" in semver |
| 68 | + if [ "${{ inputs.build-variant }}" = "release" ] |
| 69 | + then |
| 70 | + version_variant="" |
| 71 | + else |
| 72 | + version_variant=${{ inputs.build-variant }} |
| 73 | + fi |
| 74 | +
|
| 75 | + # Generated build number |
| 76 | + build_timestamp_b10=$(date +%s) # Time based build number - Base 10 |
| 77 | + build_timestamp_b16=$(printf '%x' ${build_timestamp_b10}) # Time based build number - Base 16 - More compact, but might be interpreted as a non-digit |
| 78 | + build_timestamp_bx16=$(printf '0x%x' ${build_timestamp_b10}) # As above, with leading '0x' so it's never not seen as hex (but is less compact) |
| 79 | + # Python packaging doesn't like the "x" in the build number build numbers (even though it makes it less ambigious) |
| 80 | + # Reverting to base 10 for now. *sigh* |
| 81 | + version_buildnum=${build_timestamp_b10} |
| 82 | +
|
| 83 | + # Putting it all together |
| 84 | + printf "${version_invariant}.${version_buildnum}${version_variant:+-}${version_variant}" > version-with-buildnum.txt |
| 85 | + echo "Created version-with-buildnum.txt : $(cat version-with-buildnum.txt)" |
| 86 | + echo "version-with-buildnum=$(cat version-with-buildnum.txt)" | tee -a $GITHUB_OUTPUT |
| 87 | + echo "version=$(cat version.txt)" | tee -a $GITHUB_OUTPUT |
| 88 | + echo "buildnum=${version_buildnum}" | tee -a $GITHUB_OUTPUT |
| 89 | + echo "variant-normal=${version_variant}" | tee -a $GITHUB_OUTPUT |
| 90 | + echo "variant-raw=${{ inputs.build-variant }}" | tee -a $GITHUB_OUTPUT |
| 91 | + - name: Save Artifacts - version-with-buildnum.txt |
| 92 | + uses: actions/upload-artifact@v4 |
| 93 | + with: |
| 94 | + name: version-with-buildnum-file |
| 95 | + path: version-with-buildnum.txt |
0 commit comments