From 15f4be698f25fc8fa0c6e76669ea8fe515850765 Mon Sep 17 00:00:00 2001 From: Eedrah Date: Fri, 30 Nov 2018 04:36:27 +1300 Subject: [PATCH] add option for branch --- README.markdown | 3 ++- args.js | 6 +++++- lib.js | 12 ++++++------ 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/README.markdown b/README.markdown index 95c572a..9ec7d2b 100644 --- a/README.markdown +++ b/README.markdown @@ -22,6 +22,7 @@ gitlab-le \ --production `# OPTIONAL - Obtain a real certificate instead of a dummy one and configure your repository to use it` --path `# OPTIONAL - Absolute path in your repository where challenge files should be uploaded` --jekyll `# OPTIONAL - Upload challenge files with a Jekyll-compatible YAML front matter` \ +--branch `# OPTIONAL - Upload challenge files to this branch, default is the main branch of the repository` \ ``` See `gitlab-le --help` for more details. @@ -77,4 +78,4 @@ However, GitLab does not provide a way to automatically renew certificates, so t ## Automation -Since 10.2, GitLab provides an API to configure HTTPS certificates on a GitLab page, which means `gitlab-le` can be configured to obtain new certificates when your existing ones are about to expire. \ No newline at end of file +Since 10.2, GitLab provides an API to configure HTTPS certificates on a GitLab page, which means `gitlab-le` can be configured to obtain new certificates when your existing ones are about to expire. diff --git a/args.js b/args.js index e9749a9..afca4d1 100755 --- a/args.js +++ b/args.js @@ -34,11 +34,15 @@ module.exports = yargs describe: 'Obtain a real certificate instead of a dummy one and configure your repository to use it', type: 'boolean', default: false + }).option('branch', { + describe: 'Select the branch where the challenge files should be uploaded.', + type: 'string', + default: '' }).example('$0 --domain example.com www.example.com --email rolodato@example.com --repository https://gitlab.com/foo/example.gitlab.io --token abc123', 'Simple build where all files are served from public/ inside your repository') .example('$0 --jekyll --path / --domain example.com --email rolodato@example.com --repository https://gitlab.example.com/foo/myrepo --token abc123', 'Jekyll website that serves all valid files in your repository\'s root directory') .wrap(yargs.terminalWidth()) .check(argv => { - const empty = Object.keys(argv).filter(key => key !== '_' && argv[key].length == 0); + const empty = Object.keys(argv).filter(key => key !== '_' && key !== 'branch' && argv[key].length == 0); if (empty.length > 0) { console.error(`Missing required arguments: ${empty.join(', ')}`); process.exit(1); diff --git a/lib.js b/lib.js index bcfb22b..bcbc8b1 100644 --- a/lib.js +++ b/lib.js @@ -47,7 +47,7 @@ module.exports = (options) => { }); }; - const uploadChallenge = (key, value, repo, domain) => { + const uploadChallenge = (key, value, repo, domain, branch) => { const challengeContent = options.jekyll ? `---\nlayout: null\npermalink: /.well-known/acme-challenge/${key}\n---\n${value}` : value; // Need to bluebird-ify to use .asCallback() @@ -56,20 +56,20 @@ module.exports = (options) => { url: `/projects/${repo.id}/repository/files/${filePath}`, body: { commit_message: 'Automated Let\'s Encrypt renewal: add challenge', - branch: repo.default_branch, + branch: branch || repo.default_branch, content: challengeContent, author_name: 'gitlab-le' } })).return([`http://${domain}/.well-known/acme-challenge/${key}`, value]); }; - const deleteChallenges = (key, repo) => { + const deleteChallenges = (key, repo, branch) => { const filePath = encodeURIComponent(path.posix.resolve('/', options.path, key)); return Promise.resolve(gitlabRequest.delete({ url: `/projects/${repo.id}/repository/files/${filePath}`, body: { commit_message: 'Automated Let\'s Encrypt renewal: remove challenge', - branch: repo.default_branch, + branch: branch || repo.default_branch, author_name: 'gitlab-le' } })); @@ -151,13 +151,13 @@ module.exports = (options) => { domains: options.domain, setChallenge: (hostname, key, value, cb) => { return Promise.resolve(deleteChallengesPromise) - .then(() => uploadChallenge(key, value, repo, hostname)) + .then(() => uploadChallenge(key, value, repo, hostname, options.branch)) .tap(res => console.log(`Uploaded challenge file, polling until it is available at ${res[0]}`)) .spread(pollUntilDeployed) .asCallback(cb); }, removeChallenge: (hostname, key, cb) => { - return (deleteChallengesPromise = deleteChallenges(key, repo)).finally(() => cb(null)); + return (deleteChallengesPromise = deleteChallenges(key, repo, options.branch)).finally(() => cb(null)); } }); }).tap(cert =>