Home Web Front-end JS Tutorial Building Cross-Platform CI/CD Actions with Docker

Building Cross-Platform CI/CD Actions with Docker

Jan 30, 2025 am 12:31 AM

Building Cross-Platform CI/CD Actions with Docker

Last week I got hit by a headache - our perfectly tuned Lingo.dev GitHub Actions workflow couldn't run on a contributor's GitLab instance. Then I realized I’d actually like to run the same automation everywhere, regardless of the platform.

So I went on a quest to build a cross-platform CI automation that runs on GitHub, GitLab, and Bitbucket (and possibly others too!). The solution started as a simple GitHub Action but evolved into something more powerful when we needed to support multiple code hosting platforms.

I will walk you through the exact process:

  1. starting simple, I will showcase how Github Actions work
  2. leveling up to build reusable Docker image
  3. finally I will show you how to run this on each platform

Follow the steps to build and ship your first cross-platform action. Or bookmark the article for later.

tl;dr see the template repository ???

1. Starting Simple: Run JavaScript in GitHub Actions

Running a GitHub Action

Let's start with the simplest possible GitHub Action - one that runs a JavaScript file. First, create index.js in the root of your repository:

console.log("Hello World");

Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

Now create a workflow file .github/workflows/hello.yml:

name: Hello World
on:
  push:
    branches:
      - main

jobs:
  hello:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: "20"
      - name: Say Hello
        run: node index.js

Copy after login
Copy after login
Copy after login

This action will:

  1. Trigger on push to main branch
  2. Check out your repository
  3. Set up Node.js environment
  4. Run your script

Making it Reusable

Now, let's make this action reusable by moving it to a separate repository. Create a new GitHub repository (e.g., hello-world-action like my example here) with these files:

  1. index.js (same as before):
console.log("Hello World");

Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
  1. action.yml:
name: "Hello World Action"
description: "A simple action that says hello"
runs:
  using: "node20"
  main: "index.js"

Copy after login
Copy after login

Now you can use this action in any repository by referencing it in your workflow:

name: Use Hello Action
on:
  push:
    branches:
      - main

jobs:
  hello:
    runs-on: ubuntu-latest
    steps:
      - name: Say Hello
        uses: your-username/hello-world-action@main

Copy after login
Copy after login

The key differences are:

  1. The action lives in its own repository
  2. action.yml defines how to run the action
  3. Other repositories can reference it using uses: your-username/hello-world-action@ref

2. Leveling Up: Dockerized TypeScript Action

Now, let's create a more sophisticated action that runs TypeScript code. We'll need several files:

  1. Initialize the project and set up TypeScript:

    pnpm init                # Creates package.json
    pnpm add -D typescript   # Install TypeScript as dev dependency
    
    
    Copy after login
    Copy after login
  2. Update your package.json to add the build script:

    {
      "scripts": {
        "build": "tsc"
      }
    }
    
    
    Copy after login
    Copy after login
  3. Then rename our index.js to index.ts to use TypeScript instead of JavaScript and move it to the src directory.

  4. Create tsconfig.json to configure TypeScript compilation:

    {
      "compilerOptions": {
        "target": "ES2022",
        "module": "commonjs",
        "outDir": "./build",
        "rootDir": "./src",
        "strict": true,
        "esModuleInterop": true,
        "skipLibCheck": true,
        "forceConsistentCasingInFileNames": true
      },
      "exclude": ["node_modules", "build", "**/*.test.ts"]
    }
    
    
    Copy after login
    Copy after login
  5. Create a Dockerfile:

console.log("Hello World");

Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
  1. Redefine the action in action.yml:
name: Hello World
on:
  push:
    branches:
      - main

jobs:
  hello:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: "20"
      - name: Say Hello
        run: node index.js

Copy after login
Copy after login
Copy after login

Running Docker Image Locally

To build and run the image defined in Dockerfile locally, you need Docker Desktop app. Then, assuming Docker is running locally, you can:

  1. Build the image:
console.log("Hello World");

Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
  1. Run it:
name: "Hello World Action"
description: "A simple action that says hello"
runs:
  using: "node20"
  main: "index.js"

Copy after login
Copy after login

3. Cross-Platform Support

Now comes the most interesting part - running your action on other platforms. We'll need to:

  1. Build and push our Docker image to a registry
  2. Reference it in platform-specific configurations

Building and Publishing Docker Image

First, create a workflow to build and push the Docker image on every release. We will be using GitHub Container Registry (GHCR) that comes with your GitHub (free for public repositories, 500MB for private repositories on free plan).

name: Use Hello Action
on:
  push:
    branches:
      - main

jobs:
  hello:
    runs-on: ubuntu-latest
    steps:
      - name: Say Hello
        uses: your-username/hello-world-action@main

Copy after login
Copy after login

Github Actions

To use this action in GitHub Actions of another repository, create a workflow file in .github/workflows/hello.yml (see GitHub Actions workflow syntax documentation):

pnpm init                # Creates package.json
pnpm add -D typescript   # Install TypeScript as dev dependency

Copy after login
Copy after login

If you need to run this action on GitHub Actions only, there is no need to build and push the Docker image. GitHub Actions will build the Docker container directly from the Dockerfile specified in your action.yml file on each workflow run. This is more efficient since it avoids the extra steps of pushing to and pulling from a container registry. However, if you plan to use this action on other CI platforms like GitLab or Bitbucket, you'll need to publish the Docker image as shown above.

GitHub's free plan offers the most generous CI/CD minutes allocation among the three platforms. It includes:

  • unlimited minutes for public repositories
  • 2,000 minutes/month for private repositories

GitLab CI Configuration

Create .gitlab-ci.yml (see GitLab CI/CD documentation):

{
  "scripts": {
    "build": "tsc"
  }
}

Copy after login
Copy after login

Your Gitlab free plan includes 400 CI/CD compute minutes per month regardless if the repository is public or private.

Bitbucket Pipelines

Create bitbucket-pipelines.yml (see Bitbucket Pipelines documentation):

{
  "compilerOptions": {
    "target": "ES2022",
    "module": "commonjs",
    "outDir": "./build",
    "rootDir": "./src",
    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true
  },
  "exclude": ["node_modules", "build", "**/*.test.ts"]
}

Copy after login
Copy after login

Bitbucket's free plan includes only 50 build minutes per month, regardless if the repository is public or private, making it the lowest free tier of all three platforms.

Other CI/CD Platforms

Since we published our Docker image in a public Github Container Registry, we can run this on any platform that supports Docker images. This is supported by:

  • Circle CI (docs)
  • Travis CI (docs)

Let me know if you run this somewhere else, I am curious about your use case!


? What can you do with this?

We built an action to run automated Lingo.dev localization for your web and mobile apps on any CI platform. On every commit it updates translations in your entire repository using Lingo.dev localization engine - either as a new commit or by opening a pull request (docs).

Example for GitHub:

console.log("Hello World");

Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

You can build reusable actions and easily integrate them into your workflows regardless of the code hosting platform you are using. Here are some ideas for you:

  • cross-platform test reporter (orchestrating end-to-end testing session)
  • custom code quality checker (think formatting, linting, testing)
  • release notes generator (how about publishing a changelog on your website?)

What would you use it for?

? Bonus: Template repository

If you don't feel like reading the rest of the article, you can find a template starter repository with all the code here. It contains the action - you can run it on GitHub, GitLab and Bitbucket.

https://github.com/mathio/awesome-action-starter

? Tip: Clone the repo and make it a starting point for your own action:

name: Hello World
on:
  push:
    branches:
      - main

jobs:
  hello:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: "20"
      - name: Say Hello
        run: node index.js

Copy after login
Copy after login
Copy after login

…or just create a new repository from my template.

Then just implement your own action logic in src/index.ts file.

Conclusion

We've seen how to evolve from a simple shell-based GitHub Action to a sophisticated TypeScript action that can run anywhere. The key takeaways are:

  1. Start simple with shell commands to validate your automation logic
  2. Dockerize your action to make it portable
  3. Use container registries to distribute your action across platforms
  4. Adapt the configuration for each platform while keeping the core logic in Docker

This approach gives you the flexibility to run your automation anywhere while maintaining a single codebase. Happy automating!


I occasionally post about tech stuff and new Lingo.dev features on Twitter as @mathio28. Let's keep in touch.

The above is the detailed content of Building Cross-Platform CI/CD Actions with Docker. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What should I do if I encounter garbled code printing for front-end thermal paper receipts? What should I do if I encounter garbled code printing for front-end thermal paper receipts? Apr 04, 2025 pm 02:42 PM

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

Demystifying JavaScript: What It Does and Why It Matters Demystifying JavaScript: What It Does and Why It Matters Apr 09, 2025 am 12:07 AM

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

Who gets paid more Python or JavaScript? Who gets paid more Python or JavaScript? Apr 04, 2025 am 12:09 AM

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

Is JavaScript hard to learn? Is JavaScript hard to learn? Apr 03, 2025 am 12:20 AM

Learning JavaScript is not difficult, but it is challenging. 1) Understand basic concepts such as variables, data types, functions, etc. 2) Master asynchronous programming and implement it through event loops. 3) Use DOM operations and Promise to handle asynchronous requests. 4) Avoid common mistakes and use debugging techniques. 5) Optimize performance and follow best practices.

How to achieve parallax scrolling and element animation effects, like Shiseido's official website?
or:
How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? How to achieve parallax scrolling and element animation effects, like Shiseido's official website? or: How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? Apr 04, 2025 pm 05:36 PM

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

The Evolution of JavaScript: Current Trends and Future Prospects The Evolution of JavaScript: Current Trends and Future Prospects Apr 10, 2025 am 09:33 AM

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

How to merge array elements with the same ID into one object using JavaScript? How to merge array elements with the same ID into one object using JavaScript? Apr 04, 2025 pm 05:09 PM

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

The difference in console.log output result: Why are the two calls different? The difference in console.log output result: Why are the two calls different? Apr 04, 2025 pm 05:12 PM

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...

See all articles