From 2b8a4b13378a6aeedc32573a59349569f918c66e Mon Sep 17 00:00:00 2001 From: heinpa Date: Fri, 28 Jun 2024 14:55:41 +0200 Subject: [PATCH] split generation and execution of docker commands --- .github/workflows/deploy-component.yml | 18 ++++++- service_config/deploy_components.sh | 52 ------------------ service_config/generate_deployment_script.sh | 55 ++++++++++++++++++++ 3 files changed, 71 insertions(+), 54 deletions(-) delete mode 100755 service_config/deploy_components.sh create mode 100755 service_config/generate_deployment_script.sh diff --git a/.github/workflows/deploy-component.yml b/.github/workflows/deploy-component.yml index 768b830..e77566d 100644 --- a/.github/workflows/deploy-component.yml +++ b/.github/workflows/deploy-component.yml @@ -15,17 +15,31 @@ jobs: steps: - uses: actions/checkout@v4 - name: Prepare deployment - run: bash -c ./service_config/prepare_deployment.sh + run: bash -c ./service_config/prepare_deployment.sh ./service_config/service_config.json - name: "Deploy on DINO" if: ${{ env.TARGET == 'DINO' }} # ssh into server + run: bash -c ./service_config/generate_deployment_script.sh + uses: appleboy/scp-action@master + with: + host: ${{ secrets. SERVER_HOST }} + port : ${{ secrets.SERVER_PORT }} + username: ${{ secrets.SERVER_USERNAME }} + password: ${{ secrets.SERVER_PASSWORD }} + source: "./service_config/deploy_components.sh" + target: "/" uses: appleboy/ssh-action@master with: host: ${{ secrets. SERVER_HOST }} port : ${{ secrets.SERVER_PORT }} username: ${{ secrets.SERVER_USERNAME }} password: ${{ secrets.SERVER_PASSWORD }} - script: ./service_config/deploy_components.sh ./service_config/service_config.json + script: | + pwd + ls + chmod +x deploy_components.sh && + ./deploy_components.sh || true && + rm deploy_component.sh # use MSU to update on SWE - name: "Deploy on SWE" if: ${{ env.TARGET == 'SWE' }} diff --git a/service_config/deploy_components.sh b/service_config/deploy_components.sh deleted file mode 100755 index c6d09f3..0000000 --- a/service_config/deploy_components.sh +++ /dev/null @@ -1,52 +0,0 @@ -#!/bin/bash - -# deploy components manually using the existing configuration for MSU - -env_files=$ENV_FILES - -file=$1 -if [ -z $file ]; then - echo "missing path to service config file (JSON)" - exit 0 -fi -service_cnt=$(($(jq < $file | jq .services | jq length) - 1)) - -cnt=0 -while [ $cnt -le $service_cnt ]; -do - # collect data from service config - image=$(jq < $file | jq .services.[$cnt].image -r) - tag=$(jq < $file | jq .services.[$cnt].tag -r) - port_mapping=$(jq < $file | jq .services.[$cnt].port -r) - env_file=$(jq < $file | jq '.services.['$cnt'].files.".env"' -r) - container_name=${image/'/'/'-'} - - volumes=$(jq < $file | jq .services.[$cnt].volumes -r) - if [ ! -z "${volumes}" ];then - volume_mappings="" - volume_cnt=$(($(echo $volumes | jq length) - 1)) - i=0 - while [ $i -le $volume_cnt ]; - do - mapping=$(echo $volumes | jq .[$i] -r) - volume_mappings="$volume_mappings-v $mapping " - i=$(($i + 1)) - done - fi - - echo "Stopping and removing old container" - docker stop $container_name || true && docker rm $container_name || true - echo "Pulling image: $DOCKER_IMAGE_NAME" - docker pull "$image:$tag" - echo "Starting container: $DOCKER_CONTAINER_NAME" -# TODO: add conditional on volume mappings - if [ ! -z "${volume_mappings}" ];then - echo "docker run --restart=always -d -p $port_mapping $volume_mappings --env-file $env_files/$env_file --name $container_name $image:$tag" - #docker run --restart=always -d -p $port_mapping $volume_mappings --env-file $env_files/$env_file --name $container_name "$image:$tag" - else - echo "docker run --restart=always -d -p $port_mapping --env-file $env_files/$env_file --name $container_name $image:$tag" - #docker run --restart=always -d -p $port_mapping --env-file $env_files/$env_file --name $container_name "$image:$tag" - fi - - cnt=$(($cnt + 1)) -done diff --git a/service_config/generate_deployment_script.sh b/service_config/generate_deployment_script.sh new file mode 100755 index 0000000..266c940 --- /dev/null +++ b/service_config/generate_deployment_script.sh @@ -0,0 +1,55 @@ +#!/bin/bash + +# deploy components manually using the existing configuration for MSU + +env_files=$ENV_FILES + +outfile="service_config/deploy_components.sh" +echo "" > $outfile + +file=$1 +if [ -z $file ]; then + echo "missing path to service config file (JSON)" + exit 0 +fi +service_cnt=$(($(jq < $file | jq .services | jq length) - 1)) + +cnt=0 +while [ $cnt -le $service_cnt ]; +do + # collect data from service config + image=$(jq < $file | jq .services.[$cnt].image -r) + tag=$(jq < $file | jq .services.[$cnt].tag -r) + port_mapping=$(jq < $file | jq .services.[$cnt].port -r) + env_file=$(jq < $file | jq '.services.['$cnt'].files.".env"' -r) + container_name=${image/'/'/'-'} + + volumes=$(jq < $file | jq .services.[$cnt].volumes -r) + if [ ! -z "${volumes}" ];then + volume_mappings="" + volume_cnt=$(($(echo $volumes | jq length) - 1)) + i=0 + while [ $i -le $volume_cnt ]; + do + mapping=$(echo $volumes | jq .[$i] -r) + volume_mappings="$volume_mappings-v $mapping " + i=$(($i + 1)) + done + fi + + echo 'echo "Stopping and removing old container"' >> $outfile + echo 'docker stop $container_name || true && docker rm $container_name || true' >> $outfile + echo 'echo "Pulling image: $DOCKER_IMAGE_NAME' >> $outfile + echo 'docker pull "$image:$tag"' >> $outfile + echo 'echo "Starting container: $DOCKER_CONTAINER_NAME"' >> $outfile + + if [ ! -z "${volume_mappings}" ];then + echo 'echo "docker run --restart=always -d -p $port_mapping $volume_mappings --env-file $env_files/$env_file --name $container_name $image:$tag" ' >> $outfile + echo '#docker run --restart=always -d -p $port_mapping $volume_mappings --env-file $env_files/$env_file --name $container_name "$image:$tag" ' >> $outfile + else + echo 'echo "docker run --restart=always -d -p $port_mapping --env-file $env_files/$env_file --name $container_name $image:$tag" ' >> $outfile + echo '#docker run --restart=always -d -p $port_mapping --env-file $env_files/$env_file --name $container_name "$image:$tag" ' >> $outfile + fi + + cnt=$(($cnt + 1)) +done