DEV Community

loading...
Cover image for Migrate Molecule for Ansible pipeline from Travis.ci to Github Actions

Migrate Molecule for Ansible pipeline from Travis.ci to Github Actions

Dmitry Kotov
・3 min read

I decided to migrate from Travis.ci to GitHub actions because I had spent all credits and couldn't renew them.
Alt Text
Before migration my ci/cd configuration look like:

---
language: python
python: '3.6'

env:
  global:
    - MOLECULEW_USE_SYSTEM=true
  matrix:
    # Spin off separate builds for each of the following versions of Ansible
    - MOLECULEW_ANSIBLE=2.8.18
    - MOLECULEW_ANSIBLE=2.9.16
    - MOLECULEW_ANSIBLE=2.10.4

# Require Ubuntu 20.04
dist: focal

# Require Docker
services:
  - docker

install:
  # Install dependencies
  - ./moleculew wrapper-install

  # Display versions
  - ./moleculew wrapper-versions

script:
  - ./moleculew test

branches:
  only:
    - master
    - /^(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)([\.\-].*)?$/

notifications:
  webhooks: https://galaxy.ansible.com/api/v1/notifications/
Enter fullscreen mode Exit fullscreen mode

And the molecule configuration:

---
dependency:
  name: galaxy

driver:
  name: docker

lint: |
  set -e
  yamllint .
  ansible-lint
  flake8
platforms:
  - name: ansible-role-oh-my-zsh-debian-9
    image: debian:9
  - name: ansible-role-oh-my-zsh-debian-10
    image: debian:10
  - name: ansible-role-oh-my-zsh-ubuntu-16.04
    image: ubuntu:16.04
  - name: ansible-role-oh-my-zsh-ubuntu-18.04
    image: ubuntu:18.04
  - name: ansible-role-oh-my-zsh-ubuntu-20.04
    image: ubuntu:20.04
  - name: ansible-role-oh-my-zsh-centos-7
    image: centos:7
  - name: ansible-role-oh-my-zsh-centos-8
    image: centos:8
  - name: ansible-role-oh-my-zsh-fedora-32
    image: fedora:32
  - name: ansible-role-oh-my-zsh-fedora-33
    image: fedora:33
  - name: ansible-role-oh-my-zsh-opensuse-15.1
    image: opensuse/leap:15.1
  - name: ansible-role-oh-my-zsh-opensuse-15.2
    image: opensuse/leap:15.2

provisioner:
  name: ansible

verifier:
  name: testinfra
Enter fullscreen mode Exit fullscreen mode

These files available in my repository and actual for release version 2.4.1

Following GitHub Actions setup guide, I've created a file structure for my future pipeline

  .github/
    workflows/
      ci.yml
Enter fullscreen mode Exit fullscreen mode

And take like the example one's of geerlingguy repository I've made a action ci file:

--------
name: CI
'on':
  pull_request:
  push:
    branches:
      - master
jobs:
  test:
    name: Molecule
    runs-on: ubuntu-latest
    strategy:
      matrix:
        ansible:
          - 2.8.18
          - 2.9.16
          - 2.10.4
    steps:
      - name: Check out the codebase.
        uses: actions/checkout@v2
      - name: Set up Python 3.
        uses: actions/setup-python@v2
        with:
          python-version: '3.x'
      - name: Install test dependencies.
        run: pip3 install ansible==${{ matrix.ansible }}  molecule[docker] docker testinfra yamllint ansible-lint flake8
      - name: Run Molecule tests.
        run: molecule test
        env:
          PY_COLORS: '1'
          ANSIBLE_FORCE_COLOR: '1'

Enter fullscreen mode Exit fullscreen mode

Let's look closer at each part of this file.

First is the configuration that describes when we will be run our pipeline. It'll be run on all pull requests or push to master branch

--------
name: CI
'on':
  pull_request:
  push:
    branches:
      - master
Enter fullscreen mode Exit fullscreen mode

Next, is the jobs description. In my case, there is only one test. I gave the name for this job and setup os version ubuntu-latest. And the matrix part for the strategy is similar to the Travis.ci syntax.

jobs:
  test:
    name: Molecule
    runs-on: ubuntu-latest
    strategy:
      matrix:
        ansible:
          - 2.8.18
          - 2.9.16
          - 2.10.4
Enter fullscreen mode Exit fullscreen mode

Steps:

    steps:
      - name: Check out the codebase.
        uses: actions/checkout@v2
      - name: Set up Python 3.
        uses: actions/setup-python@v2
        with:
          python-version: '3.x'
      - name: Install test dependencies.
        run: pip3 install ansible==${{ matrix.ansible }}  molecule[docker] docker testinfra yamllint ansible-lint flake8
      - name: Run Molecule tests.
        run: molecule test
        env:
          PY_COLORS: '1'
          ANSIBLE_FORCE_COLOR: '1'
Enter fullscreen mode Exit fullscreen mode

This step executes the Github action that checkout repository.

      - name: Check out the codebase.
        uses: actions/checkout@v2
Enter fullscreen mode Exit fullscreen mode

The next one installs ansible.

      - name: Set up Python 3.
        uses: actions/setup-python@v2
Enter fullscreen mode Exit fullscreen mode

And step that installs dependencies.

      - name: Install test dependencies.
        run: pip3 install ansible==${{ matrix.ansible }}  molecule[docker] docker testinfra yamllint ansible-lint flake8
Enter fullscreen mode Exit fullscreen mode

ansible==${{ matrix.ansible }} - installs different ansible version for each environment.

The final step runs molecule

      - name: Run Molecule tests.
        run: molecule test
        env:
          PY_COLORS: '1'
          ANSIBLE_FORCE_COLOR: '1'

Enter fullscreen mode Exit fullscreen mode

PY_COLORS: '1': This forces Molecule to use the colorized output in the CI environment. Without it, all the output would be white text on a black background.
ANSIBLE_FORCE_COLOR: '1': This does the same thing as PY_COLORS, but for Ansible’s playbook output.

Discussion (0)