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

chore: removing aws token validations #221

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
27 changes: 27 additions & 0 deletions .github/scripts/trigger-and-wait.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const { spawn } = require('child_process');

const triggerAndWait = async () => {
const scriptPath = '.github/scripts/trigger-and-wait.sh';
const child = spawn('bash', [scriptPath]);

child.stdout.on('data', (data) => {
console.log(`stdout: ${data}`)
})

child.stderr.on('data', (data) => {
console.log(`stderr: ${data}`)
})

child.on('data', (data) => {
console.log(`error: ${data}`)
})

child.on('exit', (code, signal) => {
if (code) console.log(`Process exit with code: ${code}`)
if (signal) console.log(`Process killed with signal: ${signal}`)
})
}

module.exports = {
triggerAndWait
};
156 changes: 156 additions & 0 deletions .github/scripts/trigger-and-wait.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
#!/bin/sh

#Functionality from convictional/trigger-workflow-and-wait.
#Link: https://github.com/convictional/trigger-workflow-and-wait

usage_docs() {
echo ""
echo " owner: twilio"
echo " repo: twilio-cli-core"
echo " github_token: \${{ secrets.GITHUB_PERSONAL_ACCESS_TOKEN }}"
echo " workflow_file_name: main.yaml"
}

validate_args() {
wait_interval=10 # Waits for 10 seconds
if [ "${INPUT_WAITING_INTERVAL}" ]
then
wait_interval=${INPUT_WAITING_INTERVAL}
fi

propagate_failure=true
if [ -n "${INPUT_PROPAGATE_FAILURE}" ]
then
propagate_failure=${INPUT_PROPAGATE_FAILURE}
fi

trigger_workflow=true
if [ -n "${INPUT_TRIGGER_WORKFLOW}" ]
then
trigger_workflow=${INPUT_TRIGGER_WORKFLOW}
fi

wait_workflow=true
if [ -n "${INPUT_WAIT_WORKFLOW}" ]
then
wait_workflow=${INPUT_WAIT_WORKFLOW}
fi

if [ -z "${INPUT_OWNER}" ]
then
echo "Error: Owner is a required argument."
usage_docs
exit 1
fi

if [ -z "${INPUT_REPO}" ]
then
echo "Error: Repo is a required argument."
usage_docs
exit 1
fi

if [ -z "${INPUT_GITHUB_TOKEN}" ]
then
echo "Error: Github token is required. You can head over settings and"
echo "under developer, you can create a personal access tokens. The"
echo "token requires repo access."
usage_docs
exit 1
fi

if [ -z "${INPUT_WORKFLOW_FILE_NAME}" ]
then
echo "Error: Workflow File Name is required"
usage_docs
exit 1
fi

inputs=$(echo '{}' | jq)
if [ "${INPUT_INPUTS}" ]
then
inputs=$(echo "${INPUT_INPUTS}" | jq)
fi

ref="main"
if [ "$INPUT_REF" ]
then
ref="${INPUT_REF}"
fi
}

trigger_workflow() {
echo "https://api.github.com/repos/${INPUT_OWNER}/${INPUT_REPO}/actions/workflows/${INPUT_WORKFLOW_FILE_NAME}/dispatches"

curl -X POST "https://api.github.com/repos/${INPUT_OWNER}/${INPUT_REPO}/actions/workflows/${INPUT_WORKFLOW_FILE_NAME}/dispatches" \
-H "Accept: application/vnd.github.v3+json" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${INPUT_GITHUB_TOKEN}" \
--data "{\"ref\":\"${ref}\",\"inputs\":${inputs}}"

# Sleep after triggering workflow so it can be polled for status
echo "Sleeping for $wait_interval seconds"
sleep $wait_interval
}

wait_for_workflow_to_finish() {
# Find the id of the last build
last_workflow=$(curl -X GET "https://api.github.com/repos/${INPUT_OWNER}/${INPUT_REPO}/actions/workflows/${INPUT_WORKFLOW_FILE_NAME}/runs" \
-H 'Accept: application/vnd.github.antiope-preview+json' \
-H "Authorization: Bearer ${INPUT_GITHUB_TOKEN}" | jq '[.workflow_runs[]] | first')
last_workflow_id=$(echo "${last_workflow}" | jq '.id')
last_workflow_url="https://github.com/${INPUT_OWNER}/${INPUT_REPO}/actions/runs/${last_workflow_id}"
echo "The workflow id is [${last_workflow_id}]."
echo "The workflow logs can be found at ${last_workflow_url}"
echo "::set-output name=workflow_id::${last_workflow_id}"
echo "::set-output name=workflow_url::${last_workflow_url}"
echo ""
conclusion=$(echo "${last_workflow}" | jq '.conclusion')
status=$(echo "${last_workflow}" | jq '.status')

while [[ "${conclusion}" == "null" && "${status}" != "\"completed\"" ]]
do
echo "Sleeping for \"${wait_interval}\" seconds"
sleep "${wait_interval}"
workflow=$(curl -X GET "https://api.github.com/repos/${INPUT_OWNER}/${INPUT_REPO}/actions/workflows/${INPUT_WORKFLOW_FILE_NAME}/runs" \
-H 'Accept: application/vnd.github.antiope-preview+json' \
-H "Authorization: Bearer ${INPUT_GITHUB_TOKEN}" | jq '.workflow_runs[] | select(.id == '${last_workflow_id}')')
conclusion=$(echo "${workflow}" | jq '.conclusion')
status=$(echo "${workflow}" | jq '.status')
echo "Checking conclusion [${conclusion}]"
echo "Checking status [${status}]"
done

if [[ "${conclusion}" == "\"success\"" && "${status}" == "\"completed\"" ]]
then
echo "Yes, success"
else
# Alternative "failure"
echo "Conclusion is not success, its [${conclusion}]."
if [ "${propagate_failure}" = true ]
then
echo "Propagating failure to upstream job"
exit 1
fi
fi
}

main() {
validate_args

if [ "${trigger_workflow}" = true ]
then
trigger_workflow
else
echo "Skipping triggering the workflow."
fi

if [ "${wait_workflow}" = true ]
then
wait_for_workflow_to_finish
else
echo "Skipping waiting for workflow."
fi
}

main
43 changes: 9 additions & 34 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ on:
description: 'HomeBrew prerelease'
default: 'false'
jobs:
cli-core-token-validation:
cli-token-validation:
runs-on: ubuntu-latest
steps:
- name: Checkout cli-core repo
Expand All @@ -23,44 +23,19 @@ jobs:
- name: Extract branch name
id: extract_branch
run: echo "##[set-output name=branch;]$(echo ${GITHUB_REF#refs/heads/})"
- name: Trigger CLI token validation workflow
run: |
fileName="$GITHUB_WORKSPACE/.github/scripts/trigger-workflow.js"
node -e "require('$fileName').triggerWorkflow()"
env:
WORKFLOW_NAME: '.github/workflows/release-token-validation.yml'
REPO_NAME: ${{ github.repository_owner }}/twilio-cli
REPO_ACCESS_TOKEN: ${{ secrets.REPO_ACCESS_TOKEN }}
BRANCH_NAME: ${{steps.extract_branch.outputs.branch}}
- name: Validate REPO_ACCESS_TOKEN
uses: actions/checkout@v2
with:
repository: '${{ github.repository_owner }}/twilio-oai'
token: ${{ secrets.REPO_ACCESS_TOKEN }}
- name: Validate AWS tokens
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-session-token: ${{ secrets.AWS_SESSION_TOKEN }}
aws-region: us-east-1

cli-token-validation:
needs: [ cli-core-token-validation ]
runs-on: ubuntu-latest
steps:
- name: Checkout cli-core repo
uses: actions/checkout@v2
- name: Execute py script to validate twilio-cli tokens
id: cli_token
run: |
output=$(python3 .github/scripts/validate_cli_tokens.py)
echo "::set-output name=tokenStatus::$output"
- name: Print status
run: echo "${{ steps.cli_token.outputs.tokenStatus }}"
- name: Validate the github workflow
if: ${{ steps.cli_token.outputs.tokenStatus != 'success'}}
run: exit 1
- name: Validate AWS token in twilio-cli project
run: node .github/scripts/trigger-and-wait.js
env:
INPUT_OWNER: ${{ github.repository_owner }}
INPUT_REPO: twilio-cli
INPUT_GITHUB_TOKEN: ${{ secrets.REPO_ACCESS_TOKEN }}
INPUT_WORKFLOW_FILE_NAME: release-token-validation.yml
INPUT_WAITING_INTERVAL: 10

notify-start:
needs: [ cli-token-validation ]
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ module.exports = {
releaseScripts: {
UpdateRelease: require('../.github/scripts/update-release'),
TriggerWorkflow: require('../.github/scripts/trigger-workflow'),
TriggerWaitWorkflow: require('../.github/scripts/trigger-and-wait'),
},
};