Skip to content
Snippets Groups Projects
Commit cee48555 authored by (INACTIVE) Marcel Bajdel (new marcel.bajdel)'s avatar (INACTIVE) Marcel Bajdel (new marcel.bajdel) :rocket:
Browse files

New build

parent 2ceb9c19
No related branches found
No related tags found
3 merge requests!20Release,!13Docker image branch,!12Docker image branch
stages:
# List of stages for jobs, and their order of execution
- check_image # Check whether the image is already existing or not
- check_image_branch # Check whether the image is already existing or not for the branch
- initial_build # builds if the image does not exist
- build_main # builds when the merge request is made into main
- check_image_tag # Check whether the image is already existing or not for the tag
- build_tag # builds when the tag is pushed
- test_main # tests when the merge request is made into main
- test_latest # tests when the image with tag latest is pushed
- test_tag # tests when the tag is pushed
check_image:
stage: check_image
check_image_branch: # Check whether the image is already existing or not
stage: check_image_branch
image:
name: gcr.io/go-containerregistry/crane:debug
entrypoint: [""]
......@@ -20,8 +21,8 @@ check_image:
paths:
- docker_latest_image.txt
initial_build:
stage: initial_build
build_branch:
stage: build_branch
image:
name: gcr.io/kaniko-project/executor:debug
entrypoint: [""]
......@@ -30,14 +31,17 @@ initial_build:
- echo "{\"auths\":{\"$CI_REGISTRY\":{\"username\":\"$CI_REGISTRY_USER\",\"password\":\"$CI_REGISTRY_PASSWORD\"}}}" > /kaniko/.docker/config.json
# Read the value of DOCKER_LATEST_IMAGE_EXISTS from the artifact
- export DOCKER_LATEST_IMAGE_EXISTS=$(cat docker_latest_image.txt)
# if the image does not exist, build it
- if [ "$DOCKER_LATEST_IMAGE_EXISTS" == "false" ]; then /kaniko/executor --context $CI_PROJECT_DIR --dockerfile $CI_PROJECT_DIR/Dockerfile --destination $CI_REGISTRY_IMAGE:$CI_COMMIT_BRANCH; else echo "Docker image already exists"; fi
rules: # if the image is pushed into main branch, the build_main job will run
- if: $CI_COMMIT_BRANCH
# Check for changes (excluding README.md)
- CHANGED_FILES=$(git diff --name-only $CI_COMMIT_BEFORE_SHA $CI_COMMIT_SHA | grep -v README.md)
# if the image does not exist and there are changes (excluding README.md), build it
- if [ "$DOCKER_LATEST_IMAGE_EXISTS" == "false" ] && [ -n "$CHANGED_FILES" ]; then /kaniko/executor --context $CI_PROJECT_DIR --dockerfile $CI_PROJECT_DIR/Dockerfile --destination $CI_REGISTRY_IMAGE:$CI_COMMIT_BRANCH; else echo "Docker image already exists or no relevant changes"; fi
# Run tests (assuming you have a command for running tests)
- ./run_tests.sh # Replace this with your actual test command
rules:
- if: '$CI_COMMIT_BRANCH && $CI_MERGE_REQUEST_TARGET_BRANCH_NAME != "main"'
changes:
- "**/*"
- if: '$CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "main"'
when: never
build_main: # This build will run only for main branch
stage: build_main
......@@ -52,6 +56,18 @@ build_main: # This build will run only for main branch
rules:
- if: '$CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "main"'
check_image_tag: # Check whether the image is already existing or not for the tag
stage: check_image_tag
image:
name: gcr.io/go-containerregistry/crane:debug
entrypoint: [""]
script:
- crane auth login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
- if crane manifest $CI_REGISTRY_IMAGE:$CI_COMMIT_BRANCH; then echo "true" > docker_latest_image.txt; else echo "false" > docker_latest_image.txt; fi
artifacts:
paths:
- docker_latest_image.txt
build_tag: # This build will run only for tags
stage: build_tag
image:
......@@ -65,7 +81,7 @@ build_tag: # This build will run only for tags
# check if dockers images exists
- if crane manifest $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_NAME; then export DOCKER_TAG_IMAGE_EXISTS=true; else export DOCKER_TAG_IMAGE_EXISTS=false; fi
# if the image does not exist, build it with the added tag
- if $DOCKER_TAG_IMAGE_EXISTS=false; then /kaniko/executor --context $CI_PROJECT_DIR --dockerfile $CI_PROJECT_DIR/Dockerfile --destination $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_NAME; else echo "Docker image already exists"; fi
- if [ "$DOCKER_LATEST_IMAGE_EXISTS" == "false" ]; then /kaniko/executor --context $CI_PROJECT_DIR --dockerfile $CI_PROJECT_DIR/Dockerfile --destination $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_NAME; else echo "Docker image already exists"; fi
test_main:
stage: test_main
......@@ -90,7 +106,7 @@ test_latest: # This test will run only for tag latest
entrypoint: [""]
script:
# specify auths for crane executor
- if DOCKER_TAG_IMAGE_EXISTS=true; then echo "Docker image already exists"; else crane auth login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY; fi
- if [ "$DOCKER_LATEST_IMAGE_EXISTS" == "false" ]; then echo "Docker image already exists"; else crane auth login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY; fi
# validate the image with tag latest
- if ! crane validate --remote $CI_REGISTRY_IMAGE:$CI_COMMIT_BRANCH; then exit 1; fi > test_latest.log
artifacts:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment