Skip to content

Commit

Permalink
feat(deploy): support tag option
Browse files Browse the repository at this point in the history
  • Loading branch information
aurrelhebert committed Feb 16, 2024
1 parent ab16b54 commit 52ea270
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 13 deletions.
8 changes: 7 additions & 1 deletion bin/clever.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,12 @@ function run () {
metavar: 'commit id',
description: 'Restart the application with a specific commit ID',
}),
gitTag: cliparse.option('tag', {
aliases: ['t'],
default: '',
metavar: 'tag',
description: 'Tag to push (none by default)',
}),
databaseId: cliparse.option('database-id', {
metavar: 'database_id',
description: 'The Database ID (e.g.: postgresql_xxx)',
Expand Down Expand Up @@ -646,7 +652,7 @@ function run () {
const deploy = lazyRequirePromiseModule('../src/commands/deploy.js');
const deployCommand = cliparse.command('deploy', {
description: 'Deploy an application',
options: [opts.alias, opts.branch, opts.quiet, opts.forceDeploy, opts.followDeployLogs, opts.sameCommitPolicy],
options: [opts.alias, opts.branch, opts.gitTag, opts.quiet, opts.forceDeploy, opts.followDeployLogs, opts.sameCommitPolicy],
}, deploy('deploy'));

// DIAG COMMAND
Expand Down
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"common-env": "^6.4.0",
"curlconverter": "^3.21.0",
"eventsource": "^1.1.0",
"isomorphic-git": "^1.25.0",
"isomorphic-git": "^1.25.3",
"linux-release-info": "^3.0.0",
"lodash": "^4.17.21",
"mkdirp": "^1.0.4",
Expand Down
23 changes: 20 additions & 3 deletions src/commands/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ const { sendToApi } = require('../models/send-to-api.js');
// Once the API call to redeploy() has been triggered successfully,
// the rest (waiting for deployment state to evolve and displaying logs) is done with auto retry (resilient to network failures)
async function deploy (params) {
const { alias, branch: branchName, quiet, force, follow, 'same-commit-policy': sameCommitPolicy } = params.options;
const { alias, branch: branchName, tag: tagName, quiet, force, follow, 'same-commit-policy': sameCommitPolicy } = params.options;

const appData = await AppConfig.getAppDetails({ alias });
const { ownerId, appId } = appData;
const branchRefspec = await git.getFullBranch(branchName);

const branchRefspec = await getBranchToDeploy(branchName, tagName);
const commitIdToPush = await git.getBranchCommit(branchRefspec);
const remoteHeadCommitId = await git.getRemoteCommit(appData.deployUrl);
const deployedCommitId = await Application.get(ownerId, appId)
Expand Down Expand Up @@ -66,7 +66,8 @@ async function deploy (params) {
const knownDeployments = await getAllDeployments({ id: ownerId, appId, limit: 5 }).then(sendToApi);

Logger.println('Pushing source code to Clever Cloud…');
await git.push(appData.deployUrl, branchRefspec, force)

await git.push(appData.deployUrl, commitIdToPush, force)
.catch(async (e) => {
const isShallow = await git.isShallow();
if (isShallow) {
Expand All @@ -86,4 +87,20 @@ async function restartOnSameCommit (ownerId, appId, commitIdToPush, quiet, follo
return Log.watchDeploymentAndDisplayLogs({ ownerId, appId, deploymentId: restart.deploymentId, quiet, follow });
}

async function getBranchToDeploy (branchName, tagName) {
if (tagName) {
const useTag = await git.isExistingTag(tagName);
if (useTag) {
const tagRefspec = await git.getFullBranch(tagName);
return tagRefspec;
}
else {
throw new Error(`Tag ${tagName} doesn't exist locally`);
}
}
else {
return await git.getFullBranch(branchName);
}
}

module.exports = { deploy };
15 changes: 14 additions & 1 deletion src/models/git.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,19 @@ async function getFullBranch (branchName) {

async function getBranchCommit (refspec) {
const repo = await getRepo();
return git.resolveRef({ ...repo, ref: refspec });
const oid = await git.resolveRef({ ...repo, ref: refspec });
// When a refspec refers to an annotated tag, the OID ref represents the annotation and not the commit directly,
// that's why we need a call to `readCommit`.
const res = await git.readCommit({ ...repo, ref: refspec, oid });
return res.oid;
}

async function isExistingTag (tag) {
const repo = await getRepo();
const tags = await git.listTags({
...repo,
});
return tags.includes(tag);
}

async function push (remoteUrl, branchRefspec, force) {
Expand Down Expand Up @@ -131,4 +143,5 @@ module.exports = {
push,
completeBranches,
isShallow,
isExistingTag,
};

0 comments on commit 52ea270

Please sign in to comment.