DEV Community

Albert Coronado
Albert Coronado

Posted on

Create CI/CD pipelines en Golang

Golang CI/CD is a library to build and automate continuous integration and continuous delivery (CI/CD) pipelines. By leveraging Go's concurrency features, efficiency, and robust standard library, devops can create highly customizable and efficient CI/CD pipelines tailored to their specific project requirements. This allows for faster development cycles, improved code quality, and more reliable deployments.

Atention! Is not just for Golang projects. You can create CICD pipelines for all languages and technologies.

Why

  • Lightweight, flexible and fast.
  • You can compile the pipeline in order to ofuscate secrets.
  • Use concurrency, logs, functional programming and other resources of Golang.
  • Connect to Git servers, SSH remote servers, etc.
  • Easy to integrate with Github, Gitlab, Cloud and other tools.

How to create a pipeline

Create a module and install the library:

go mod init gocicd-sample && go get -u github.com/acoronadoc/cicd
Enter fullscreen mode Exit fullscreen mode

Create your first pipeline(script.go):

package main

import (
    "log"

    "github.com/acoronadoc/cicd"
)

const repoURL = "git@github.com:acoronadoc/test-repo.git"
const repoBranch = "main"
const repoKey = "./key"

func main() {

    cicd.StartPipe(true,

        cicd.PipeWaitForCommit(repoURL, repoBranch, repoKey, 10),

        &cicd.RepoPipe{Action: func(state *map[string]interface{}) {
            log.Println("Download Commit", (*state)["COMMITID"])

            cicd.ExecuteCommand("rm", []string{"-R", "-f", "./repo"}, []string{})
            cicd.GitCloneSSH(repoURL, repoBranch, "./repo", repoKey)
            cicd.ExecuteCommand("docker", []string{"stop", "app1"}, []string{})
            cicd.ExecuteCommand("docker", []string{"rm", "app1"}, []string{})
            cicd.ExecuteCommand("docker", []string{"run", "-d", "--name", "app1", "-p", "8080:80", "nginx:1.27-alpine3.20"}, []string{})
            cicd.ExecuteCommand("docker", []string{"cp", "./repo/index.html", "app1:/usr/share/nginx/html/index.html"}, []string{})
        }},
    )

}
Enter fullscreen mode Exit fullscreen mode

You have more samples here.

Top comments (1)

Collapse
 
acoronadoc profile image
Albert Coronado

Brutal!