Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[question] How to authenticate in GitHub Action using conan.tools.scm.Git #13167

Closed
1 task
daniel-eiband-snkeos opened this issue Feb 19, 2023 · 6 comments
Closed
1 task

Comments

@daniel-eiband-snkeos
Copy link

What is your question?

I'm trying to refactor my recipes and make them ready for Conan 2.0. The migration guide has an example that shows the usage of conan.tools.scm.Git. However when executing conan create inside a GitHub action of a private repository with the new Git helper then I get the following error:

ERROR: 
	CalledProcessErrorWithStderr: Command 'git clone "https://github.com/xxx/xxx"  .' returned non-zero exit status 128.
Cloning into '.'...
fatal: could not read Username for 'https://github.com/': No such device or address

What is the recommended way of passing the GITHUB_TOKEN secret or authenticate in general?

Have you read the CONTRIBUTING guide?

  • I've read the CONTRIBUTING guide
@memsharded
Copy link
Member

Hi @daniel-eiband-snkeos

Uhm, this is interesting. I don't know much about Github actions yet, I would assume that it would be possible to access the same repo that is being processed, but apparently it is not possible by default.
I think we can reduce the problem and eliminate Conan from the equation, and the question would be how to do a git clone of the same repo is processing directly from the Github action.

I see Github actions can define something like

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

But that would work only if passed in the url like https://${TOKEN}:[email protected], but this is not what Conan scm is doing, it will capture the repo URL, not including such a expression.

In general this feature assumes that there is already a git system authentication, like using ssh-keys or something like that. Ephemeral CIs don't work this way, this is definitely something we should investigate a bit further.

Any Github action experienced user can provide some ideas?

@daniel-eiband-snkeos
Copy link
Author

daniel-eiband-snkeos commented Feb 19, 2023

I just implemented the following workaround:

    def source(self):
        token = os.environ.get('GITHUB_TOKEN')
        sources = self.conan_data['sources']

        git = Git(self)
        git.fetch_commit(
            url=sources['url'] if not token else \
                sources['url'].replace(
                    'https://github.com',
                    'https://oauth2:{}@github.com'.format(token)
                ),
            commit=sources['commit']
        )

This works when you set the token secret as environment variable of the job:

jobs:
  conan:
    name: Conan package
    env:
      GITHUB_TOKEN: ${{ github.token }}

${{ github.token }} is equivalent to ${{ secrets.GITHUB_TOKEN }}.

@memsharded
Copy link
Member

Great point @daniel-eiband-snkeos , thanks for sharing.
Indeed, this is why the old scm feature is now split in exports() and source(), because it allows this kind of customization and flexibility. It might not be great to require it, but at least it is possible.

I'd still love to learn if there is some Github action built-in way so it is possible to clone using the bare URL without auth.

@memsharded memsharded modified the milestones: 1.60, 1.61 May 8, 2023
@memsharded memsharded modified the milestones: 1.61, 1.62 Sep 11, 2023
@franramirez688 franramirez688 modified the milestones: 1.62, 1.63 Nov 7, 2023
@memsharded
Copy link
Member

This has been inactive for a while, but I'd still like to learn if there are other possibilities for GH actions there.
As Conan 2.0 is already 1 year old, I am taking this to the Conan 2.0 train, as it will get a bit more attention there, thanks!

@memsharded memsharded modified the milestones: 1.63, 2.2 Feb 12, 2024
@memsharded memsharded modified the milestones: 2.2.0, 2.3.0 Mar 15, 2024
@memsharded memsharded self-assigned this Apr 15, 2024
@memsharded
Copy link
Member

Hi all,

I am following up on this. The docs page in https://docs.conan.io/2/examples/tools/scm/git/capture_scm/git_capture_scm.html#credentials-management was added in conan-io/docs#3112.

I have just tried this and it seems to work great:

  • Uses modern and recommended mechanism like Github deploys keys, with read-only default
  • Requires no modification to recipes at all the basic:
        def export(self):
          git = Git(self, self.recipe_folder)
          git.coordinates_to_conandata()
    
      def source(self):
          git = Git(self)
          git.checkout_from_conandata_coordinates()
    works fine

I think we can close this ticket as solved? What do you think? Thanks for your feedback!

@memsharded
Copy link
Member

I have checked with the team, our conclusions:

  • We recommend using ssh keys everywhere, it is transparent, clean, the default clone creds in most popular sites as Github, Gitlab, etc.
  • It requires 0 modifications to recipes
  • Conan is completely unaware of the auth, so it cannot leak or mess with the credentials by accident

This scm approach can also work well with gitconfig as described in conan-io/docs#3683, configuring credentials helpers and even switching from git-https urls.

Trying to provide a built-in feature that uses in-url tokens or the like is kind of risky, so it wouldn't be planned at this moment.

I am closing this ticket as solved, if there is any pending question or new feedback, I'd recommend to create a new one. Thanks very much for your feedback!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment