diff --git a/.github/workflows/template-build.yaml b/.github/workflows/template-build.yaml index 948c282..825e163 100644 --- a/.github/workflows/template-build.yaml +++ b/.github/workflows/template-build.yaml @@ -77,16 +77,44 @@ jobs: id: branch run: echo "branch=${GITHUB_REF_NAME}" >> $GITHUB_OUTPUT - - name: Build, tag, and push image to Amazon ECR - id: build + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Check if Build Cache exists + id: build-cache env: ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} ECR_REPOSITORY: hb-${{ inputs.service }} - IMAGE_TAG: ${{ steps.sha.outputs.sha_short }} run: | - echo "building and pushing to: $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" - docker buildx build --ssh default=${{ env.SSH_AUTH_SOCK }} -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG --build-arg githubUsername=hbh-github --build-arg githubToken=${{ secrets.GH_TOKEN }} . - docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG + docker pull $ECR_REGISTRY/$ECR_REPOSITORY:buildcache + if [ $? -eq 0 ]; then + echo "Cache found" + else + echo "Cache not found, building and pushing now" + docker buildx build --ssh default=${{ env.SSH_AUTH_SOCK }} -t $ECR_REGISTRY/$ECR_REPOSITORY:buildcache --build-arg githubUsername=hbh-github --build-arg githubToken=${{ secrets.GH_TOKEN }} . + docker push $ECR_REGISTRY/$ECR_REPOSITORY:buildcache + fi + + - name: Build and push + uses: docker/build-push-action@v6 + with: + push: true + tags: ${{ steps.login-ecr.outputs.registry }}/hb-${{ inputs.service }}:${{ steps.sha.outputs.sha_short }} + ssh: [default=${{ env.SSH_AUTH_SOCK }}] + build-args: [githubUsername=hbh-github, githubToken=${{ secrets.GH_TOKEN }}] + cache-from: type=registry,ref=${{ steps.login-ecr.outputs.registry }}/hb-${{ inputs.service }}:buildcache + cache-to: type=registry,ref=${{ steps.login-ecr.outputs.registry }}/hb-${{ inputs.service }}:buildcache,mode=max + + # - name: Build, tag, and push image to Amazon ECR + # id: build + # env: + # ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} + # ECR_REPOSITORY: hb-${{ inputs.service }} + # IMAGE_TAG: ${{ steps.sha.outputs.sha_short }} + # run: | + # echo "building and pushing to: $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" + # docker buildx build --ssh default=${{ env.SSH_AUTH_SOCK }} -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG --build-arg githubUsername=hbh-github --build-arg githubToken=${{ secrets.GH_TOKEN }} . + # docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG - name: Check if branch is up to date id: checkCurrent