diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml new file mode 100644 index 0000000..455bf1d --- /dev/null +++ b/.github/workflows/build.yaml @@ -0,0 +1,66 @@ +name: Build and push OCI image to Docker Hub + +on: + push: + tags: + - 'v*' + +jobs: + check-current-branch: + runs-on: ubuntu-latest + outputs: + branch: ${{ steps.check_step.outputs.branch }} + steps: + - name: Checkout + uses: actions/checkout@v3 + with: + fetch-depth: 0 + + - name: Get current branch + id: check_step + # 1. Get the list of branches ref where this tag exists + # 2. Remove 'origin/' from that result + # 3. Put that string in output + run: | + raw=$(git branch -r --contains ${{ github.ref }}) + branch="$(echo ${raw//origin\//} | tr -d '\n')" + echo "{name}=branch" >> $GITHUB_OUTPUT + echo "Branches where this tag exists : $branch." + + image: + runs-on: ubuntu-latest + needs: check-current-branch + if: contains(${{ needs.check.outputs.branch }}, 'main')` + steps: + - name: Check out the repo + uses: actions/checkout@v4 + + - name: Get build args + id: build_args + run: | + echo "APP_COMMIT=$(git rev-parse --short HEAD)" >> "$GITHUB_OUTPUT" + echo "APP_VERSION=$(git describe --tags --always --abbrev=0 --match='v[0-9]*.[0-9]*.[0-9]*' 2> /dev/null | sed 's/^.//')" >> "$GITHUB_OUTPUT" + + - name: Log in to Docker Hub + uses: docker/login-action@v3 + with: + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_PASSWORD }} + + - name: Extract metadata (tags, labels) + id: meta + uses: docker/metadata-action@v5 + with: + images: bsvb/uhrp-storage-server + + - name: Build and push image + uses: docker/build-push-action@v5 + with: + context: . + file: ./Dockerfile + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + build-args: | + APP_COMMIT=${{ steps.build_args.outputs.APP_COMMIT }} + APP_VERSION=${{ steps.build_args.outputs.APP_VERSION }}