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

total_seconds - Output Variable #335

Merged
merged 1 commit into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ As seen above, we have two steps. One for a noop deploy, and one for a regular d
| `approved_reviews_count` | The number of approved reviews on the pull request |
| `needs_to_be_deployed` | A comma separated list of environments that need successful and active deployments before the current environment (that was requested) can be deployed. This output is tied to the `enforced_deployment_order` input option - See the [enforced deployment order docs](./docs/enforced-deployment-order.md) for more details |
| `commit_verified` | The string `"true"` if the commit is verified, otherwise `"false"` |
| `total_seconds` | The total number of seconds that the deployment took to complete (Integer) |

## Custom Deployment Messages ✏️

Expand Down
11 changes: 8 additions & 3 deletions __tests__/functions/post-deploy-message.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var parsed_params
var deployment_end_time
var logs
var deployment_metadata
var total_seconds

function renderDeploymentMetadata(data) {
return dedent(`
Expand All @@ -39,7 +40,8 @@ function renderDeploymentMetadata(data) {
\t\t\t\t "deployment": {
\t\t\t\t "id": ${data.deployment_id ? parseInt(data.deployment_id) : null},
\t\t\t\t "timestamp": "${data.deployment_end_time}",
\t\t\t\t "logs": "${data.logs}"
\t\t\t\t "logs": "${data.logs}",
\t\t\t\t "duration": ${data.total_seconds}
\t\t\t\t },
\t\t\t\t "git": {
\t\t\t\t "branch": "${data.ref}",
Expand Down Expand Up @@ -98,6 +100,7 @@ beforeEach(() => {
_: ['LOG_LEVEL=debug']
})
deployment_end_time = '2024-01-01T00:00:00Z'
total_seconds = 27

context = {
actor: 'monalisa',
Expand Down Expand Up @@ -132,7 +135,8 @@ beforeEach(() => {
deployment_end_time: deployment_end_time,
actor: context.actor,
logs: logs,
commit_verified: false
commit_verified: false,
total_seconds: total_seconds
}

deployment_metadata = renderDeploymentMetadata(data)
Expand Down Expand Up @@ -323,6 +327,7 @@ test('successfully constructs a post deploy message with a custom markdown file'
- \`deployment_end_time\` - The end time of the deployment - this value is not _exact_ but it is very close (String)
- \`logs\` - The url to the logs of the deployment (String)
- \`commit_verified\` - Whether or not the commit was verified (Boolean)
- \`total_seconds\` - The total number of seconds the deployment took (String of a number)

Here is an example:

Expand All @@ -338,7 +343,7 @@ test('successfully constructs a post deploy message with a custom markdown file'

The deployment had the following "parsed" parameters provided in the deploy command: \`{"config":{"db":{"host":"localhost","port":5432}},"_":["LOG_LEVEL=debug"]}\`

The deployment process ended at \`2024-01-01T00:00:00Z\`.
The deployment process ended at \`2024-01-01T00:00:00Z\` and it took \`27\` seconds to complete.

Here are the deployment logs: https://github.com/corp/test/actions/runs/12345

Expand Down
3 changes: 2 additions & 1 deletion __tests__/functions/post-deploy.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ beforeEach(() => {
config: {db: {host: 'localhost', port: 5432}},
_: ['LOG_LEVEL=debug']
}),
commit_verified: false
commit_verified: false,
deployment_start_time: '2024-01-01T00:00:00Z'
}
})

Expand Down
3 changes: 2 additions & 1 deletion __tests__/functions/post.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ const validStates = {
parsed_params: JSON.stringify({
config: {db: {host: 'localhost', port: 5432}},
_: ['LOG_LEVEL=debug']
})
}),
deployment_start_time: '2024-01-01T00:00:00Z'
}

const setFailedMock = jest.spyOn(core, 'setFailed').mockImplementation(() => {})
Expand Down
4 changes: 4 additions & 0 deletions __tests__/schemas/action.schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -617,3 +617,7 @@ outputs:
description:
type: string
required: true
total_seconds:
description:
type: string
required: true
3 changes: 2 additions & 1 deletion __tests__/templates/test_deployment_message.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ The following variables are available to use in this template:
- `deployment_end_time` - The end time of the deployment - this value is not _exact_ but it is very close (String)
- `logs` - The url to the logs of the deployment (String)
- `commit_verified` - Whether or not the commit was verified (Boolean)
- `total_seconds` - The total number of seconds the deployment took (String of a number)

Here is an example:

Expand All @@ -32,7 +33,7 @@ The deployment had the following parameters provided in the deploy command: `{{

The deployment had the following "parsed" parameters provided in the deploy command: `{{ parsed_params | safe }}`

The deployment process ended at `{{ deployment_end_time }}`.
The deployment process ended at `{{ deployment_end_time }}` and it took `{{ total_seconds }}` seconds to complete.

Here are the deployment logs: {{ logs }}

Expand Down
2 changes: 2 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,8 @@ outputs:
description: 'A comma separated list of environments that need successful and active deployments before the current environment (that was requested) can be deployed. This output is tied to the "enforced_deployment_order" input option.'
commit_verified:
description: 'The string "true" if the commit has a verified signature, otherwise "false"'
total_seconds:
description: 'The total number of seconds that the deployment took to complete (Integer)'
runs:
using: "node20"
main: "dist/index.js"
Expand Down
35 changes: 31 additions & 4 deletions dist/index.js

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

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions src/functions/post-deploy-message.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import nunjucks from 'nunjucks'
// - attribute: parsed_params: A string representation of the parsed deployment parameters (String)
// - attribute: deployment_end_time: The time the deployment ended - this value is not _exact_ but it is very close (String)
// - attribute: commit_verified: Indicates whether the commit is verified or not (Boolean)
// - attribute: total_seconds: The total amount of seconds that the deployment took (Int)
// :returns: The formatted message (String)
export async function postDeployMessage(context, data) {
// fetch the inputs
Expand All @@ -47,7 +48,8 @@ export async function postDeployMessage(context, data) {
deployment_end_time: data.deployment_end_time,
actor: context.actor,
logs: `${process.env.GITHUB_SERVER_URL}/${context.repo.owner}/${context.repo.repo}/actions/runs/${process.env.GITHUB_RUN_ID}`,
commit_verified: data.commit_verified
commit_verified: data.commit_verified,
total_seconds: data.total_seconds
}

// this is kinda gross but wrangling dedent() and nunjucks is a pain
Expand All @@ -66,7 +68,8 @@ export async function postDeployMessage(context, data) {
\t\t\t\t "deployment": {
\t\t\t\t "id": ${vars.deployment_id},
\t\t\t\t "timestamp": "${vars.deployment_end_time}",
\t\t\t\t "logs": "${vars.logs}"
\t\t\t\t "logs": "${vars.logs}",
\t\t\t\t "duration": ${vars.total_seconds}
\t\t\t\t },
\t\t\t\t "git": {
\t\t\t\t "branch": "${vars.ref}",
Expand Down
24 changes: 23 additions & 1 deletion src/functions/post-deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const nonStickyMsg = `🧹 ${COLORS.highlight}non-sticky${COLORS.reset} lock det
// - attribute: params: The raw string of deployment parameters (String)
// - attribute: parsed_params: A string representation of the parsed deployment parameters (String)
// - attribute: commit_verified: Indicates whether the commit is verified or not (Boolean)
// - attribute: deployment_start_time: The timestamp of when the deployment started (String)
// :returns: 'success' if the deployment was successful, 'success - noop' if a noop, throw error otherwise
export async function postDeploy(context, octokit, data) {
// check the inputs to ensure they are valid
Expand All @@ -50,6 +51,16 @@ export async function postDeploy(context, octokit, data) {
const deployment_end_time = now.toISOString()
core.debug(`deployment_end_time: ${deployment_end_time}`)

// calculate the total amount of seconds that the deployment took
const total_seconds = calculateDeploymentTime(
data.deployment_start_time,
deployment_end_time
)
core.info(
`🕒 deployment completed in ${COLORS.highlight}${total_seconds}${COLORS.reset} seconds`
)
core.setOutput('total_seconds', total_seconds)

const message = await postDeployMessage(context, {
environment: data.environment,
environment_url: data.environment_url,
Expand All @@ -64,7 +75,8 @@ export async function postDeploy(context, octokit, data) {
params: data.params,
parsed_params: data.parsed_params,
deployment_end_time: deployment_end_time,
commit_verified: data.commit_verified
commit_verified: data.commit_verified,
total_seconds: total_seconds
})

// update the action status to indicate the result of the deployment as a comment
Expand Down Expand Up @@ -253,3 +265,13 @@ function validateInputs(data) {
additionalInputs.forEach(input => validateInput(data[input], input))
}
}

// Helper function to calculate the deployment time in seconds
// :param start_time: The timestamp of when the deployment started (String)
// :param end_time: The timestamp of when the deployment ended (String)
// :returns: The total amount of seconds that the deployment took (Integer) - rounded to the nearest second
function calculateDeploymentTime(start_time, end_time) {
const start = new Date(start_time)
const end = new Date(end_time)
return Math.round((end - start) / 1000)
}
3 changes: 2 additions & 1 deletion src/functions/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ export async function post() {
'skip_successful_deploy_labels_if_approved'
)
},
commit_verified: core.getState('commit_verified') === 'true'
commit_verified: core.getState('commit_verified') === 'true',
deployment_start_time: core.getState('deployment_start_time')
}

core.info(`🧑‍🚀 commit SHA: ${COLORS.highlight}${data.sha}${COLORS.reset}`)
Expand Down
1 change: 1 addition & 0 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,7 @@ export async function run() {
const now = new Date()
const deployment_start_time = now.toISOString()
core.debug(`deployment_start_time: ${deployment_start_time}`)
core.saveState('deployment_start_time', deployment_start_time)

const commentBody = dedent(`
### Deployment Triggered 🚀
Expand Down
Loading