Migrating from Gitlab Ci/Cd pipeline to Github Actions

Migrating from Gitlab Ci/Cd pipeline to Github Actions

if you are thinking to move to GitHub workflows that might help you a little bit, I started looking for how to transfer the project I have from using the GitLab pipeline to working with GitHub workflow, so after a while from googling how can I make it done, the first result will come to you is the GitHub docs that taking about how to migrate, it's very helpful and I used it while I was transferring the ci pipeline.

GitHub Actions and GitLab CI/CD share several configuration similarities, which makes migrating to GitHub Actions relatively straightforward.

so if you look at the GitHub docs you will see that

  • GitLab pipeline is just a bunch of jobs running after each other running sequentially but GitHub jobs are running simultaneously

  • Runners are machines on which the jobs run. Both GitLab CI/CD and GitHub Actions offer managed and self-hosted variants of runners. In GitLab CI/CD, tags are used to run jobs on different platforms, while in GitHub Actions it is done with the runs-on key.

when u continue reading the docs, you will find a lot of similarities and the equivalent of each part in the YAML file for your GitLab ci like

  • Docker images
  • Condition and expression syntax
  • Dependencies between Jobs
  • Variables and secrets
  • Caching

I had like 10 jobs that are running in my automated ci pipeline going through checking the source code and code quality, linting, testing then building artifacts and saving them to our registry then deploying the last build image to our server.

I thought it will be a little challenging if I made something that will automate this process of transferring, so why not maybe also I can write about it and share the code script with other people so it might be helpful for someone else

at the first I thought it will be straight forward I'm just gonna manipulate the YAML file and change some keys and everything will be fine and that is what I did, I made a node script that read my GitLab ci file and then convert it to JSON object then I start to manipulate with the object until I made the new structure that fits with GitHub workflow then convert the JSON to new YAML file that I can use for GitHub workflow

The Script Link

you can use it from here it's openSource, you can modify it in the way it fits with your need, feel free to send pull requests as you want...

it will help you to convert easy jobs and it will work fine with you but it's not fully automating everything you should review its output, I just made it to satisfy my needs and generic as I can make it, but when you reach jobs like building and deploying, it had different flows for every project that you should look for it

GitHub workflows offer you their extensions GitHub actions, you can find a lot of them in the markeplace search for your use case and how to use the action which attributes it needs, it's very easy

I will list some of them that were very helpful for me

1- actions/checkout

  steps:
      - name: checkout repository
        uses: actions/checkout@v3
  • you will find it's the first step in your job is to checkout as you are running in a new machine that is empty and doesn't have your code yet, checkout action help to clone your repo into that runner machine so you can do whatever you want on the code there

2- docker/login-action

name: ci

on:
  push:
    branches: main

jobs:
  login:
    runs-on: ubuntu-latest
    steps:
      -
        name: Login to Docker Hub
        uses: docker/login-action@v2
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}
  • this action as you can see will help you to login into your registry so you can push your artifact image there

3- docker/build-push-action

name: ci

on:
  push:
    branches:
      - 'main'

jobs:
  docker:
    runs-on: ubuntu-latest
    steps:
      -
        name: Set up QEMU
        uses: docker/setup-qemu-action@v2
      -
        name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v2
      -
        name: Login to DockerHub
        uses: docker/login-action@v2
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}
      -
        name: Build and push
        uses: docker/build-push-action@v3
        with:
          push: true
          file: deploy/Dockerfile
          tags: user/app:latest
  • build-push-action will help you to build your docker image you can specify the place of the docker file that will build your image then it will push your build image to your registry

so now you can use the script to setup your basic jobs from code style and testing, then make a job using GitHub action to login to your registry and build the docker image now all you need to do is to create a deploy job: it's very customized to your case so I will leave this for you

some other tips that might help you

  • if you are working in an organization so you might have a lot of projects that have their own ci/cd pipeline on Github and also the org might work with a self-hosted runner as it may find that cheaper (like AWS EC2 servers on spot) so instead of creating a runner per project, you can make general runner per organization, of course not all the projects gonna always run their ci in the same time, also you can specify to make your general runner scalable with a limit to handle that case

  • you don't have to be restricted to one workflow like GitLab pipeline every job running there, GitHub allows you to run parrel workflow at the same time so you can separate your jobs into 3 or 4 workflows or more, you can specify one for build on staging other for production and one for deploying one for testing, one for specific conditions whatever you want to do you can easily do by creating new workflow..