Skip to content

Commit

Permalink
Refactor Docker image build and push logic in create_packages.yml
Browse files Browse the repository at this point in the history
- Updated the workflow to only build images on the production branch, while tags now retag existing production images without rebuilding.
- Enhanced the push logic to handle tagging scenarios, ensuring that production images are pulled and retagged appropriately.
- Streamlined conditions for building and pushing images based on branch context, improving the efficiency of the CI/CD process.

These changes optimize the Docker image management, ensuring that only necessary images are built and pushed based on the current branch and tag context.
  • Loading branch information
jhagberg committed Jan 3, 2025
1 parent 6c47a02 commit ab56353
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions .github/workflows/create_packages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,11 @@ jobs:
DOCKER_BUILDKIT: 1
COMPOSE_DOCKER_CLI_BUILD: 1
run: |
# For tags and production branch, build frontend and main
if [[ "${{ github.ref_type }}" == "tag" || "${{ github.ref_name }}" == "production" ]]; then
# Only build on production branch, tags will just retag existing images
if [[ "${{ github.ref_name }}" == "production" ]]; then
docker compose build --build-arg BUILDKIT_INLINE_CACHE=1 herdbook-frontend main
else
# For other branches, only build changed images and their dependents
elif [[ "${{ github.ref_type }}" != "tag" ]]; then
# For other branches (except tags), only build changed images and their dependents
if [[ "${{ needs.changes.outputs.frontend }}" == "true" ]]; then
docker compose build --build-arg BUILDKIT_INLINE_CACHE=1 herdbook-frontend main
elif [[ "${{ needs.changes.outputs.main }}" == "true" ]]; then
Expand All @@ -110,9 +110,17 @@ jobs:
branch="${{ github.ref_name }}"
safe_branch="${branch//\//-}" # Replace / with -
# Function to push an image if it was built
# Function to push an image if it was built or if we're tagging
push_if_built() {
local image="$1"
local is_tag="${2:-false}"
# For tags, try to pull the production image first
if [[ "$is_tag" == "true" ]]; then
docker pull "ghcr.io/nbisweden/$image:production" || return 1
docker tag "ghcr.io/nbisweden/$image:production" "$image:latest"
fi
if docker image inspect "$image:latest" >/dev/null 2>&1; then
docker tag "$image:latest" "ghcr.io/nbisweden/$image:$safe_branch"
docker push "ghcr.io/nbisweden/$image:$safe_branch"
Expand All @@ -124,8 +132,11 @@ jobs:
fi
}
# For tags and production branch, push frontend and main
if [[ "${{ github.ref_type }}" == "tag" || "${{ github.ref_name }}" == "production" ]]; then
# For tags, retag production images. For production, push built images
if [[ "${{ github.ref_type }}" == "tag" ]]; then
push_if_built "herdbook_frontend" true
push_if_built "herdbook_main" true
elif [[ "${{ github.ref_name }}" == "production" ]]; then
push_if_built "herdbook_frontend"
push_if_built "herdbook_main"
else
Expand Down

0 comments on commit ab56353

Please sign in to comment.