|
| 1 | +name: "Docker Push" |
| 2 | + |
| 3 | +on: |
| 4 | + release: |
| 5 | + types: [published] |
| 6 | + schedule: |
| 7 | + - cron: '30 12 * * *' |
| 8 | + |
| 9 | +permissions: |
| 10 | + packages: write |
| 11 | + contents: read |
| 12 | + |
| 13 | +env: |
| 14 | + IMAGE_NAME: ${{ github.repository }} |
| 15 | + |
| 16 | +jobs: |
| 17 | + Docker: |
| 18 | + name: Docker Build & Publish |
| 19 | + runs-on: ubuntu-latest |
| 20 | + |
| 21 | + steps: |
| 22 | + - name: Checkout |
| 23 | + uses: actions/checkout@v3 |
| 24 | + with: |
| 25 | + fetch-depth: 0 |
| 26 | + |
| 27 | + - name: Login to DockerHub |
| 28 | + uses: docker/login-action@v2 |
| 29 | + with: |
| 30 | + username: ${{ secrets.DOCKERHUB_USERNAME }} |
| 31 | + password: ${{ secrets.DOCKERHUB_TOKEN }} |
| 32 | + |
| 33 | + - name: Login to GitHub Container Registry |
| 34 | + uses: docker/login-action@v2 |
| 35 | + with: |
| 36 | + registry: ghcr.io |
| 37 | + username: ${{ github.actor }} |
| 38 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 39 | + |
| 40 | + - name: Checkout the latest tag |
| 41 | + run: | |
| 42 | + rev=$(git rev-list --tags --max-count=1) |
| 43 | + tag=$(git describe --tags "$rev") |
| 44 | +
|
| 45 | + if [[ -z "$tag" ]] ; then |
| 46 | + echo "::error::Can't find the latest tag" |
| 47 | + exit 1 |
| 48 | + fi |
| 49 | +
|
| 50 | + echo -e "\033[34mRev:\033[0m $rev" |
| 51 | + echo -e "\033[34mTag:\033[0m $tag" |
| 52 | +
|
| 53 | + git checkout "$tag" |
| 54 | +
|
| 55 | + - name: Prepare metadata for build |
| 56 | + id: metadata |
| 57 | + run: | |
| 58 | + rev=$(git rev-list --tags --max-count=1) |
| 59 | + version=$(git describe --tags "$rev" | tr -d 'v') |
| 60 | +
|
| 61 | + if [[ -z "$version" ]] ; then |
| 62 | + echo "::error::Can't find version info" |
| 63 | + exit 1 |
| 64 | + fi |
| 65 | +
|
| 66 | + docker_file="Dockerfile" |
| 67 | + base_image=$(grep 'FROM ' $docker_file | tail -1 | cut -f2 -d' ') |
| 68 | +
|
| 69 | + if [[ -z "$base_image" ]] ; then |
| 70 | + echo "::error::Can't extract base image info" |
| 71 | + exit 1 |
| 72 | + fi |
| 73 | +
|
| 74 | + echo "::set-output name=version::$version" |
| 75 | + echo "::set-output name=dockerfile::$docker_file" |
| 76 | + echo "::set-output name=baseimage::$base_image" |
| 77 | +
|
| 78 | + echo -e "\033[34mVersion:\033[0m $version" |
| 79 | + echo -e "\033[34mDockerfile:\033[0m $docker_file" |
| 80 | + echo -e "\033[34mBase image:\033[0m $base_image" |
| 81 | +
|
| 82 | + - name: Check if build/rebuild is required |
| 83 | + id: build_check |
| 84 | + run: | |
| 85 | + if [[ "${{github.event_name}}" == "release" ]] ; then |
| 86 | + echo "::set-output name=build::true" |
| 87 | + exit 0 |
| 88 | + fi |
| 89 | +
|
| 90 | + echo -e "::group::\033[34mDownloading built image…\033[0m" |
| 91 | +
|
| 92 | + if ! docker pull ghcr.io/${{env.IMAGE_NAME}}:latest ; then |
| 93 | + echo "::error::Can't download image ghcr.io/${{env.IMAGE_NAME}}:latest" |
| 94 | + exit 1 |
| 95 | + fi |
| 96 | +
|
| 97 | + echo "::endgroup::" |
| 98 | + echo -e "::group::\033[34mDownloading base image…\033[0m" |
| 99 | +
|
| 100 | + if ! docker pull ${{steps.metadata.outputs.baseimage}} ; then |
| 101 | + echo "::error::Can't download image ${{steps.metadata.outputs.baseimage}}" |
| 102 | + exit 1 |
| 103 | + fi |
| 104 | +
|
| 105 | + echo "::endgroup::" |
| 106 | +
|
| 107 | + base_layer=$(docker inspect "${{steps.metadata.outputs.baseimage}}" | jq -r '.[0].RootFS.Layers[-1]') |
| 108 | +
|
| 109 | + if [[ -z "$base_layer" ]] ; then |
| 110 | + echo "::error::Can't extract layers info from base image" |
| 111 | + exit 1 |
| 112 | + fi |
| 113 | +
|
| 114 | + if ! docker inspect "ghcr.io/${{env.IMAGE_NAME}}:latest" | jq -r '.[0].RootFS.Layers' | grep -q "$base_layer" ; then |
| 115 | + echo "::warning::Rebuild image (reason: base image rebuilt)" |
| 116 | + echo "::set-output name=build::true" |
| 117 | + exit 0 |
| 118 | + fi |
| 119 | +
|
| 120 | + - name: Build and push Docker image |
| 121 | + if: ${{ steps.build_check.outputs.build == 'true' }} |
| 122 | + uses: docker/build-push-action@v3 |
| 123 | + with: |
| 124 | + push: true |
| 125 | + context: . |
| 126 | + file: ${{steps.metadata.outputs.dockerfile}} |
| 127 | + tags: | |
| 128 | + ghcr.io/${{env.IMAGE_NAME}}:${{steps.metadata.outputs.version}} |
| 129 | + ghcr.io/${{env.IMAGE_NAME}}:latest |
| 130 | + ${{env.IMAGE_NAME}}:${{steps.metadata.outputs.version}} |
| 131 | + ${{env.IMAGE_NAME}}:latest |
| 132 | +
|
| 133 | + - name: Show info about built Docker image |
| 134 | + uses: essentialkaos/docker-info-action@v1 |
| 135 | + with: |
| 136 | + image: ${{env.IMAGE_NAME}}:latest |
| 137 | + show-labels: true |
0 commit comments