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

How to use "git push" ? #28

Closed
zhengjian2016 opened this issue Oct 22, 2020 · 7 comments
Closed

How to use "git push" ? #28

zhengjian2016 opened this issue Oct 22, 2020 · 7 comments

Comments

@zhengjian2016
Copy link

Hi,
Thanks for [git-lambda-layer]!
I want to build a function to commit something to my github.
Under the layer, I can clone/add/commit in /tmp/my-repository folder, but when I try to push the change to origin branch, I run into below issue. Could you help me to resolve the issue?

error:
...
Could not create directory '/home/sbx_user1051/.ssh'.
Host key verification failed.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

...

This is my code

_const { execSync } = require('child_process')
const process = require('process')
const { GITHUB_TOKEN, GITHUB_USERNAME, GITHUB_EMAIL, GITHUB_REPO, PASSWD } = process.env
exports.handler = async (event) => {
const config = {
token: GITHUB_TOKEN,
username: GITHUB_USERNAME,
email: GITHUB_EMAIL,
repo: GITHUB_REPO,
password: PASSWD
}
const gitUrl = github.com/${config.username}/${config.repo}.git
const execOpt = { encoding: 'utf8', stdio: 'inherit' }

//execSync(`git config --global user.email ${config.email}`, execOpt)
//execSync(`git config --global user.name ${config.username}`, execOpt)

execSync(`rm -rf /tmp/${config.repo}`, execOpt)
execSync(`cd /tmp && git clone https://${gitUrl} \

&& ls && cd SI-AutomationTest && echo testxxx>test.txt
&& git config --local user.email ${config.email}
&& git config --local user.name ${config.username}
&& git add test.txt
&& git branch
&& git config --local user.email ${config.email}
&& git config --local user.name ${config.username}
&& git commit -m '[skip ci] hhhaa'
&& git remote -v
&& git config --local user.email ${config.email}
&& git config --local user.name ${config.username}
&& git remote set-url origin git@github.com:${config.username}/${config.rep}.git
&& git config --local user.email ${config.email}
&& git config --local user.name ${config.username}
&& git push -u origin main`, execOpt)
}_

@mhart
Copy link
Member

mhart commented Oct 22, 2020

The problem is you're setting the origin using ssh:

git remote set-url origin git@github.com:${config.username}/${config.rep}.git

Just remove that line

@zhengjian2016
Copy link
Author

Hi mhart,
Thanks for your quickly response!
I tried it, but not work like below
fatal: could not read Username for 'https://github.com': No such file or directory

@zhengjian2016
Copy link
Author

Hi mhart,
Thanks for your quickly response!
I tried it, but not work like below
fatal: could not read Username for 'https://github.com': No such file or directory

When change to below

git push -u git@github.com:${config.username}/${config.rep}.git main`, execOpt)

I got another error:

Could not create directory '/home/sbx_user1051/.ssh'.
Host key verification failed.
fatal: Could not read from remote repository.
Please make sure you have the correct access rights and the repository exists.

@mhart
Copy link
Member

mhart commented Oct 22, 2020

Again, you're trying to push using an SSH address – you should be using the HTTP address that you cloned the repo with

@mhart
Copy link
Member

mhart commented Oct 22, 2020

See here for how to use an access token: https://docs.github.com/en/free-pro-team@latest/github/authenticating-to-github/creating-a-personal-access-token

This isn't an issue with this layer – you would have this issue no matter where you're trying to clone or push from, so I'm going to close this

@mhart mhart closed this as completed Oct 22, 2020
@mhart
Copy link
Member

mhart commented Oct 22, 2020

And it's easier to include the token when you clone – then you don't need to enter it again. https://github.blog/2012-09-21-easier-builds-and-deployments-using-git-over-https-and-oauth/#using-oauth-with-git

@zhengjian2016
Copy link
Author

And it's easier to include the token when you clone – then you don't need to enter it again. https://github.blog/2012-09-21-easier-builds-and-deployments-using-git-over-https-and-oauth/#using-oauth-with-git

Thank you very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants