Local Pipeline Project Like Gitlab CI CD With Golang and Docker 🚀

You, golanggitlabci cdgithub actions
Back

In this article, I'm gonna show you the pin. The pin is a local pipeline project. You can create your configuration and you can run it in your local Docker.

I have used Gitlab CI/CD and CircleCI in my projects. I want to create my own pipeline project. Thus I start working on it and I made the pin. The pin is written in Golang and uses the Docker Golang API to communicate with the Docker Daemon. The pin has a configuration file like any other pipeline service. It reads the configuration file and runs your jobs in your local Docker.

You can access project here

✍️Let's look at the usage and examples

First of all, you must download the pin from releases to your machine and your Docker must be running.

In this example, we send a request to Google and print the status code if it is successful. Create index.js and pipeline.yaml files like below.

const axios = require('axios');

axios
  .get('https://google.com')
  .then(res => {
    console.log(`statusCode: ${res.status}`);
  })
  .catch(error => {
    console.error(`error: ${error.code}`);
  });
workflow:
  - myJob

myJob:
  image: node:current-alpine3.15
  copyFiles: true
  soloExecution: true
  script:
    - node -v
    - npm install axios
    - node index.js

I want to explain some properties in pipeline.yaml

And you can run this command in your terminal

pin apply -f .\pipeline.yaml

The result seems like below

⚉ myJob Image is available
⚉ myJob Start creating container
⚉ myJob Starting the container
⚉ myJob Execute command: node -v
⚉ myJob Command execution successful
⚉ myJob Command Log:
v18.1.0
⚉ myJob Execute command: npm install axios
⚉ myJob Command execution successful
⚉ myJob Command Log:
up to date, audited 9 packages in 1s
1 package is looking for funding
  run `npm fund` for details
found 0 vulnerabilities
npm notice
npm notice New minor version of npm available! 8.8.0 -> 8.10.0
npm notice Changelog: <https://github.com/npm/cli/releases/tag/v8.10.0>
npm notice Run `npm install -g npm@8.10.0` to update!
npm notice
⚉ myJob Execute command: node index.js
⚉ myJob Command execution successful
⚉ myJob Command Log:
statusCode: 200
⚉ myJob Container stopping
⚉ myJob Container stopped
⚉ myJob Container removing
⚉ myJob Container removed
⚉ myJob Job ended

Let’ create another example, change index.js and pipeline.yaml files like below.

const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  res.send('Hello World!')
})

app.listen(port, () => {
  console.log(`Example app listening on port ${port}`)
})
workflow:
  - myJob

myJob:
  image: node:current-alpine3.15
  copyFiles: true
  soloExecution: true
  port:
    - 3000:3000
  script:
    - npm install express
    - node index.js

In this example, we create a server with express.js. pipeline.yaml has changed, we add port field. The Port field exposes the specified ports to the container and we can access the container with them.

Let’s run pin

pin apply -f .\pipeline.yaml

The result should be like below

⚉ myJob Image is available
⚉ myJob Start creating container
⚉ myJob Starting the container
⚉ myJob Execute command: npm install express
⚉ myJob Command execution successful
⚉ myJob Command Log:
added 55 packages, and audited 64 packages in 4s
8 packages are looking for funding
  run `npm fund` for details
found 0 vulnerabilities
npm notice
npm notice New minor version of npm available! 8.8.0 -> 8.10.0
npm notice Changelog: <https://github.com/npm/cli/releases/tag/v8.10.0>
npm notice Run `npm install -g npm@8.10.0` to update!
npm notice
⚉ myJob Execute command: node index.js

Our configuration waiting like this because we create a server, let’s the open the browser and go to http://localhost:3000/ and see the “Hello World!” message.

Localhost

You can exit from the job, pin support graceful shutdown.

⚉ myJob Execute command: node index.js
System call detected!
⚉ myJob Container removing

And we have done, I just create this project to improve my Golang and Github skills. I actively try to use Github Actions, Issues, and Pages. This project be like my playground 😁

Thanks for reading.👋

© Muhammed İkinci.RSS