Skip to content

Commit

Permalink
huh...
Browse files Browse the repository at this point in the history
  • Loading branch information
kentcdodds committed Mar 18, 2021
1 parent 5e795cc commit 822a168
Show file tree
Hide file tree
Showing 5 changed files with 203 additions and 37 deletions.
18 changes: 18 additions & 0 deletions .github/actions/get-last-commit-hash/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: 'Get Last Commit Hash'
description: 'Gets the commit hash of the most recent successful run.'
runs:
using: 'node12'
main: 'index.js'
inputs:
workflow_name:
description: The name of the workflow in question
required: true
branch:
description: The name of the branch
required: true
github_token:
description: The GitHub Token
required: true
outputs:
commit_hash: # id of output
description: 'The commit hash of the most recent successful run'
52 changes: 52 additions & 0 deletions .github/actions/get-last-commit-hash/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const core = require('@actions/core')
const github = require('@actions/github')

async function go() {
const {GITHUB_RUN_ID} = process.env

const octokit = github.getOctokit(core.getInput('github_token'))
const {
repo: {owner, repo},
} = github.context

const {data: current_run} = await octokit.actions.getWorkflowRun({
owner,
repo,
run_id: Number(GITHUB_RUN_ID),
})

const {
data: {workflow_runs},
} = await octokit.actions.listWorkflowRuns({
owner,
repo,
workflow_id: String(current_run.workflow_id),
status: 'success',
branch: current_run.head_branch,
event: 'push',
})

const headCommits = workflow_runs.map(run => {
return run.head_commit
})

const sortedHeadCommits = headCommits.sort((a, b) => {
const dateA = new Date(a.timestamp)
const dateB = new Date(b.timestamp)
if (dateA < dateB) return -1
if (dateA > dateB) return 1
return 0
})

const lastSuccessCommitHash =
sortedHeadCommits[sortedHeadCommits.length - 1].id

core.setOutput('commit_hash', lastSuccessCommitHash)
}

go().then(
() => core.info('Get Last Commit Hash Complete.'),
error => {
core.setFailed(error.message)
},
)
23 changes: 23 additions & 0 deletions .github/actions/skip-deploy/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: 'Skip deploy'
description: 'Outputs a variable to indicate whether deploy should be skipped'
runs:
using: 'node12'
main: 'index.js'
inputs:
start_commit:
description: The starting commit to compare
required: true
deploy_regex:
description: If any changed file matches this regex, it will deploy
default: '.*'
required: false
skip_regex:
description:
Any of the files that match the deploy_regex will be further filtered with
skip_regex. Deploy will be skipped if no files are left.
required: true
outputs:
content_only:
description:
'"true" if only content was changed between the given commits and "false"
if anything else was changed'
51 changes: 51 additions & 0 deletions .github/actions/skip-deploy/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// HT: @alexanderson1993 for creating the first version of this action ❤

const core = require('@actions/core')
const github = require('@actions/github')

async function go() {
const skipRegexes = JSON.parse(github.getOctokit(core.getInput('skip_regex')))
const [owner, repo] = process.env.GITHUB_REPOSITORY.split('/')
const workflowName = core.getInput('workflow_name')

const {data: repoWorkflows} = await octokit.actions.listRepoWorkflows({
owner,
repo,
})
const workflow = repoWorkflows.workflows.find(
({name}) => name === workflowName,
)

const res = await octokit.actions.listWorkflowRuns({
owner,
repo,
workflow_id: workflow.id,
status: 'success',
branch: core.getInput('branch'),
event: 'push',
})

const headCommits = res.data.workflow_runs.map(run => {
return run.head_commit
})

const sortedHeadCommits = headCommits.sort((a, b) => {
const dateA = new Date(a.timestamp)
const dateB = new Date(b.timestamp)
if (dateA < dateB) return -1
if (dateA > dateB) return 1
return 0
})

const lastSuccessCommitHash =
sortedHeadCommits[sortedHeadCommits.length - 1].id

core.setOutput('commit_hash', lastSuccessCommitHash)
}

go().then(
() => core.info('Get Last Commit Hash Complete.'),
error => {
core.setFailed(error.message)
},
)
96 changes: 59 additions & 37 deletions .github/workflows/validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,47 +16,69 @@ jobs:
- name: ⬇️ Checkout repo
uses: actions/checkout@v2

- name: ⎔ Setup node
uses: actions/setup-node@v1
- name: 🔀 Getting last successful commit
id: last_commit
uses: ./.github/actions/get-last-commit-hash
with:
node-version: 14
github_token: ${{ secrets.GITHUB_TOKEN }}

- name: 📥 Download deps
uses: bahmutov/npm-install@v1
- name: Echo last commit
run: echo "The last commit ${{ steps.last_commit.commit_hash }}"

- name: ▶️ Run validate script
run: npx kcd-scripts validate test,lint,typecheck

- name: ⬆️ Upload coverage report
uses: codecov/codecov-action@v1

- name: 🌳 Cypress run
uses: cypress-io/github-action@v2
- name: 🔀 Determining whether to skip the deploy
id: skip_deploy
uses: ./.github/actions/skip-deploy
with:
start: npm start
wait-on: 'http://localhost:8811'
env:
PORT: 8811
github_token: ${{ secrets.GITHUB_TOKEN }}
start_commit: ${{ steps.last_commit.commit_hash }}
skip_regex: '["^content", "README.md"]'

- name: 🚀 Deploy
# only deploy main branch on pushes
- name: Pretend deploy
if:
${{ github.ref == 'refs/heads/main' && github.event_name == 'push' }}
uses: akhileshns/heroku-deploy@v3.6.8
env:
# "HD_" is removed by akhileshns/heroku-deploy
# from the docs:
# > so the action can tell your environment variable apart
# > from multiple other variables (passed by your language,
# > github actions etc) which you probably don't want sitting
# > in your heroku app's config vars.
HD_REMIX_REGISTRY_TOKEN: ${{ secrets.REMIX_REGISTRY_TOKEN }}
HD_CYPRESS_INSTALL_BINARY: '0'
HD_HUSKY_SKIP_INSTALL: '1'
HD_EMAIL_USERNAME: ${{ secrets.EMAIL_USERNAME }}
HD_EMAIL_PASSWORD: ${{ secrets.EMAIL_PASSWORD }}
HD_BOT_GITHUB_TOKEN: ${{ secrets.BOT_GITHUB_TOKEN }}
with:
heroku_api_key: ${{ secrets.HEROKU_API_KEY }}
heroku_app_name: kentcdodds-remix
heroku_email: me@kentcdodds.com
run: echo "I'm pretending to deploy"
# TODO: uncomment these when testing is over...
# - name: ⎔ Setup node
# uses: actions/setup-node@v1
# with:
# node-version: 14

# - name: 📥 Download deps
# uses: bahmutov/npm-install@v1

# - name: ▶️ Run validate script
# run: npx kcd-scripts validate test,lint,typecheck

# - name: ⬆️ Upload coverage report
# uses: codecov/codecov-action@v1

# - name: 🌳 Cypress run
# uses: cypress-io/github-action@v2
# with:
# start: npm start
# wait-on: 'http://localhost:8811'
# env:
# PORT: 8811

# - name: 🚀 Deploy
# # only deploy main branch on pushes
# if:
# ${{ github.ref == 'refs/heads/main' && github.event_name == 'push' }}
# uses: akhileshns/heroku-deploy@v3.6.8
# env:
# # "HD_" is removed by akhileshns/heroku-deploy
# # from the docs:
# # > so the action can tell your environment variable apart
# # > from multiple other variables (passed by your language,
# # > github actions etc) which you probably don't want sitting
# # > in your heroku app's config vars.
# HD_REMIX_REGISTRY_TOKEN: ${{ secrets.REMIX_REGISTRY_TOKEN }}
# HD_CYPRESS_INSTALL_BINARY: '0'
# HD_HUSKY_SKIP_INSTALL: '1'
# HD_EMAIL_USERNAME: ${{ secrets.EMAIL_USERNAME }}
# HD_EMAIL_PASSWORD: ${{ secrets.EMAIL_PASSWORD }}
# HD_BOT_GITHUB_TOKEN: ${{ secrets.BOT_GITHUB_TOKEN }}
# with:
# heroku_api_key: ${{ secrets.HEROKU_API_KEY }}
# heroku_app_name: kentcdodds-remix
# heroku_email: me@kentcdodds.com

0 comments on commit 822a168

Please sign in to comment.