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:
Follow the steps to build and ship your first cross-platform action. Or bookmark the article for later.
tl;dr see the template repository ???
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");
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
This action will:
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:
console.log("Hello World");
name: "Hello World Action" description: "A simple action that says hello" runs: using: "node20" main: "index.js"
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
The key differences are:
Now, let's create a more sophisticated action that runs TypeScript code. We'll need several files:
Initialize the project and set up TypeScript:
pnpm init # Creates package.json pnpm add -D typescript # Install TypeScript as dev dependency
Update your package.json to add the build script:
{ "scripts": { "build": "tsc" } }
Then rename our index.js to index.ts to use TypeScript instead of JavaScript and move it to the src directory.
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"] }
Create a Dockerfile:
console.log("Hello World");
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
To build and run the image defined in Dockerfile locally, you need Docker Desktop app. Then, assuming Docker is running locally, you can:
console.log("Hello World");
name: "Hello World Action" description: "A simple action that says hello" runs: using: "node20" main: "index.js"
Now comes the most interesting part - running your action on other platforms. We'll need to:
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
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
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:
Create .gitlab-ci.yml (see GitLab CI/CD documentation):
{ "scripts": { "build": "tsc" } }
Your Gitlab free plan includes 400 CI/CD compute minutes per month regardless if the repository is public or private.
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"] }
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.
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:
Let me know if you run this somewhere else, I am curious about your use case!
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");
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:
What would you use it for?
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.jsCopy after loginCopy after loginCopy after login…or just create a new repository from my template.
Then just implement your own action logic in src/index.ts file.
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:
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!