|
| 1 | +name: Release to Github Packages. # https://ghcr.io |
| 2 | +on: # https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#on |
| 3 | + workflow_dispatch: |
| 4 | + push: |
| 5 | + tags: # This builds for all branches with semantically versioned tags (v0.12.3). |
| 6 | + - v* # https://semver.org will fail, if there are any other tags |
| 7 | + |
| 8 | + #release: # Builds only releases. Includes draft and pre-releases. |
| 9 | + #types: [created] |
| 10 | + |
| 11 | + #pull_request: # Run 'tests' for any PRs. Default is to not run for first-time contributors: see /settings/actions |
| 12 | + |
| 13 | +env: |
| 14 | + TAG_LATEST: true # Encourage users to use a major version (foobar:1) instead of :latest. |
| 15 | + # By semantic versioning standards, major changes are changes 'backwards incompatible'. Major upgrades are often rare and prehaps, need attention from the user. |
| 16 | +jobs: |
| 17 | + # Push image to GitHub Packages. |
| 18 | + push: |
| 19 | + runs-on: ubuntu-latest |
| 20 | + permissions: |
| 21 | + packages: write |
| 22 | + contents: read |
| 23 | + |
| 24 | + steps: |
| 25 | + - uses: actions/checkout@v2 |
| 26 | + |
| 27 | + - name: Build image |
| 28 | + run: docker build . --file Dockerfile --tag ${{ github.event.repository.name }} --label "runnumber=${GITHUB_RUN_ID}" |
| 29 | + |
| 30 | + - name: Authenticate with ghcr.io |
| 31 | + run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin |
| 32 | + |
| 33 | + - name: Push image |
| 34 | + run: | |
| 35 | + # Destiination, trsnform to lowercase |
| 36 | + IMAGE_ID=$(echo ghcr.io/${{ github.repository }} | tr '[A-Z]' '[a-z]') |
| 37 | + function tag_push() { |
| 38 | + docker tag ${{ github.event.repository.name }} $IMAGE_ID:$1 |
| 39 | + docker push $IMAGE_ID:$1 |
| 40 | + } |
| 41 | + # Strip git ref prefix from version |
| 42 | + VERSION=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,') |
| 43 | + # trigger is (hopefully semantically) tagged |
| 44 | + if [[ "${{ github.ref }}" == "refs/tags/"* ]]; then |
| 45 | + # Strip +buildinfo |
| 46 | + VERSION=$(cut -d+ -f1 <<< $VERSION) |
| 47 | + # Strip "v" prefix from tag name (v1.2.3 to 1.2.3) |
| 48 | + VERSION=$(sed -e 's/^v//' <<< $VERSION) |
| 49 | + |
| 50 | + if [[ -z $(cut -sd- -f1 <<< $VERSION) ]]; then # Not a prerelease (not v0.1.2-rc4) |
| 51 | + |
| 52 | + [[ ${TAG_LATEST} == "true" ]] && tag_push latest |
| 53 | + |
| 54 | + tag_push $VERSION # push patch (:1.2.3) |
| 55 | + |
| 56 | + # push minor version (:1.2) |
| 57 | + VERSION=$(cut -d. -f -2 <<< $VERSION) |
| 58 | + tag_push $VERSION |
| 59 | + |
| 60 | + # major version (:1) |
| 61 | + VERSION=$(cut -d. -f -1 <<< $VERSION) |
| 62 | + fi |
| 63 | + fi |
| 64 | + |
| 65 | + # push normally (and possibly major) |
| 66 | + tag_push $VERSION |
| 67 | + # Can't push multiple tags at once: https://github.com/docker/cli/issues/267 |
0 commit comments