|
| 1 | +name: Docker |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + # Publish `main` as Docker `latest` image. |
| 6 | + branches: |
| 7 | + - main |
| 8 | + |
| 9 | + # Publish `v1.2.3` tags as releases. |
| 10 | + tags: |
| 11 | + - v* |
| 12 | + |
| 13 | + # Run tests for any PRs. |
| 14 | + pull_request: |
| 15 | + |
| 16 | +env: |
| 17 | + IMAGE_NAME: SPRINGBOOTDEMO |
| 18 | + |
| 19 | +jobs: |
| 20 | + |
| 21 | + test: |
| 22 | + runs-on: ubuntu-latest |
| 23 | + |
| 24 | + steps: |
| 25 | + - uses: actions/checkout@v2 |
| 26 | + |
| 27 | + - name: Run tests |
| 28 | + run: | |
| 29 | + docker image build . --file Dockerfile |
| 30 | +
|
| 31 | + push: |
| 32 | + needs: test |
| 33 | + |
| 34 | + runs-on: ubuntu-latest |
| 35 | + |
| 36 | + steps: |
| 37 | + - uses: actions/checkout@v2 |
| 38 | + |
| 39 | + - name: Build image |
| 40 | + run: docker image build --pull --no-cache --squash . --file Dockerfile --tag $IMAGE_NAME |
| 41 | + |
| 42 | + - name: Log into registry |
| 43 | + run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin |
| 44 | + |
| 45 | + - name: Push image |
| 46 | + run: | |
| 47 | + IMAGE_ID=ghcr.io/${{ github.repository }}/$IMAGE_NAME |
| 48 | +
|
| 49 | + # Change all uppercase to lowercase |
| 50 | + IMAGE_ID=$(echo $IMAGE_ID | tr '[A-Z]' '[a-z]') |
| 51 | +
|
| 52 | + # Strip git ref prefix from version |
| 53 | + VERSION=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,') |
| 54 | +
|
| 55 | + # Strip "v" prefix from tag name |
| 56 | + [[ "${{ github.ref }}" == "refs/tags/"* ]] && VERSION=$(echo $VERSION | sed -e 's/^v//') |
| 57 | +
|
| 58 | + # Use Docker `latest` tag convention |
| 59 | + [ "$VERSION" == "main" ] && VERSION=latest |
| 60 | +
|
| 61 | + SEMREG='[^0-9]*\([0-9]*\)[.]\([0-9]*\)[.]\([0-9]*\)\([0-9A-Za-z-]*\)' |
| 62 | + SEM=`echo $VERSION | sed -e "s#^v##"` |
| 63 | + TAGS=$SEM |
| 64 | + MAJOR=`echo $SEM | sed -e "s#$SEMREG#\1#"` |
| 65 | + MINOR=`echo $SEM | sed -e "s#$SEMREG#\2#"` |
| 66 | + PATCH=`echo $SEM | sed -e "s#$SEMREG#\3#"` |
| 67 | + SPECIAL=`echo $SEM | sed -e "s#$SEMREG#\4#"` |
| 68 | + # add semantic tags |
| 69 | + if [ "$MAJOR" != "$SEM" ] && [ -z "$SPECIAL" ]; then |
| 70 | + TAGS="$SEM $MAJOR.$MINOR $MAJOR latest" |
| 71 | + if [ -n "$SPECIAL" ]; then |
| 72 | + TAGS="$MAJOR.$MINOR.$PATCH $TAGS" |
| 73 | + fi |
| 74 | + fi |
| 75 | +
|
| 76 | + for tag in $TAGS; do |
| 77 | + echo -e "\nTagging and pushing: ${IMAGE_ID}:${tag}" |
| 78 | + docker image tag $IMAGE_NAME $IMAGE_ID:$tag |
| 79 | + done |
| 80 | + |
| 81 | + docker image push --all-tags $IMAGE_ID |
| 82 | + |
0 commit comments