@ethicdevs/gitfoss | Show object: eb6cf6f3ab0da561167bc2ab4fadb451f8fe409a ∙ GitFOSS
fix(gitfoss-ci): make code clean
+ 97
- 84
@@ -31,3 +31,7 @@ data/git_repos_qa/
 data/postgres_data/
 gitfoss_web.tar
 .docker_temp*
+packages/gitfoss-ci-runner/.workdir/
+packages/gitfoss-ci-runner/file_to_cache.txt/
+packages/gitfoss-ci-runner/.workdir/file.txt
+packages/gitfoss-ci-runner/file.txt

packages/gitfoss-ci-runner/example.ci.yml
@@ -1,7 +1,7 @@
 stages:
   - name: hello
     image: alpine:3.18
-    workdir: .:/home/node/
+    workdir: ./.workdir/:/home/node/
     env:
       FOO: "$HOME"
     shell: sh

...
@@ -14,13 +14,13 @@ stages:
       - /home/node/build/
   - name: success
     image: alpine:3.18
-    workdir: .:/home/node/
-    shell: sh
     commands:
       - exit 0 # On-purpose success for example
   - name: failing
     image: alpine:3.18
-    workdir: .:/home/node/
-    shell: sh
     commands:
-      - exit 1 # On-purpose failure for example
+      - exit 0 # On-purpose failure for example
+  - name: success2
+    image: alpine:3.16
+    commands:
+      - exit 0 # On-purpose success for example

packages/gitfoss-ci-runner/src/command.cr
@@ -23,8 +23,8 @@ struct Command
     @env = {} of String => String,
     @commands = [] of String,
     @cached = [] of String,
-    @shell = "",
-    @workdir = "",
+    @shell = "sh",
+    @workdir = ".",
     @artifacts_to_set = {} of String => String,
     @artifacts_to_get = {} of String => String,
   )

...
@@ -38,24 +38,27 @@ struct Command
     stage : Stage,
   )
     exit_code = 0
+    logs = ""
     binds = [] of String
     container = nil
+    env_array = ["CI=1"] of String
 
-    begin
-      # log_pipe "Env. vars parsing ..."
-      env_array = [] of String
-      @env.each do |k, v|
-        val = v
-        if val.size > 0 && val[0] == '$'
-          key = val[1..-1]?
-          val = key ? ENV[key]? || "" : ""
+    if @env.size > 0
+      begin
+        # log_pipe "Env. vars parsing ..."
+        @env.each do |k, v|
+          val = v
+          if val.size > 0 && val[0] == '$'
+            key = val[1..-1]?
+            val = key ? ENV[key]? || "" : ""
+          end
+          env_array << "#{k}=#{val}"
         end
-        env_array << "#{k}=#{val}"
+        log_pipe "Env. vars set up"
+      rescue ex : Exception
+        err_pipe "Failure: env_array", ex
+        exit 1
       end
-      log_pipe "Env. vars set up"
-    rescue ex : Exception
-      err_pipe "Failure: env_array", ex
-      exit 1
     end
 
     begin

...
@@ -75,22 +78,23 @@ struct Command
       exit 1
     end
 
-    begin
-      log_pipe "Cache volumes parsing ..."
-      @cached.each do |path|
-        vid = "cache_volume"
-        # todo(docker): fork docker shard and add missing modules (volumes, archives, etc)
-        volumes = docker.volumes.list(filters: { "name" => [vid] }) rescue [] of Docker::Api::Models::Volume
-        if volumes.size == 0
-          docker.volumes.create(name: vid)
+    if @cached.size > 0
+      begin
+        log_pipe "Cache volumes parsing ..."
+        @cached.each do |path|
+          vid = "cache_volume"
+          volumes = docker.volumes.list(filters: { "name" => [vid] }) rescue [] of Docker::Api::Models::Volume
+          if volumes.size == 0
+            docker.volumes.create(name: vid)
+          end
+          log_pipe "Mounted volume: #{vid}:#{path}"
+          binds << "#{vid}:#{path}"
         end
-        log_pipe "Mounted volume: #{vid}:#{path}"
-        binds << "#{vid}:#{path}"
+        log_pipe "Cache volumes set up"
+      rescue ex : Exception
+        err_pipe "Failure: cache volumes", ex
+        exit 1
       end
-      log_pipe "Cache volumes set up"
-    rescue ex : Exception
-      err_pipe "Failure: cache volumes", ex
-      exit 1
     end
 
     begin

...
@@ -130,6 +134,7 @@ struct Command
       log_pipe "Written pipeline script in container: /tmp/script.sh"
     rescue ex : Exception
       err_pipe "Failure: container.archive.put", ex
+      cleanup_container container
       exit 1
     end
 

...
@@ -149,26 +154,28 @@ struct Command
       # log_pipe "Container reloaded: #{container.id}"
     rescue ex : Exception
       err_pipe "Failure: container.reload", ex
+      cleanup_container container
       exit 1
     end
 
-    begin
-      log_pipe "Getting artifacts ..."
-      @artifacts_to_get.each do |name, path|
-        data = pipeline.get_artifact(name)
-        if data.nil?
-          raise "Failure: pipeline.get_artifact: #{name} not found"
+    if @artifacts_to_get.size > 0
+      begin
+        log_pipe "Getting artifacts ..."
+        @artifacts_to_get.each do |name, path|
+          data = pipeline.get_artifact(name)
+          if data.nil?
+            raise "Failure: pipeline.get_artifact: #{name} not found"
+          end
+          dest = path
+          dest = "#{workdir_actual}/#{path}" if path[0]? != '/'
+          container.archive.put(dest, data)
         end
-
-        dest = path
-        dest = "#{workdir_actual}/#{path}" if path[0]? != '/'
-
-        container.archive.put(dest, data)
+        log_pipe "Artifacts retrieved!"
+      rescue ex : Exception
+        err_pipe "Failure: container.archive.put", ex
+        cleanup_container container
+        exit 1
       end
-      log_pipe "Artifacts retrieved!"
-    rescue ex : Exception
-      err_pipe "Failure: container.archive.put", ex
-      exit 1
     end
 
     # start log streamer

...
@@ -188,6 +195,8 @@ struct Command
     #     end
     #   rescue ex
     #     STDERR.puts "log stream error: #{ex.message}"
+    #     cleanup_container container
+    #     exit 1
     #   end
     # end
 

...
@@ -197,6 +206,7 @@ struct Command
       log_pipe "Container started: #{container.id}"
     rescue ex : Exception
       log_pipe "Failed to start container: #{ex.message}"
+      cleanup_container container
       exit 1
     end
 

...
@@ -206,6 +216,7 @@ struct Command
       log_pipe "Container exited: #{exit_code} (#{container.id})"
     rescue ex : Exception
       log_pipe "Failed to wait for container: #{ex.message}"
+      cleanup_container container
       exit 1
     end
 

...
@@ -215,52 +226,49 @@ struct Command
       log_pipe "Container logs:\n#{logs}"
     rescue ex : Exception
       log_pipe "Failed to fetch container logs: #{ex.message}"
+      cleanup_container container
       exit 1
     end
 
     if exit_code != 0
+      cleanup_container container
       raise Exception.new("Stage #{stage.name} didn't complete.")
     end
 
-    begin
-      # log_pipe "Reloading container ..."
-      container.reload!
-      log_pipe "Container reloaded: #{container.id}"
-    rescue ex : Exception
-      log_pipe "Failed to reload container: #{ex.message}"
-      exit 1
-    end
-
-    begin
-      # log_pipe "Setting artifacts ..."
-      @artifacts_to_set.each do |artifact_name, path|
-        tar_bytes_from_container = container.archive.get(path)
-        pipeline.set_artifact(
-          artifact_name,
-          tar_bytes_from_container
-        )
-      end
-      log_pipe "Artifacts set: #{@artifacts_to_set.size}"
-    rescue ex : Exception
-      err_pipe "Failed to set artifacts:", ex
-      exit 1
-    end
-
-    if container
+    if @artifacts_to_set.size > 0
       begin
-        # log_pipe "Stopping container ..."
-        container.stop rescue nil
-        log_pipe "Stopped container: #{container.id}"
-      ensure
-        # log_pipe "Removing container ..."
-        container.remove(v: true, force: true) rescue nil
-        log_pipe "Removed container: #{container.id}"
+        # log_pipe "Setting artifacts ..."
+        @artifacts_to_set.each do |artifact_name, path|
+          tar_bytes_from_container = container.archive.get(path)
+          pipeline.set_artifact(
+            artifact_name,
+            tar_bytes_from_container
+          )
+        end
+        log_pipe "Artifacts set: #{@artifacts_to_set.size}"
+      rescue ex : Exception
+        err_pipe "Failed to set artifacts:", ex
+        cleanup_container container
+        exit 1
       end
     end
 
+    cleanup_container container
     exit_code
   end
 
+  def cleanup_container(container : Docker::Container)
+    begin
+      # log_pipe "Stopping container ..."
+      container.stop rescue nil
+      log_pipe "Stopped container: #{container.id}"
+    ensure
+      # log_pipe "Removing container ..."
+      container.remove(v: true, force: true) rescue nil
+      log_pipe "Removed container: #{container.id}"
+    end
+  end
+
   def create_commands_tar : Bytes
     buf = IO::Memory.new
     Crystar::Writer.open(buf) do |tw|

fix(gitfoss-ci): make code clean
+ 97
- 84
@@ -31,3 +31,7 @@ data/git_repos_qa/
 data/postgres_data/
 gitfoss_web.tar
 .docker_temp*
+packages/gitfoss-ci-runner/.workdir/
+packages/gitfoss-ci-runner/file_to_cache.txt/
+packages/gitfoss-ci-runner/.workdir/file.txt
+packages/gitfoss-ci-runner/file.txt

packages/gitfoss-ci-runner/example.ci.yml
@@ -1,7 +1,7 @@
 stages:
   - name: hello
     image: alpine:3.18
-    workdir: .:/home/node/
+    workdir: ./.workdir/:/home/node/
     env:
       FOO: "$HOME"
     shell: sh

...
@@ -14,13 +14,13 @@ stages:
       - /home/node/build/
   - name: success
     image: alpine:3.18
-    workdir: .:/home/node/
-    shell: sh
     commands:
       - exit 0 # On-purpose success for example
   - name: failing
     image: alpine:3.18
-    workdir: .:/home/node/
-    shell: sh
     commands:
-      - exit 1 # On-purpose failure for example
+      - exit 0 # On-purpose failure for example
+  - name: success2
+    image: alpine:3.16
+    commands:
+      - exit 0 # On-purpose success for example

packages/gitfoss-ci-runner/src/command.cr
@@ -23,8 +23,8 @@ struct Command
     @env = {} of String => String,
     @commands = [] of String,
     @cached = [] of String,
-    @shell = "",
-    @workdir = "",
+    @shell = "sh",
+    @workdir = ".",
     @artifacts_to_set = {} of String => String,
     @artifacts_to_get = {} of String => String,
   )

...
@@ -38,24 +38,27 @@ struct Command
     stage : Stage,
   )
     exit_code = 0
+    logs = ""
     binds = [] of String
     container = nil
+    env_array = ["CI=1"] of String
 
-    begin
-      # log_pipe "Env. vars parsing ..."
-      env_array = [] of String
-      @env.each do |k, v|
-        val = v
-        if val.size > 0 && val[0] == '$'
-          key = val[1..-1]?
-          val = key ? ENV[key]? || "" : ""
+    if @env.size > 0
+      begin
+        # log_pipe "Env. vars parsing ..."
+        @env.each do |k, v|
+          val = v
+          if val.size > 0 && val[0] == '$'
+            key = val[1..-1]?
+            val = key ? ENV[key]? || "" : ""
+          end
+          env_array << "#{k}=#{val}"
         end
-        env_array << "#{k}=#{val}"
+        log_pipe "Env. vars set up"
+      rescue ex : Exception
+        err_pipe "Failure: env_array", ex
+        exit 1
       end
-      log_pipe "Env. vars set up"
-    rescue ex : Exception
-      err_pipe "Failure: env_array", ex
-      exit 1
     end
 
     begin

...
@@ -75,22 +78,23 @@ struct Command
       exit 1
     end
 
-    begin
-      log_pipe "Cache volumes parsing ..."
-      @cached.each do |path|
-        vid = "cache_volume"
-        # todo(docker): fork docker shard and add missing modules (volumes, archives, etc)
-        volumes = docker.volumes.list(filters: { "name" => [vid] }) rescue [] of Docker::Api::Models::Volume
-        if volumes.size == 0
-          docker.volumes.create(name: vid)
+    if @cached.size > 0
+      begin
+        log_pipe "Cache volumes parsing ..."
+        @cached.each do |path|
+          vid = "cache_volume"
+          volumes = docker.volumes.list(filters: { "name" => [vid] }) rescue [] of Docker::Api::Models::Volume
+          if volumes.size == 0
+            docker.volumes.create(name: vid)
+          end
+          log_pipe "Mounted volume: #{vid}:#{path}"
+          binds << "#{vid}:#{path}"
         end
-        log_pipe "Mounted volume: #{vid}:#{path}"
-        binds << "#{vid}:#{path}"
+        log_pipe "Cache volumes set up"
+      rescue ex : Exception
+        err_pipe "Failure: cache volumes", ex
+        exit 1
       end
-      log_pipe "Cache volumes set up"
-    rescue ex : Exception
-      err_pipe "Failure: cache volumes", ex
-      exit 1
     end
 
     begin

...
@@ -130,6 +134,7 @@ struct Command
       log_pipe "Written pipeline script in container: /tmp/script.sh"
     rescue ex : Exception
       err_pipe "Failure: container.archive.put", ex
+      cleanup_container container
       exit 1
     end
 

...
@@ -149,26 +154,28 @@ struct Command
       # log_pipe "Container reloaded: #{container.id}"
     rescue ex : Exception
       err_pipe "Failure: container.reload", ex
+      cleanup_container container
       exit 1
     end
 
-    begin
-      log_pipe "Getting artifacts ..."
-      @artifacts_to_get.each do |name, path|
-        data = pipeline.get_artifact(name)
-        if data.nil?
-          raise "Failure: pipeline.get_artifact: #{name} not found"
+    if @artifacts_to_get.size > 0
+      begin
+        log_pipe "Getting artifacts ..."
+        @artifacts_to_get.each do |name, path|
+          data = pipeline.get_artifact(name)
+          if data.nil?
+            raise "Failure: pipeline.get_artifact: #{name} not found"
+          end
+          dest = path
+          dest = "#{workdir_actual}/#{path}" if path[0]? != '/'
+          container.archive.put(dest, data)
         end
-
-        dest = path
-        dest = "#{workdir_actual}/#{path}" if path[0]? != '/'
-
-        container.archive.put(dest, data)
+        log_pipe "Artifacts retrieved!"
+      rescue ex : Exception
+        err_pipe "Failure: container.archive.put", ex
+        cleanup_container container
+        exit 1
       end
-      log_pipe "Artifacts retrieved!"
-    rescue ex : Exception
-      err_pipe "Failure: container.archive.put", ex
-      exit 1
     end
 
     # start log streamer

...
@@ -188,6 +195,8 @@ struct Command
     #     end
     #   rescue ex
     #     STDERR.puts "log stream error: #{ex.message}"
+    #     cleanup_container container
+    #     exit 1
     #   end
     # end
 

...
@@ -197,6 +206,7 @@ struct Command
       log_pipe "Container started: #{container.id}"
     rescue ex : Exception
       log_pipe "Failed to start container: #{ex.message}"
+      cleanup_container container
       exit 1
     end
 

...
@@ -206,6 +216,7 @@ struct Command
       log_pipe "Container exited: #{exit_code} (#{container.id})"
     rescue ex : Exception
       log_pipe "Failed to wait for container: #{ex.message}"
+      cleanup_container container
       exit 1
     end
 

...
@@ -215,52 +226,49 @@ struct Command
       log_pipe "Container logs:\n#{logs}"
     rescue ex : Exception
       log_pipe "Failed to fetch container logs: #{ex.message}"
+      cleanup_container container
       exit 1
     end
 
     if exit_code != 0
+      cleanup_container container
       raise Exception.new("Stage #{stage.name} didn't complete.")
     end
 
-    begin
-      # log_pipe "Reloading container ..."
-      container.reload!
-      log_pipe "Container reloaded: #{container.id}"
-    rescue ex : Exception
-      log_pipe "Failed to reload container: #{ex.message}"
-      exit 1
-    end
-
-    begin
-      # log_pipe "Setting artifacts ..."
-      @artifacts_to_set.each do |artifact_name, path|
-        tar_bytes_from_container = container.archive.get(path)
-        pipeline.set_artifact(
-          artifact_name,
-          tar_bytes_from_container
-        )
-      end
-      log_pipe "Artifacts set: #{@artifacts_to_set.size}"
-    rescue ex : Exception
-      err_pipe "Failed to set artifacts:", ex
-      exit 1
-    end
-
-    if container
+    if @artifacts_to_set.size > 0
       begin
-        # log_pipe "Stopping container ..."
-        container.stop rescue nil
-        log_pipe "Stopped container: #{container.id}"
-      ensure
-        # log_pipe "Removing container ..."
-        container.remove(v: true, force: true) rescue nil
-        log_pipe "Removed container: #{container.id}"
+        # log_pipe "Setting artifacts ..."
+        @artifacts_to_set.each do |artifact_name, path|
+          tar_bytes_from_container = container.archive.get(path)
+          pipeline.set_artifact(
+            artifact_name,
+            tar_bytes_from_container
+          )
+        end
+        log_pipe "Artifacts set: #{@artifacts_to_set.size}"
+      rescue ex : Exception
+        err_pipe "Failed to set artifacts:", ex
+        cleanup_container container
+        exit 1
       end
     end
 
+    cleanup_container container
     exit_code
   end
 
+  def cleanup_container(container : Docker::Container)
+    begin
+      # log_pipe "Stopping container ..."
+      container.stop rescue nil
+      log_pipe "Stopped container: #{container.id}"
+    ensure
+      # log_pipe "Removing container ..."
+      container.remove(v: true, force: true) rescue nil
+      log_pipe "Removed container: #{container.id}"
+    end
+  end
+
   def create_commands_tar : Bytes
     buf = IO::Memory.new
     Crystar::Writer.open(buf) do |tw|

packages/gitfoss-ci-runner/src/gitfoss-ci-runner.cr
@@ -130,6 +130,7 @@ module Gitfoss::Ci::Runner
           exit 1
         end
       end
+      puts "\n"
     end
   end
 end

packages/gitfoss-ci-runner/src/utils.cr
@@ -9,8 +9,8 @@ end
 
 def map_to_command(command_name : String, node : YAML::Any) : Command
   image = node["image"].as_s
-  workdir = node["workdir"].as_s? || "."
-  shell = node["shell"].as_s? || "bash"
+  workdir = node["workdir"]?.try(&.as_s) || ""
+  shell = node["shell"]?.try(&.as_s) || "sh"
   env = {} of String => String
   if node["env"]?
     node["env"].as_h.each do |k, v|

packages/gitfoss-ci-runner/src/gitfoss-ci-runner.cr
@@ -130,6 +130,7 @@ module Gitfoss::Ci::Runner
           exit 1
         end
       end
+      puts "\n"
     end
   end
 end

packages/gitfoss-ci-runner/src/utils.cr
@@ -9,8 +9,8 @@ end
 
 def map_to_command(command_name : String, node : YAML::Any) : Command
   image = node["image"].as_s
-  workdir = node["workdir"].as_s? || "."
-  shell = node["shell"].as_s? || "bash"
+  workdir = node["workdir"]?.try(&.as_s) || ""
+  shell = node["shell"]?.try(&.as_s) || "sh"
   env = {} of String => String
   if node["env"]?
     node["env"].as_h.each do |k, v|

packages/gitfoss-ci-runner/src/gitfoss-ci-runner.cr
@@ -130,6 +130,7 @@ module Gitfoss::Ci::Runner
           exit 1
         end
       end
+      puts "\n"
     end
   end
 end

packages/gitfoss-ci-runner/src/utils.cr
@@ -9,8 +9,8 @@ end
 
 def map_to_command(command_name : String, node : YAML::Any) : Command
   image = node["image"].as_s
-  workdir = node["workdir"].as_s? || "."
-  shell = node["shell"].as_s? || "bash"
+  workdir = node["workdir"]?.try(&.as_s) || ""
+  shell = node["shell"]?.try(&.as_s) || "sh"
   env = {} of String => String
   if node["env"]?
     node["env"].as_h.each do |k, v|

GitFOSS • v0.2.0 (#50a2553) • MIT License