Skip to content

Commit

Permalink
feat: add option to not build image for test_compose
Browse files Browse the repository at this point in the history
  • Loading branch information
spwoodcock committed Jul 26, 2024
1 parent b2d2a40 commit 610f2e7
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions .github/workflows/test_compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ on:
description: "The image root name, without tag. E.g. 'ghcr.io/[dollar]{{ github.repository }}'"
required: true
type: string
build_img:
description: "If the image must be built first, or false to just pull."
required: false
type: boolean
default: true
build_context:
description: "Root directory to start the build from."
required: false
Expand Down Expand Up @@ -82,9 +87,10 @@ permissions:
packages: write

jobs:
# Get cached images from previous runs (when running multiple times)
check-img-cache:
runs-on: ubuntu-latest
if: ${{ inputs.cache_extra_imgs || inputs.cache_image }}
if: ${{ inputs.build_img && inputs.cache_image }}
environment:
name: ${{ inputs.environment }}
outputs:
Expand All @@ -96,10 +102,12 @@ jobs:
path: /tmp/images
key: image-cache-${{ runner.os }}

# Build the test image if required (unless `inputs.build_img: false`)
test-img-build:
uses: hotosm/gh-workflows/.github/workflows/[email protected]
needs: [check-img-cache]
if: ${{ needs.check-img-cache.outputs.cache-hit != 'true' }}
# Only build if no cached image is found
if: ${{ inputs.build_img && needs.check-img-cache.outputs.cache-hit != 'true' }}
with:
context: ${{ inputs.build_context }}
dockerfile: ${{ inputs.build_dockerfile }}
Expand Down Expand Up @@ -132,6 +140,7 @@ jobs:
path: /tmp/images
key: image-cache-${{ runner.os }}

# Cache the newly built image, plus extra images used in the compose file
- name: Save Local Images
if: ${{ (inputs.cache_extra_imgs || inputs.cache_image) && steps.image-cache.outputs.cache-hit != 'true' }}
run: |
Expand Down Expand Up @@ -177,6 +186,7 @@ jobs:
done
fi
# Load the cached image .tar files via `docker image load`
- name: Load Cached Imgs
if: ${{ (inputs.cache_extra_imgs || inputs.cache_image) && steps.image-cache.outputs.cache-hit == 'true' }}
run: |
Expand All @@ -199,6 +209,8 @@ jobs:
echo "TAG_OVERRIDE=${image_tag}" >> $GITHUB_ENV
fi
# Extract repo vars/secrets into environment variables for the next step
# NOTE it may look like secrets are exposed, but they are hidden in the GUI
- name: Vars and Secrets to Env
env:
TAG_OVERRIDE: ${{ env.TAG_OVERRIDE || 'ci-development' }}
Expand Down Expand Up @@ -226,6 +238,8 @@ jobs:
echo "${SECRETS_CONTEXT}" | to_envs >> $GITHUB_ENV
fi
# From generated environment variables, create a `.env` file in repo root
# If a .env.example exists, the variables are substituted in from Github env
- name: Create .env file
env:
EXAMPLE_DOTENV: ${{ inputs.example_env_file_path }}
Expand All @@ -250,10 +264,12 @@ jobs:
echo "GIT_BRANCH=${GIT_BRANCH}" >> .env
echo "TAG_OVERRIDE=${TAG_OVERRIDE}" >> .env
# Run the tests via a pre-configured docker compose service
- name: Run Tests
if: ${{ ! inputs.coverage }}
run: |
${{ inputs.pre_command }}
docker compose --file ${{ inputs.compose_file }} \
run --no-TTY \
${{ inputs.compose_service }} ${{ inputs.compose_command }}
Expand All @@ -262,6 +278,7 @@ jobs:
if: ${{ inputs.coverage }}
run: |
${{ inputs.pre_command }}
docker compose --file ${{ inputs.compose_file }} \
run --no-TTY --entrypoint "sh -c" \
--volume ${{ github.workspace }}/coverage:/tmp/coverage \
Expand Down

0 comments on commit 610f2e7

Please sign in to comment.