|
| 1 | +name: Publish Docker image |
| 2 | + |
| 3 | +on: [push] |
| 4 | + |
| 5 | +permissions: |
| 6 | + contents: write |
| 7 | + |
| 8 | +env: |
| 9 | + DOCKER_ID: ${{ secrets.DOCKER_ID }} |
| 10 | + IMAGE_NAME: ${{ secrets.IMAGE_NAME }} |
| 11 | + DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }} |
| 12 | + DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }} |
| 13 | + |
| 14 | +jobs: |
| 15 | + shellcheck: |
| 16 | + name: Shellcheck |
| 17 | + runs-on: ubuntu-latest |
| 18 | + steps: |
| 19 | + - uses: actions/checkout@v4 |
| 20 | + name: Checkout repository |
| 21 | + |
| 22 | + - name: Copy .env.example to .env |
| 23 | + run: cp .env.example .env |
| 24 | + |
| 25 | + - name: Run ShellCheck |
| 26 | + uses: ludeeus/action-shellcheck@master |
| 27 | + with: |
| 28 | + check_together: 'yes' |
| 29 | + ignore_paths: >- |
| 30 | + sources |
| 31 | +
|
| 32 | + push_to_registry: |
| 33 | + if: github.event_name != 'pull_request' |
| 34 | + name: Build and push Docker image |
| 35 | + needs: shellcheck |
| 36 | + runs-on: ubuntu-latest |
| 37 | + steps: |
| 38 | + - name: Checkout repository |
| 39 | + uses: actions/checkout@v4 |
| 40 | + |
| 41 | + - name: Setup Docker Buildx |
| 42 | + uses: docker/setup-buildx-action@v3 |
| 43 | + |
| 44 | + - name: Log into Docker Hub |
| 45 | + uses: docker/login-action@v3 |
| 46 | + with: |
| 47 | + username: ${{ env.DOCKER_USERNAME }} |
| 48 | + password: ${{ env.DOCKER_PASSWORD }} |
| 49 | + |
| 50 | + - name: Extract Docker metadata |
| 51 | + id: meta |
| 52 | + uses: docker/metadata-action@v5 |
| 53 | + with: |
| 54 | + images: ${{ env.DOCKER_ID }}/${{ env.IMAGE_NAME }} |
| 55 | + tags: | |
| 56 | + type=ref,event=branch |
| 57 | + type=ref,event=tag |
| 58 | + type=semver,pattern={{version}} |
| 59 | + type=raw,value=latest,enable=${{ github.ref == 'refs/heads/main' }} |
| 60 | +
|
| 61 | + - name: Get version from Git tags |
| 62 | + id: get_version |
| 63 | + run: | |
| 64 | + version=$(git describe --tags --always --abbrev=0) |
| 65 | + echo ::set-output name=version::$version |
| 66 | +
|
| 67 | + - name: Increment version number |
| 68 | + id: inc_version |
| 69 | + run: | |
| 70 | + version=${{ steps.get_version.outputs.version }} |
| 71 | + version=${version#v} |
| 72 | + if [ -z "$version" ]; then |
| 73 | + major=0 |
| 74 | + minor=0 |
| 75 | + patch=0 |
| 76 | + else |
| 77 | + IFS='.' read -r -a parts <<< "$version" |
| 78 | + major=${parts[0]:-0} |
| 79 | + minor=${parts[1]:-0} |
| 80 | + patch=${parts[2]:-0} |
| 81 | + fi |
| 82 | + patch=$((patch+1)) |
| 83 | + if [ "$patch" -ge 10 ]; then |
| 84 | + patch=0 |
| 85 | + minor=$((minor+1)) |
| 86 | + fi |
| 87 | + if [ "$minor" -ge 10 ]; then |
| 88 | + minor=0 |
| 89 | + major=$((major+1)) |
| 90 | + fi |
| 91 | + new_version="v$major.$minor.$patch" |
| 92 | + echo "New version: $new_version" |
| 93 | + echo ::set-output name=new_version::$new_version |
| 94 | +
|
| 95 | + - name: Set new version tag |
| 96 | + run: | |
| 97 | + git tag ${{ steps.inc_version.outputs.new_version }} |
| 98 | + git push origin ${{ steps.inc_version.outputs.new_version }} |
| 99 | +
|
| 100 | + - name: Build and push Docker image |
| 101 | + id: build-and-push |
| 102 | + uses: docker/build-push-action@v6 |
| 103 | + with: |
| 104 | + context: php |
| 105 | + push: true |
| 106 | + tags: | |
| 107 | + ${{ steps.meta.outputs.tags }} |
| 108 | + ${{ steps.inc_version.outputs.new_version }} |
| 109 | + labels: ${{ steps.meta.outputs.labels }} |
| 110 | + build-args: | |
| 111 | + PHP_VERSION=8.3 |
| 112 | + USER_ID=1000 |
| 113 | + GROUP_ID=1000 |
0 commit comments