Home > Software engineering >  Increment version number in GitHub Actions
Increment version number in GitHub Actions

Time:01-11

Not an expert on traditional Linux commands as well as grep. I have a pipeline setup that everytime I make a major release of my application, I want

  • Create a GitHub tag
  • Increment the version build in my pubspec.yaml

Creating the GitHub tag already works and I used the following command (I assume there is room for improvement too):

echo "TAG_NAME=$(cat ${GITHUB_WORKSPACE}/pubspec.yaml | grep 'version:' | head -1 | cut -f2- -d: | sed -e 's/^[ \t]*//')" >> $GITHUB_OUTPUT

For the second step however, I am pretty lost. I already considered search/replace the corresponding line in my pubspec.yaml, but I don't know how I could now apply such transformation on my version number.

For example:

  • 1.2.3 22 -> 1.2.4 23
  • 2.14.9 99 -> 2.14.10 100

Edit: The commands I have now working for me are:

- name: "Retrieve version"
        id: version
        run: |
          echo "OLD_VERSION=$(cat ${GITHUB_WORKSPACE}/pubspec.yaml | grep 'version:' | head -1 | cut -f2- -d: | sed -e 's/^[ \t]*//')" >> $GITHUB_OUTPUT
          echo "NEW_VERSION=$(awk '{ match($0,/([0-9] )\ ([0-9] )/,a); a[1]=a[1] 1; a[2]=a[2] 1; sub(/[0-9] \ [0-9] /,a[1]" "a[2])}1' ${GITHUB_WORKSPACE}/pubspec.yaml | grep 'version:' | head -1 | cut -f2- -d: | sed -e 's/^[ \t]*//')" >> $GITHUB_OUTPUT
      - name: "Increment version"
        run: |
          sed -i 's/${{ steps.version.outputs.OLD_VERSION }}/${{ steps.version.outputs.NEW_VERSION }}/g' ${GITHUB_WORKSPACE}/pubspec.yaml
          echo ${{ steps.version.outputs.OLD_VERSION }}
          echo ${{ steps.version.outputs.NEW_VERSION }}

Can probably be optimized a lot.

CodePudding user response:

If awk is an option, you can try;

$ awk '{ match($0,/([0-9] )\ ([0-9] )/,a); a[1]=a[1] 1; a[2]=a[2] 1; sub(/[0-9] \ [0-9] /,a[1]" "a[2])}1' input_file
1.2.4 23
2.14.10 100
  • Related