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

feat: create github deployment #45

Draft
wants to merge 11 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion .github/workflows/example-scope.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:
- master

jobs:
static:
example-scope:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
Expand Down
5 changes: 4 additions & 1 deletion .github/workflows/example-static.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ on:
push:
branches:
- master
- feature/*
pull_request:
branches:
- master
Expand All @@ -25,6 +24,8 @@ jobs:
alias-domains: |
staging.static.vercel-action.amond.dev
pr-{{PR_NUMBER}}.static.vercel-action.amond.dev
github-deployment: true
github-deployment-environment: static-staging
- name: production or not
id: prod_or_not
run: |
Expand All @@ -48,3 +49,5 @@ jobs:
working-directory: example/static
alias-domains: |
{{BRANCH}}.static.vercel-action.amond.dev
github-deployment: true
github-deployment-environment: static-production
5 changes: 4 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ inputs:
required: false
default: 'false'
description: 'if you want to create github deployment, set true, default: false'
github-deployment-environment:
required: false
description: 'The environment parameter allows deployments to be issued to different runtime environments. Teams often have multiple environments for verifying their applications, such as production, staging, and qa. This parameter makes it easier to track which environments have requested deployments. The default environment is production.'
default: 'production'
working-directory:
required: false
description: the working directory
Expand Down Expand Up @@ -55,7 +59,6 @@ inputs:
required: false
description: 'You can assign a domain to this deployment. Please note that this domain must have been configured in the project. You can use pull request number via `{{PR_NUMBER}}` and branch via `{{BRANCH}}`'
default: ''

outputs:
preview-url:
description: 'deployment preview URL'
Expand Down
51 changes: 48 additions & 3 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1149,6 +1149,9 @@ const aliasDomains = core
return url;
});

const githubDeployment = core.getInput('github-deployment') === 'true';
const githubDeploymentEnv = core.getInput('github-deployment-environment');

let octokit;
if (githubToken) {
octokit = new github.GitHub(githubToken);
Expand Down Expand Up @@ -1399,17 +1402,54 @@ async function aliasDomainsToDeployment(deploymentUrl) {
await Promise.all(promises);
}

function getEffectiveRef() {
let { ref } = context;
if (github.context.eventName === 'pull_request') {
const pullRequestPayload = github.context.payload;
ref = pullRequestPayload.pull_request.head.ref;
}
return ref;
}

async function createGithubDeployment() {
if (githubDeployment && octokit) {
core.debug(`Create a github deployment.`);
const { data: deployment } = await octokit.repos.createDeployment({
...context.repo,
ref: getEffectiveRef(),
environment: githubDeploymentEnv,
required_contexts: [],
});
core.debug(`Created deployment is ${deployment.id}`);
return deployment.id;
}
return null;
}

async function createGithubDeploymentStatus(deploymentId, status, logUrl) {
if (githubDeployment && deploymentId && status && octokit) {
await octokit.repos.createDeploymentStatus({
...context.repo,
deployment_id: deploymentId,
state: status,
log_url: logUrl,
});
}
}

async function run() {
core.debug(`action : ${context.action}`);
core.debug(`ref : ${context.ref}`);
core.debug(`eventName : ${context.eventName}`);
core.debug(`actor : ${context.actor}`);
core.debug(`sha : ${context.sha}`);
core.debug(`workflow : ${context.workflow}`);
let { ref } = context;
let ref = getEffectiveRef();
let { sha } = context;
await setEnv();

const githubDeploymentId = await createGithubDeployment();

let commit = execSync('git log -1 --pretty=format:%B')
.toString()
.trim();
Expand All @@ -1434,9 +1474,14 @@ async function run() {
core.debug(`The head commit is: ${commit}`);
}
}

const deploymentUrl = await vercelDeploy(ref, commit);

if (githubDeploymentId) {
await createGithubDeploymentStatus(
githubDeploymentId,
'success',
deploymentUrl,
);
}
if (deploymentUrl) {
core.info('set preview-url output');
if (aliasDomains && aliasDomains.length) {
Expand Down
51 changes: 48 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ const aliasDomains = core
return url;
});

const githubDeployment = core.getInput('github-deployment') === 'true';
const githubDeploymentEnv = core.getInput('github-deployment-environment');

let octokit;
if (githubToken) {
octokit = new github.GitHub(githubToken);
Expand Down Expand Up @@ -302,17 +305,54 @@ async function aliasDomainsToDeployment(deploymentUrl) {
await Promise.all(promises);
}

function getEffectiveRef() {
let { ref } = context;
if (github.context.eventName === 'pull_request') {
const pullRequestPayload = github.context.payload;
ref = pullRequestPayload.pull_request.head.ref;
}
return ref;
}

async function createGithubDeployment() {
if (githubDeployment && octokit) {
core.debug(`Create a github deployment.`);
const { data: deployment } = await octokit.repos.createDeployment({
...context.repo,
ref: getEffectiveRef(),
environment: githubDeploymentEnv,
required_contexts: [],
});
core.debug(`Created deployment is ${deployment.id}`);
return deployment.id;
}
return null;
}

async function createGithubDeploymentStatus(deploymentId, status, logUrl) {
if (githubDeployment && deploymentId && status && octokit) {
await octokit.repos.createDeploymentStatus({
...context.repo,
deployment_id: deploymentId,
state: status,
log_url: logUrl,
});
}
}

async function run() {
core.debug(`action : ${context.action}`);
core.debug(`ref : ${context.ref}`);
core.debug(`eventName : ${context.eventName}`);
core.debug(`actor : ${context.actor}`);
core.debug(`sha : ${context.sha}`);
core.debug(`workflow : ${context.workflow}`);
let { ref } = context;
let ref = getEffectiveRef();
let { sha } = context;
await setEnv();

const githubDeploymentId = await createGithubDeployment();

let commit = execSync('git log -1 --pretty=format:%B')
.toString()
.trim();
Expand All @@ -337,9 +377,14 @@ async function run() {
core.debug(`The head commit is: ${commit}`);
}
}

const deploymentUrl = await vercelDeploy(ref, commit);

if (githubDeploymentId) {
await createGithubDeploymentStatus(
githubDeploymentId,
'success',
deploymentUrl,
);
}
if (deploymentUrl) {
core.info('set preview-url output');
if (aliasDomains && aliasDomains.length) {
Expand Down