Working directory context in github action with ansible playbook

Hi all,

I used to do automated deployments with an ansible playbook. The idea is basically to build a new docker image locally, tar it and send it over with ssh to the server. Load the image, kill the container if already running and start the new version. Basically this works for lots of static sites in an nginx docker container behind Traefik.

Now I want to embed the playbook in a GH action so on commits to master, the same route is followed. To easen this transition I’d like this route to be as similar as the “old” local playbook. Perhaps everything can be written as GH action, but I am not sure yet.

The problem

First, I use a ansible task to get metadata from a file. This works flawlessy, so using the local working directory seems OK.

- name: Get current working directory
      set_fact: "local_dir={{ lookup('env', 'PWD') }}"

    - name: Load project metadata
      include_vars: "{{ lookup('first_found', possible_files) }}"
      vars:
        possible_files:
          - "{{ local_dir }}/deploy.yml"
          - "{{ local_dir }}/.deploy.yml"
          - "{{ local_dir }}/.github/deploy.yml"
          - "{{ local_dir }}/.github/.deploy.yml"

Now another command is to build the image locally. I use ansible’s local_action:

- name: Build local docker image
      local_action: "command docker build -t {{ image_name }}:{{ project_tag }} {{ local_dir }}"
      register: out

Somehow, GH action’s environment can find the deploy.yml file but it’s not able to build the docker image as it says no file or directoy:

TASK [Build local docker image] ************************************************
task path: /github/workspace/.github/playbook.yml:47
fatal: [nuala.slui.mn -> localhost]: FAILED! => {"changed": false, "cmd": "docker build -t sluimn/slui.mn:202004181819 /github/workspace", "msg": "[Errno 2] No such file or directory", "rc": 2}

The GH action workflow is kept as simple as possible:

name: Deploy site to web server

on:
  push:
    branches: [master]

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@master

    - name: Execute Ansible Playbook
      uses: saubermacherag/ansible-playbook-docker-action@v1.3
      with:
        playbookName: .github/playbook.yml
        inventoryFile: .github/inventory
        keyFile: ".github/key.pem"
        keyFileVaultPass: ${{ secrets.KEYFILE_VAULT_PASS }}
        extraVars: "-e ansible_user=github -e ansible_become=true -e ansible_become_pass=${{ secrets.SSH_PASSWORD }}"
        verbosity: vv

The actions/checkout gives me my current repo and I can perform tasks with Ansible based on the repos content, but somehow I am not able to build a docker image inside the repo?

If there is any clue to get this done I’d love to hear! To be completely transparent, this is the repository: https://github.com/juriansluiman/slui.mn

Hi @juriansluiman ,

After checkout, the code are copied to ‘/home/runner/work/slui.mn/slui.mn’ which is your github workspace.

I assume the ‘docker build’ command cannot recognize ‘/github/workspace’. Please try to use relative/obsolute path for a try. 

Thanks.