|
| 1 | +name: "#️⃣ Get image tag (action)" |
| 2 | + |
| 3 | +description: This action gets the image tag from the commit ref or input (if provided) |
| 4 | + |
| 5 | +outputs: |
| 6 | + tag: |
| 7 | + description: The image tag |
| 8 | + value: ${{ steps.get_tag.outputs.tag }} |
| 9 | + is_semver: |
| 10 | + description: Whether the tag is a semantic version |
| 11 | + value: ${{ steps.check_semver.outputs.is_semver }} |
| 12 | + |
| 13 | +inputs: |
| 14 | + tag: |
| 15 | + description: The image tag. If this is set it will return the tag as is. |
| 16 | + required: false |
| 17 | + default: "" |
| 18 | + |
| 19 | +runs: |
| 20 | + using: "composite" |
| 21 | + steps: |
| 22 | + - name: "#️⃣ Get image tag (step)" |
| 23 | + id: get_tag |
| 24 | + shell: bash |
| 25 | + run: | |
| 26 | + if [[ -n "${{ inputs.tag }}" ]]; then |
| 27 | + tag="${{ inputs.tag }}" |
| 28 | + elif [[ "${{ github.ref_type }}" == "tag" ]]; then |
| 29 | + if [[ "${{ github.ref_name }}" == infra-*-* ]]; then |
| 30 | + env=$(echo ${{ github.ref_name }} | cut -d- -f2) |
| 31 | + sha=$(echo ${{ github.sha }} | head -c7) |
| 32 | + ts=$(date +%s) |
| 33 | + tag=${env}-${sha}-${ts} |
| 34 | + elif [[ "${{ github.ref_name }}" == v.docker.* ]]; then |
| 35 | + version="${GITHUB_REF_NAME#v.docker.}" |
| 36 | + tag="v${version}" |
| 37 | + elif [[ "${{ github.ref_name }}" == build-* ]]; then |
| 38 | + tag="${GITHUB_REF_NAME#build-}" |
| 39 | + else |
| 40 | + echo "Invalid git tag: ${{ github.ref_name }}" |
| 41 | + exit 1 |
| 42 | + fi |
| 43 | + elif [[ "${{ github.ref_name }}" == "main" ]]; then |
| 44 | + tag="main" |
| 45 | + else |
| 46 | + echo "Invalid git ref: ${{ github.ref }}" |
| 47 | + exit 1 |
| 48 | + fi |
| 49 | + echo "tag=${tag}" >> "$GITHUB_OUTPUT" |
| 50 | +
|
| 51 | + - name: 🔍 Check for validity |
| 52 | + id: check_validity |
| 53 | + shell: bash |
| 54 | + env: |
| 55 | + tag: ${{ steps.get_tag.outputs.tag }} |
| 56 | + run: | |
| 57 | + if [[ "${tag}" =~ ^[a-z0-9]+([._-][a-z0-9]+)*$ ]]; then |
| 58 | + echo "Tag is valid: ${tag}" |
| 59 | + else |
| 60 | + echo "Tag is not valid: ${tag}" |
| 61 | + exit 1 |
| 62 | + fi |
| 63 | +
|
| 64 | + - name: 🆚 Check for semver |
| 65 | + id: check_semver |
| 66 | + shell: bash |
| 67 | + env: |
| 68 | + tag: ${{ steps.get_tag.outputs.tag }} |
| 69 | + # Will match most semver formats except build metadata, i.e. v1.2.3+build.1 |
| 70 | + # Valid matches: |
| 71 | + # v1.2.3 |
| 72 | + # v1.2.3-alpha |
| 73 | + # v1.2.3-alpha.1 |
| 74 | + # v1.2.3-rc.1 |
| 75 | + # v1.2.3-beta-1 |
| 76 | + run: | |
| 77 | + if [[ "${tag}" =~ ^v[0-9]+\.[0-9]+\.[0-9]+(-[0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*)?$ ]]; then |
| 78 | + echo "Tag is a semantic version: ${tag}" |
| 79 | + is_semver=true |
| 80 | + else |
| 81 | + echo "Tag is not a semantic version: ${tag}" |
| 82 | + is_semver=false |
| 83 | + fi |
| 84 | + echo "is_semver=${is_semver}" >> "$GITHUB_OUTPUT" |
0 commit comments