GitHub Actions vs GitLab CI side by side
The same Go pipeline for shorty (lint, test with Postgres and the race detector, build and push an image) written for GitHub Actions and for GitLab CI, with every concept mapped across.
I have written shorty's CI twice now, once for GitHub Actions and once for GitLab CI. Both files do the same job and they still look different enough that switching between them costs me a few minutes of "wait, how does this one do it". In this blog we will put the two pipelines next to each other, line by line, and map the concepts across.
Level is beginner. You should have seen at least one of the two before. If not, start with GitHub Actions from zero or GitLab CI from zero. The app is shorty, the URL shortener in Go from the REST API post. Versions: Go 1.27.4, Postgres 18, actions/checkout@v7, actions/setup-go@v7, docker/build-push-action@v7, Docker 29.10 for the GitLab image job. I checked the GitHub file with actionlint 1.7.12 and the GitLab file with gitlab-ci-local 4.77.1, and ran the Go commands both pipelines run on my Mac. I don't have a GitLab instance or a GitHub repo wired up for this post, so neither pipeline ran on the real service.
The pipeline we want
Same three jobs on both sides:
lint:go vetand agofmtcheck.test:go test -racewith a real Postgres next to it, so the integration test ininternal/linksruns too.build-image: build shorty's Dockerfile and push it to the platform's own registry, only onmain.
lint and test run on every push and every merge or pull request. The image is built only after both passed.
The GitHub Actions version
# shorty CI on GitHub Actions: lint and test on every PR and push, image on main.
name: ci
on:
push:
branches: [main]
pull_request:
jobs:
lint:
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v7
- uses: actions/setup-go@v7
with:
go-version: "1.27.4"
- run: go vet ./...
- run: test -z "$(gofmt -l $(go list -f '{{.Dir}}' ./...))"
test:
runs-on: ubuntu-24.04
services:
postgres:
image: postgres:18-alpine
env:
POSTGRES_USER: shorty
POSTGRES_PASSWORD: shorty
POSTGRES_DB: shorty
ports:
- 5432:5432
options: >-
--health-cmd "pg_isready -h 127.0.0.1 -U shorty"
--health-interval 2s
--health-timeout 3s
--health-retries 15
env:
SHORTY_TEST_DATABASE_URL: postgres://shorty:shorty@localhost:5432/shorty?sslmode=disable
steps:
- uses: actions/checkout@v7
- uses: actions/setup-go@v7
with:
go-version: "1.27.4"
- run: go test -race -count=1 ./...
build-image:
if: github.ref == 'refs/heads/main'
needs: [lint, test]
runs-on: ubuntu-24.04
permissions:
contents: read
packages: write
steps:
- uses: actions/checkout@v7
# GHCR wants a lowercase image name, github.repository keeps the owner's case.
- run: echo "IMAGE=ghcr.io/${GITHUB_REPOSITORY,,}" >> "$GITHUB_ENV"
- uses: docker/login-action@v4
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- uses: docker/build-push-action@v7
with:
context: .
push: true
build-args: VERSION=${{ github.sha }}
tags: ${{ env.IMAGE }}:${{ github.sha }}
The GitLab CI version
# shorty CI on GitLab: lint and test on every MR and branch, image on main.
stages: [test, build]
workflow:
rules:
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
- if: $CI_COMMIT_BRANCH && $CI_OPEN_MERGE_REQUESTS
when: never
- if: $CI_COMMIT_BRANCH
variables:
# Keep Go's caches inside the project dir, GitLab can only cache paths under it.
GOPATH: $CI_PROJECT_DIR/.go
GOCACHE: $CI_PROJECT_DIR/.cache/go-build
default:
image: golang:1.27.4
cache:
key:
files: [go.sum]
paths: [.go/pkg/mod/, .cache/go-build/]
lint:
stage: test
script:
- go vet ./...
- test -z "$(gofmt -l $(go list -f '{{.Dir}}' ./...))"
test:
stage: test
services:
- name: postgres:18-alpine
alias: postgres
variables:
POSTGRES_USER: shorty
POSTGRES_PASSWORD: shorty
POSTGRES_DB: shorty
SHORTY_TEST_DATABASE_URL: postgres://shorty:shorty@postgres:5432/shorty?sslmode=disable
script:
- go test -race -count=1 ./...
build-image:
stage: build
image: docker:29.10.0-cli
services:
- docker:29.10.0-dind
variables:
DOCKER_TLS_CERTDIR: "/certs"
cache: []
rules:
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
script:
- echo "$CI_REGISTRY_PASSWORD" | docker login -u "$CI_REGISTRY_USER" --password-stdin "$CI_REGISTRY"
- docker build --build-arg VERSION=$CI_COMMIT_SHORT_SHA -t "$CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA" .
- docker push "$CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA"
Shorter, but lot more of it is implicit. Let us go through the differences one at a time.
Triggers
GitHub says when to run at the top with on:. Here that is pushes to main and every pull request.
GitLab has no on:. Every push creates a pipeline unless workflow:rules says otherwise. The three rules above are the usual pattern: run for merge requests, skip the branch pipeline when that branch already has an open MR (or you get two pipelines per push), and run for every other branch. Without the middle rule I'd get duplicate pipelines, that one confuses everybody the first time.
Where jobs run
runs-on: ubuntu-24.04 picks a GitHub hosted VM. Every step runs on that VM, and setup-go installs Go onto it.
In GitLab the job runs inside a container on whatever runner picks it up, and image: decides which container. So there is no setup step for Go. The image itself is the setup. I used the Debian based golang:1.27.4 and not the Alpine one, and there is a reason: -race needs cgo and a C compiler. I ran the test in golang:1.27.4-alpine to be sure and got go: -race requires cgo; enable cgo by setting CGO_ENABLED=1, then with cgo on, C compiler "gcc" not found. The GitLab CI from zero post fixes it with apk add gcc musl-dev. Switching image is less to maintain. The GitHub Ubuntu runner already has gcc, so the problem never shows up there.
Choosing a runner in GitLab is done with tags:, which match tags on the runner. No tags means any runner that accepts untagged jobs. If you host your own, the EC2 runner post covers that.
Ordering: needs vs stages
GitHub jobs all run in parallel unless they say needs:. That is why build-image has needs: [lint, test].
GitLab goes the other way. Jobs are grouped into stages, a stage waits for the previous one, and inside a stage everything runs in parallel. build-image is in the build stage, so it waits for lint and test without saying so. GitLab also has needs: for when you want to skip the stage wait, but for three jobs I didn't bother.
Services
Both platforms can start Postgres next to the job, and both call it services. The details differ though.
On GitHub, a job that runs straight on the VM reaches the service through a mapped port, so the URL says localhost:5432 and you need the ports: line. The health check options make the job wait until Postgres accepts connections.
On GitLab the service is a container on the same network as the job container, reachable by its alias. The URL says postgres:5432. There is no health check option. The runner waits for the service's exposed port to open, which for Postgres is good enough. shorty's integration test runs its own goose migrations, so the database only has to exist. I ran go test -race -count=1 ./... against a postgres:18-alpine container on my Mac with the same URL shape, and all four test packages passed.
Caching
setup-go@v7 caches the module and build caches on its own, keyed on go.sum. You don't write anything.
GitLab caches only what you list, and only paths inside the project directory. That is why GOPATH and GOCACHE are moved into $CI_PROJECT_DIR. This changes the gofmt line too. The usual gofmt -l . would now also walk .go/, the module cache inside the project, and complain about every downloaded dependency. go list -f '{{.Dir}}' ./... gives only shorty's own package dirs (go ignores directories starting with a dot), so I used the same line on both sides to keep them identical.
Secrets and the registry
Pushing an image shows the biggest difference in style.
On GitHub the job asks for packages: write and logs in to GHCR with the built in GITHUB_TOKEN. The image name needs a small fix: GHCR wants lowercase names and github.repository keeps the owner's case. My GitHub name has capitals in it, so ${GITHUB_REPOSITORY,,} (bash lowercase) goes into an env var first.
GitLab gives every job CI_REGISTRY, CI_REGISTRY_USER, CI_REGISTRY_PASSWORD and CI_REGISTRY_IMAGE for the project's own registry, and CI_REGISTRY_IMAGE is already lowercase. The catch is the build itself. See, there is no Docker on the job container, so the job starts Docker-in-Docker as a service, and that needs a runner with privileged mode. GitHub's VM simply has Docker. That privileged part is worth a post by itself.
Variables you add yourself are similar on both: GitHub has secrets and variables per repo and per environment, GitLab has CI/CD variables that can be masked, hidden, and protected so only protected branches see them.
A mistake I made on the GitLab side
My first GitLab file called the last job image:. gitlab-ci-local didn't print a nice error, it crashed with TypeError: undefined is not an object (evaluating 'n.includes') in its image parsing code. It took me a minute, actually. image at the top level isn't a job name in GitLab, it is the old global keyword for the default image. Same for services, cache, before_script and a few others. GitHub doesn't have this problem because jobs live under jobs:. GitLab jobs are just top level keys, so a job name can collide with a keyword. I renamed it to build-image on both sides.
Checking both files without the services
go install github.com/rhysd/actionlint/cmd/actionlint@v1.7.12
actionlint # reads .github/workflows/*.yml
gitlab-ci-local --list # validates .gitlab-ci.yml against GitLab's schema
name description stage when allow_failure environment needs
lint test on_success false
test test on_success false
build-image build on_success false
actionlint was quiet, which is what you want.
When each fits
Basically, I'd pick by where the code already lives. Moving code to get a nicer YAML is not worth it.
If you get to choose, GitHub Actions wins on the marketplace. Login, build-push, cache, setup for every language, it's a uses: line away. GitLab wins when you run your own runners and want one tool for code, registry, environments and deploy approvals without third party actions. Its pipeline graph and parent-child pipelines also handle big monorepos better, I think.
There we have it, same pipeline twice. Keep the job names away from GitLab keywords and the rest maps across quite easily.