2

I have a problem with github action and terraform.

I have fives repository on my github account. I'm able to clone/push/pull them from my computer thanks to my ssh key.

On my main.tf file, I've created some module when the source are my other repository :

module "1" {
    source   = "git@github.com:my_user/XXX.git"
}

module "2" {
    source   = "git@github.com:my_user/XXX.git"
}

module "3" {
    source   = "git@github.com:my_user/XXX.git"
}

module "4" {
    source   = "git@github.com:my_user/XXX.git"
}

If I execute these commands from my computer :

terraform init
terraform plan
terraform apply -auto-approve

Everything works well.

If I try make a github action, I've this error at the terraform init step :

│ git@github.com: Permission denied (publickey).
│ fatal: Could not read from remote repository.
│ 
│ Please make sure you have the correct access rights
│ and the repository exists.

Why I've this error while each repo are on the same github account ?

I've tried to create a github token with the full rights and I've putted it in the secrets section of my git and in the env section of my .yaml :

    env:
      GITHUB_TOKEN: ${{ secrets.TOKEN }}

But I've the same error.

There is someone to help me ?

Thanks a lot !

Morph59
  • 33
  • 3

2 Answers2

2

Github token can only do https based git authentication, but you're using SSH urls to git repos. You can configure git to automatically rewrite them for you with InsteadOf:

git config --global url."https://x-access-token:${GITHUB_TOKEN}@github.com/".insteadOf "git@github.com:"
erik258
  • 14,701
  • 2
  • 25
  • 31
  • If it's correct I think @Morph59 can also use the HTTPS address in the `source` field, like: `source = git::https://github.com/my_user/XXX.git` – Danilo Cacace Mar 06 '23 at 16:22
  • 1
    yeah possibly, but the existing ssh auth may be used in other places, namely locally – erik258 Mar 06 '23 at 16:24
0

you are using SSH syntax not HTTPS. so you must have a valid SSH key on your server.

Make sure you have this file on your server (if not create your SSH key) '/root/.ssh/id_rsa' and '/root/.ssh/id_rsa.pub'

Also make sure you either deployed 'id_rsa.pub' key under your user or under the repos you want to check out

NOTE: your SSH key name MUST BE 'id_rsa', do not use a different name, it won't work!!!

vedat
  • 1,193
  • 9
  • 10