Skip to content

Commit

Permalink
Add build custom image step
Browse files Browse the repository at this point in the history
  • Loading branch information
antas-marcin committed May 29, 2023
1 parent 47cac3d commit adf2310
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 90 deletions.
10 changes: 7 additions & 3 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ jobs:
DOCKER_USERNAME: ${{secrets.DOCKER_USERNAME}}
DOCKER_PASSWORD: ${{secrets.DOCKER_PASSWORD}}
run: |
export GIT_TAG=${GITHUB_REF##*/}
cicd/docker_push.sh
build-custom-image:
name: Build custom image
Expand All @@ -104,11 +103,16 @@ jobs:
with:
username: ${{secrets.DOCKER_USERNAME}}
password: ${{secrets.DOCKER_PASSWORD}}
- name: Build custom image
env:
DOCKER_USERNAME: ${{secrets.DOCKER_USERNAME}}
DOCKER_PASSWORD: ${{secrets.DOCKER_PASSWORD}}
run: |
cicd/build_custom_base.sh
- name: Deploy
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags')
env:
DOCKER_USERNAME: ${{secrets.DOCKER_USERNAME}}
DOCKER_PASSWORD: ${{secrets.DOCKER_PASSWORD}}
run: |
export GIT_TAG=${GITHUB_REF##*/}
cicd/build_custom_base.sh
cicd/docker_push_custom_base.sh
66 changes: 1 addition & 65 deletions cicd/build_custom_base.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,68 +2,4 @@

set -eou pipefail

function build() {
docker build -f custom.Dockerfile -t "custom-base" .
}

git_hash=
pr=
remote_repo=${REMOTE_REPO?Variable REMOTE_REPO is required}
docker_username=${DOCKER_USERNAME?Variable DOCKER_USERNAME is required}
docker_password=${DOCKER_PASSWORD?Variable DOCKER_PASSWORD is required}

function main() {
init
echo "git branch is $GIT_BRANCH"
echo "git tag is $GIT_TAG"
echo "pr is $pr"
push_main
push_tag
}

function init() {
git_hash="$(git rev-parse HEAD | head -c 7)"
pr=false
if [ ! -z "$GIT_PULL_REQUEST" ]; then
pr="$GIT_PULL_REQUEST"
fi

docker run --rm --privileged multiarch/qemu-user-static --reset -p yes
docker buildx create --use

echo "$docker_password" | docker login -u "$docker_username" --password-stdin
}

# Note that some CI systems, such as travis, will not provide the branch, but
# the tag on a tag-push. So this method will not be called on a tag-run.
function push_main() {
if [ "$GIT_BRANCH" == "main" ] && [ "$pr" == "false" ]; then
# The ones that are always pushed

tag="$remote_repo:custom-$git_hash"
docker buildx build --platform=linux/arm64,linux/amd64 -f custom.Dockerfile \
--tag "$tag" \
--push \
.
fi
}

function push_tag() {
if [ ! -z "$GIT_TAG" ]; then
tag_git="$remote_repo:custom-$GIT_TAG"
tag_latest="$remote_repo:custom-latest"
tag="$remote_repo:custom"

echo "Tag & Push $tag, $tag_latest, $tag_git"
docker tag "custom-base" "$tag" && docker push "$tag"

docker buildx build --platform=linux/arm64,linux/amd64 -f custom.Dockerfile \
--tag "$tag" \
--tag "$tag_latest" \
--tag "$tag_git" \
--push \
.
fi
}

main "${@}"
docker build -f custom.Dockerfile -t "custom-base" .
28 changes: 6 additions & 22 deletions cicd/docker_push.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@
set -eou pipefail

# Docker push rules
# If not on main
# - nothing is pushed
# If on main and not PR
# - any commit is pushed as :<model>-<7-digit-hash>
# If on tag (e.g. 1.0.0)
# - any commit is pushed as :<model>-<semver>
# - any commit is pushed as :<model>-latest
Expand All @@ -18,13 +14,15 @@ model_name=${MODEL_NAME?Variable MODEL_NAME is required}
original_model_name=$model_name
docker_username=${DOCKER_USERNAME?Variable DOCKER_USERNAME is required}
docker_password=${DOCKER_PASSWORD?Variable DOCKER_PASSWORD is required}
git_tag=${GITHUB_REF##*/}

function main() {
init
echo "git ref type is $GITHUB_REF_TYPE"
echo "git ref name is $GITHUB_REF_NAME"
echo "git branch is $GIT_BRANCH"
echo "git tag is $GIT_TAG"
echo "git tag is $git_tag"
echo "pr is $pr"
push_main
push_tag
}

Expand All @@ -47,23 +45,9 @@ function init() {
echo "$docker_password" | docker login -u "$docker_username" --password-stdin
}

# Note that some CI systems, such as travis, will not provide the branch, but
# the tag on a tag-push. So this method will not be called on a tag-run.
function push_main() {
if [ "$GIT_BRANCH" == "main" ] && [ "$pr" == "false" ]; then
# The ones that are always pushed

tag="$remote_repo:$model_name-$git_hash"
docker buildx build --platform=linux/arm64,linux/amd64 \
--build-arg "MODEL_NAME=$original_model_name" \
--push \
--tag "$tag" .
fi
}

function push_tag() {
if [ ! -z "$GIT_TAG" ]; then
tag_git="$remote_repo:$model_name-$GIT_TAG"
if [ ! -z "$git_tag" ]; then
tag_git="$remote_repo:$model_name-$git_tag"
tag_latest="$remote_repo:$model_name-latest"
tag="$remote_repo:$model_name"

Expand Down
46 changes: 46 additions & 0 deletions cicd/docker_push_custom_base.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/usr/bin/env bash

set -eou pipefail

git_hash=
git_tag=${GITHUB_REF##*/}
remote_repo=${REMOTE_REPO?Variable REMOTE_REPO is required}
docker_username=${DOCKER_USERNAME?Variable DOCKER_USERNAME is required}
docker_password=${DOCKER_PASSWORD?Variable DOCKER_PASSWORD is required}

function main() {
init
echo "git ref type is $GITHUB_REF_TYPE"
echo "git ref name is $GITHUB_REF_NAME"
echo "git tag is $git_tag"
push_tag
}

function init() {
git_hash="$(git rev-parse HEAD | head -c 7)"

docker run --rm --privileged multiarch/qemu-user-static --reset -p yes
docker buildx create --use

echo "$docker_password" | docker login -u "$docker_username" --password-stdin
}

function push_tag() {
if [ ! -z "$git_tag" ] && [ "$GITHUB_REF_TYPE" == "tag" ]; then
tag_git="$remote_repo:custom-$git_tag"
tag_latest="$remote_repo:custom-latest"
tag="$remote_repo:custom"

echo "Tag & Push $tag, $tag_latest, $tag_git"
docker tag "custom-base" "$tag" && docker push "$tag"

docker buildx build --platform=linux/arm64,linux/amd64 -f custom.Dockerfile \
--tag "$tag" \
--tag "$tag_latest" \
--tag "$tag_git" \
--push \
.
fi
}

main "${@}"

0 comments on commit adf2310

Please sign in to comment.