|
| 1 | +name: "Prepare builder" |
| 2 | +description: "Prepares build environment using Podman with GHCR caching" |
| 3 | +author: "embtom" |
| 4 | + |
| 5 | +inputs: |
| 6 | + service: |
| 7 | + description: "The service in docker-compose.yml" |
| 8 | + required: true |
| 9 | + |
| 10 | +runs: |
| 11 | + using: "composite" |
| 12 | + steps: |
| 13 | + - name: Checkout repository |
| 14 | + uses: actions/checkout@v4 |
| 15 | + |
| 16 | + - name: Install Podman and podman-compose |
| 17 | + uses: ./.github/actions/setup-podman |
| 18 | + |
| 19 | + - name: Login to GHCR |
| 20 | + shell: bash |
| 21 | + run: | |
| 22 | + echo "${{ github.token }}" | podman login ghcr.io -u "${{ github.actor }}" --password-stdin |
| 23 | +
|
| 24 | + - name: Read image name from compose |
| 25 | + id: image-name |
| 26 | + shell: bash |
| 27 | + run: | |
| 28 | + IMAGE=$(yq -r '.services."${{ inputs.service }}".image' docker-compose.yml) |
| 29 | + BASE_IMAGE="${IMAGE%%:*}" |
| 30 | + HASH=$(sha256sum Dockerfile | cut -d " " -f1) |
| 31 | + echo "image=${IMAGE}" >> $GITHUB_OUTPUT |
| 32 | +
|
| 33 | + CACHE_IMAGE="ghcr.io/${{ github.repository_owner }}/cache-${BASE_IMAGE}:${HASH}" |
| 34 | + |
| 35 | + echo "hash=${HASH}" >> $GITHUB_OUTPUT |
| 36 | + echo "cache-image=${CACHE_IMAGE}" >> $GITHUB_OUTPUT |
| 37 | +
|
| 38 | + - name: Try pulling cache from GHCR |
| 39 | + id: pull-cache |
| 40 | + shell: bash |
| 41 | + run: | |
| 42 | + echo "Trying to pull cache image: ${{ steps.image-name.outputs.cache-image }}" |
| 43 | + if podman pull "${{ steps.image-name.outputs.cache-image }}"; then |
| 44 | + echo "cache-hit=true" >> $GITHUB_OUTPUT |
| 45 | + else |
| 46 | + echo "cache-hit=false" >> $GITHUB_OUTPUT |
| 47 | + fi |
| 48 | +
|
| 49 | + - name: Load cached image tag into local tag |
| 50 | + if: steps.pull-cache.outputs.cache-hit == 'true' |
| 51 | + shell: bash |
| 52 | + run: | |
| 53 | + podman tag "${{ steps.image-name.outputs.cache-image }}" \ |
| 54 | + "${{ steps.image-name.outputs.image }}" |
| 55 | +
|
| 56 | + echo "Loaded cached image: ${{ steps.image-name.outputs.image }}" |
| 57 | +
|
| 58 | + - name: Build image if cache miss |
| 59 | + if: steps.pull-cache.outputs.cache-hit == 'false' |
| 60 | + shell: bash |
| 61 | + run: | |
| 62 | + echo "Cache miss → building image…" |
| 63 | + podman-compose -f docker-compose.yml build "${{ inputs.service }}" |
| 64 | +
|
| 65 | + - name: Push updated image to GHCR as cache |
| 66 | + shell: bash |
| 67 | + run: | |
| 68 | + podman tag "${{ steps.image-name.outputs.image }}" \ |
| 69 | + "${{ steps.image-name.outputs.cache-image }}" |
| 70 | +
|
| 71 | + podman push "${{ steps.image-name.outputs.cache-image }}" |
0 commit comments