Skip to content

Commit

Permalink
feat: support downloading goreleaser pro (#284)
Browse files Browse the repository at this point in the history
  • Loading branch information
caarlos0 committed May 27, 2021
1 parent a3b2f49 commit 70eb4e5
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 47 deletions.
28 changes: 17 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,14 @@ jobs:
name: Run GoReleaser
uses: goreleaser/goreleaser-action@v2
with:
# either 'goreleaser' (default) or 'goreleaser-pro'
distribution: goreleaser
version: latest
args: release --rm-dist
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# Your GoReleaser Pro key, if you are using the 'goreleaser-pro' distribution
# GORELEASER_KEY: ${{ secrets.GORELEASER_KEY }}
```

> **IMPORTANT**: note the `fetch-depth: 0` input in `Checkout` step. It is required for the changelog to work correctly.
Expand Down Expand Up @@ -165,30 +169,32 @@ steps:

Following inputs can be used as `step.with` keys

| Name | Type | Default | Description |
|------------------|---------|-----------|-------------------------------------------|
| `version`**¹** | String | `latest` | GoReleaser version |
| `args` | String | | Arguments to pass to GoReleaser |
| `workdir` | String | `.` | Working directory (below repository root) |
| `install-only` | Bool | `false` | Just install GoReleaser |
| Name | Type | Default | Description |
|------------------|---------|--------------|------------------------------------------------------------------|
| `distribution` | String | `goreleaser` | GoReleaser distribution, either `goreleaser` or `goreleaser-pro` |
| `version`**¹** | String | `latest` | GoReleaser version |
| `args` | String | | Arguments to pass to GoReleaser |
| `workdir` | String | `.` | Working directory (below repository root) |
| `install-only` | Bool | `false` | Just install GoReleaser |

> **¹** Can be a fixed version like `v0.117.0` or a max satisfying semver one like `~> 0.132`. In this case this will return `v0.132.1`.
### environment variables

Following environment variables can be used as `step.env` keys

| Name | Description |
|----------------|---------------------------------------|
| `GITHUB_TOKEN` | [GITHUB_TOKEN](https://help.github.com/en/actions/configuring-and-managing-workflows/authenticating-with-the-github_token) as provided by `secrets` |
| Name | Description |
|------------------|---------------------------------------|
| `GITHUB_TOKEN` | [GITHUB_TOKEN](https://help.github.com/en/actions/configuring-and-managing-workflows/authenticating-with-the-github_token) as provided by `secrets` |
| `GORELEASER_KEY` | Your [GoReleaser Pro](https://goreleaser.com/pro) License Key, in case you are using the `goreleaser-pro` distribution |

## Limitation

`GITHUB_TOKEN` permissions [are limited to the repository](https://help.github.com/en/actions/configuring-and-managing-workflows/authenticating-with-the-github_token#about-the-github_token-secret)
that contains your workflow.
that contains your workflow.

If you need to push the homebrew tap to another repository, you must therefore create a custom [Personal Access Token](https://help.github.com/articles/creating-a-personal-access-token-for-the-command-line/)
with `repo` permissions and [add it as a secret in the repository](https://help.github.com/en/actions/automating-your-workflow-with-github-actions/creating-and-using-encrypted-secrets). If you create a
with `repo` permissions and [add it as a secret in the repository](https://help.github.com/en/actions/automating-your-workflow-with-github-actions/creating-and-using-encrypted-secrets). If you create a
secret named `GH_PAT`, the step will look like this:

```yaml
Expand Down
22 changes: 18 additions & 4 deletions __tests__/github.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,33 @@ import * as github from '../src/github';

describe('github', () => {
it('returns latest GoReleaser GitHub release', async () => {
const release = await github.getRelease('latest');
const release = await github.getRelease('goreleaser', 'latest');
expect(release).not.toBeNull();
expect(release?.tag_name).not.toEqual('');
console.log(`tag_name: ${release?.tag_name}`);
});
it('returns v0.117.0 GoReleaser GitHub release', async () => {
const release = await github.getRelease('v0.117.0');
const release = await github.getRelease('goreleaser', 'v0.117.0');
expect(release).not.toBeNull();
expect(release?.tag_name).toEqual('v0.117.0');
});
it('returns v0.132.1 GoReleaser GitHub release', async () => {
const release = await github.getRelease('~> 0.132');
const release = await github.getRelease('goreleaser', '~> 0.132');
expect(release).not.toBeNull();
expect(release?.tag_name).toEqual('v0.132.1');
});
it('returns latest GoReleaser Pro GitHub release', async () => {
const release = await github.getRelease('goreleaser-pro', 'latest');
expect(release).not.toBeNull();
expect(release?.tag_name).not.toEqual('');
});
it('returns v0.166.0-pro GoReleaser Pro GitHub release', async () => {
const release = await github.getRelease('goreleaser-pro', 'v0.166.0-pro');
expect(release).not.toBeNull();
expect(release?.tag_name).toEqual('v0.166.0-pro');
});
it('returns v0.166.0-pro GoReleaser Pro GitHub release when using semver', async () => {
const release = await github.getRelease('goreleaser-pro', '~> 0.166');
expect(release).not.toBeNull();
expect(release?.tag_name).toEqual('v0.166.0-pro');
});
});
14 changes: 12 additions & 2 deletions __tests__/installer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,22 @@ import * as installer from '../src/installer';

describe('installer', () => {
it('acquires v0.117.0 version of GoReleaser', async () => {
const goreleaser = await installer.getGoReleaser('v0.117.0');
const goreleaser = await installer.getGoReleaser('goreleaser', 'v0.117.0');
expect(fs.existsSync(goreleaser)).toBe(true);
}, 100000);

it('acquires latest version of GoReleaser', async () => {
const goreleaser = await installer.getGoReleaser('latest');
const goreleaser = await installer.getGoReleaser('goreleaser', 'latest');
expect(fs.existsSync(goreleaser)).toBe(true);
}, 100000);

it('acquires v0.166.0-pro version of GoReleaser Pro', async () => {
const goreleaser = await installer.getGoReleaser('goreleaser-pro', 'v0.166.0-pro');
expect(fs.existsSync(goreleaser)).toBe(true);
}, 100000);

it('acquires latest version of GoReleaser Pro', async () => {
const goreleaser = await installer.getGoReleaser('goreleaser-pro', 'latest');
expect(fs.existsSync(goreleaser)).toBe(true);
}, 100000);
});
10 changes: 10 additions & 0 deletions __tests__/pro.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import * as pro from '../src/pro';

describe('pro', () => {
it('suffixes pro distribution', async () => {
expect(pro.suffix('goreleaser-pro')).toEqual('-pro');
});
it('does not suffix oss distribution', async () => {
expect(pro.suffix('goreleaser')).toEqual('');
});
});
60 changes: 45 additions & 15 deletions dist/index.js

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

28 changes: 20 additions & 8 deletions src/github.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,44 @@
import * as httpm from '@actions/http-client';
import * as core from '@actions/core';
import * as semver from 'semver';
import * as pro from './pro';

export interface GitHubRelease {
id: number;
tag_name: string;
}

export const getRelease = async (version: string): Promise<GitHubRelease | null> => {
const resolvedVersion: string = (await resolveVersion(version)) || version;
const url: string = `https://github.com/goreleaser/goreleaser/releases/${resolvedVersion}`;
export const getRelease = async (distribution: string, version: string): Promise<GitHubRelease | null> => {
const resolvedVersion: string = (await resolveVersion(distribution, version)) || version;
const url: string = `https://github.com/goreleaser/${distribution}/releases/${resolvedVersion}`;
const http: httpm.HttpClient = new httpm.HttpClient('goreleaser-action');
return (await http.getJson<GitHubRelease>(url)).result;
};

const resolveVersion = async (version: string): Promise<string | null> => {
const allTags: Array<string> | null = await getAllTags();
const resolveVersion = async (distribution: string, version: string): Promise<string | null> => {
const allTags: Array<string> | null = await getAllTags(distribution);
if (!allTags) {
throw new Error(`Cannot find GoReleaser tags`);
}
core.debug(`Found ${allTags.length} tags in total`);

return semver.maxSatisfying(allTags, version);
if (version === 'latest' || !pro.isPro(distribution)) {
return semver.maxSatisfying(allTags, version);
}

const cleanTags: Array<string> = allTags.map(tag => cleanTag(tag));
const cleanVersion: string = cleanTag(version);
return semver.maxSatisfying(cleanTags, cleanVersion) + pro.suffix(distribution);
};

interface GitHubTag {
tag_name: string;
}

const getAllTags = async (): Promise<Array<string>> => {
const getAllTags = async (distribution: string): Promise<Array<string>> => {
const http: httpm.HttpClient = new httpm.HttpClient('goreleaser-action');
const url: string = `https://goreleaser.com/static/releases.json`;
const suffix: string = pro.suffix(distribution);
const url: string = `https://goreleaser.com/static/releases${suffix}.json`;
const getTags = http.getJson<Array<GitHubTag>>(url);

return getTags.then(response => {
Expand All @@ -41,3 +49,7 @@ const getAllTags = async (): Promise<Array<string>> => {
return response.result.map(obj => obj.tag_name);
});
};

const cleanTag = (tag: string): string => {
return tag.replace(/-pro$/, '');
};
15 changes: 9 additions & 6 deletions src/installer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,24 @@ import * as os from 'os';
import * as path from 'path';
import * as util from 'util';
import * as github from './github';
import * as pro from './pro';
import * as core from '@actions/core';
import * as tc from '@actions/tool-cache';

const osPlat: string = os.platform();
const osArch: string = os.arch();

export async function getGoReleaser(version: string): Promise<string> {
const release: github.GitHubRelease | null = await github.getRelease(version);
export async function getGoReleaser(distribution: string, version: string): Promise<string> {
const release: github.GitHubRelease | null = await github.getRelease(distribution, version);
if (!release) {
throw new Error(`Cannot find GoReleaser ${version} release`);
}

core.info(`✅ GoReleaser version found: ${release.tag_name}`);
const filename = getFilename();
const filename = getFilename(distribution);
const downloadUrl = util.format(
'https://github.com/goreleaser/goreleaser/releases/download/%s/%s',
'https://github.com/goreleaser/%s/releases/download/%s/%s',
distribution,
release.tag_name,
filename
);
Expand All @@ -44,9 +46,10 @@ export async function getGoReleaser(version: string): Promise<string> {
return exePath;
}

const getFilename = (): string => {
const getFilename = (distribution: string): string => {
const platform: string = osPlat == 'win32' ? 'Windows' : osPlat == 'darwin' ? 'Darwin' : 'Linux';
const arch: string = osArch == 'x64' ? 'x86_64' : 'i386';
const ext: string = osPlat == 'win32' ? 'zip' : 'tar.gz';
return util.format('goreleaser_%s_%s.%s', platform, arch, ext);
const suffix: string = pro.suffix(distribution);
return util.format('goreleaser%s_%s_%s.%s', suffix, platform, arch, ext);
};
3 changes: 2 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import {dirname} from 'path';

async function run(): Promise<void> {
try {
const distribution = core.getInput('distribution') || 'goreleaser';
const version = core.getInput('version') || 'latest';
const args = core.getInput('args');
const workdir = core.getInput('workdir') || '.';
const isInstallOnly = /^true$/i.test(core.getInput('install-only'));
const goreleaser = await installer.getGoReleaser(version);
const goreleaser = await installer.getGoReleaser(distribution, version);
core.info(`✅ GoReleaser installed successfully`);

if (isInstallOnly) {
Expand Down
7 changes: 7 additions & 0 deletions src/pro.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const suffix = (distribution: string): string => {
return isPro(distribution) ? '-pro' : '';
};

export const isPro = (distribution: string): boolean => {
return distribution === 'goreleaser-pro';
};

0 comments on commit 70eb4e5

Please sign in to comment.