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

Allow to templatize schedule tag #1

Merged
merged 4 commits into from
Oct 25, 2020
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
27 changes: 25 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: ci

on:
schedule:
- cron: '0 10 * * 0' # everyday sunday at 10am
- cron: '0 * * * *' # every hours
push:
branches:
- '**'
Expand All @@ -22,14 +22,37 @@ jobs:
uses: actions/[email protected]
-
name: Docker meta
id: docker_meta
uses: ./
with:
images: |
${{ env.DOCKER_IMAGE }}
ghcr.io/name/app
tag-sha: true

tag-schedule:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
tag-schedule:
- ""
- "cron-{{date 'YYYYMMDD'}}"
- "{{date 'YYYYMMDD-HHmmss'}}"
- "schedule"
steps:
-
name: Checkout
uses: actions/[email protected]
-
name: Docker meta
uses: ./
with:
images: |
${{ env.DOCKER_IMAGE }}
ghcr.io/name/app
tag-sha: true
tag-schedule: ${{ matrix.tag-schedule }}

docker-push:
runs-on: ubuntu-latest
services:
Expand Down
22 changes: 19 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ ___
* [Customizing](#customizing)
* [inputs](#inputs)
* [outputs](#outputs)
* [Notes](#notes)
* [Templates available for schedule tag](#templates-available-for-schedule-tag)
* [Keep up-to-date with GitHub Dependabot](#keep-up-to-date-with-github-dependabot)
* [How can I help?](#how-can-i-help)
* [License](#license)
Expand All @@ -34,13 +36,13 @@ ___

| Event | Ref | Commit SHA | Docker Tag | Pushed |
|-----------------|-------------------------------|------------|------------------------------------|--------|
| `schedule` | | | `nightly` | Yes |
| `schedule` | | `45f132a` | `sha-45f132a`, `nightly` | Yes |
| `pull_request` | `refs/pull/2/merge` | `a123b57` | `sha-a123b57`, `pr-2` | No |
| `push` | `refs/heads/<default_branch>` | `676cae2` | `sha-676cae2`, `edge` | Yes |
| `push` | `refs/heads/dev` | `cf20257` | `sha-cf20257`, `dev` | Yes |
| `push` | `refs/heads/my/branch` | `a5df687` | `sha-a5df687`, `my-branch` | Yes |
| `push tag` | `refs/tags/v1.2.3` | | `1.2.3`, `latest` | Yes |
| `push tag` | `refs/tags/mytag` | | `mytag` | Yes |
| `push tag` | `refs/tags/v1.2.3` | `bf4565b` | `sha-bf4565b`, `1.2.3`, `latest` | Yes |
| `push tag` | `refs/tags/mytag` | `afb7833` | `sha-afb7833`, `mytag` | Yes |

```yaml
name: ci
Expand Down Expand Up @@ -107,6 +109,7 @@ Following inputs can be used as `step.with` keys
| `images` | List/CSV | List of Docker images to use as base name for tags |
| `tag-sha` | Bool | Add git short SHA as Docker tag (default `false`) |
| `tag-edge` | String | Branch that will be tagged as edge (default `repo.default_branch`) |
| `tag-schedule` | String | [Handlebars template](https://handlebarsjs.com/guide/) to apply to schedule tag (default `nightly`) |
| `sep-tags` | String | Separator to use for tags output (default `\n`) |
| `sep-labels` | String | Separator to use for labels output (default `\n`) |

Expand All @@ -122,6 +125,19 @@ Following outputs are available
| `tags` | String | Generated Docker tags |
| `labels` | String | Generated Docker labels |

## Notes

### Templates available for schedule tag

`tag-schedule` is specially crafted input to support [Handlebars template](https://handlebarsjs.com/guide/) with
the following expressions:

| Expression | Example | Description |
|-------------------------|-------------------------------------------|------------------------------------------|
| `{{date 'format'}}` | `{{date 'YYYYMMDD'}}` > `20200110` | Render date by its [moment format](https://momentjs.com/docs/#/displaying/format/)

You can find more examples in the [CI workflow](.github/workflows/ci.yml).

## Keep up-to-date with GitHub Dependabot

Since [Dependabot](https://docs.github.com/en/github/administering-a-repository/keeping-your-actions-up-to-date-with-github-dependabot)
Expand Down
105 changes: 75 additions & 30 deletions __tests__/meta.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import * as fs from 'fs';
import * as path from 'path';
import * as dotenv from 'dotenv';
import {Inputs} from '../src/context';
import * as moment from 'moment';
import {getInputs, Inputs} from '../src/context';
import * as github from '../src/github';
import {Meta} from '../src/meta';
import {Context} from '@actions/github/lib/context';
Expand All @@ -23,6 +24,10 @@ jest.spyOn(global.Date.prototype, 'toISOString').mockImplementation(() => {
return '2020-01-10T00:30:00.000Z';
});

jest.mock('moment', () => {
return () => jest.requireActual('moment')('2020-01-10T00:30:00.000Z');
});

describe('tags and labels', () => {
beforeEach(() => {
Object.keys(process.env).forEach(function (key) {
Expand All @@ -38,7 +43,7 @@ describe('tags and labels', () => {
'event_null.env',
{
images: ['user/app'],
},
} as Inputs,
undefined,
[],
[
Expand All @@ -56,7 +61,7 @@ describe('tags and labels', () => {
'event_empty.env',
{
images: ['user/app'],
},
} as Inputs,
undefined,
[],
[
Expand All @@ -74,7 +79,7 @@ describe('tags and labels', () => {
'event_pull_request.env',
{
images: ['user/app'],
},
} as Inputs,
'pr-2',
[
'user/app:pr-2'
Expand All @@ -94,7 +99,7 @@ describe('tags and labels', () => {
'event_push.env',
{
images: ['user/app'],
},
} as Inputs,
'dev',
[
'user/app:dev'
Expand All @@ -114,7 +119,7 @@ describe('tags and labels', () => {
'event_push_defbranch.env',
{
images: ['user/app'],
},
} as Inputs,
'edge',
[
'user/app:edge'
Expand All @@ -134,7 +139,7 @@ describe('tags and labels', () => {
'event_release.env',
{
images: ['user/app'],
},
} as Inputs,
'1.1.1',
[
'user/app:1.1.1',
Expand All @@ -155,7 +160,7 @@ describe('tags and labels', () => {
'event_schedule.env',
{
images: ['user/app'],
},
} as Inputs,
'nightly',
[
'user/app:nightly'
Expand All @@ -171,11 +176,53 @@ describe('tags and labels', () => {
"org.opencontainers.image.licenses=MIT"
]
],
[
'event_schedule.env',
{
images: ['user/app'],
tagSchedule: `{{date 'YYYYMMDD'}}`
} as Inputs,
'20200110',
[
'user/app:20200110'
],
[
"org.opencontainers.image.title=Hello-World",
"org.opencontainers.image.description=This your first repo!",
"org.opencontainers.image.url=https://github.com/octocat/Hello-World",
"org.opencontainers.image.source=https://github.com/octocat/Hello-World.git",
"org.opencontainers.image.version=20200110",
"org.opencontainers.image.created=2020-01-10T00:30:00.000Z",
"org.opencontainers.image.revision=90dd6032fac8bda1b6c4436a2e65de27961ed071",
"org.opencontainers.image.licenses=MIT"
]
],
[
'event_schedule.env',
{
images: ['user/app'],
tagSchedule: `{{date 'YYYYMMDD-HHmmss'}}`
} as Inputs,
'20200110-003000',
[
'user/app:20200110-003000'
],
[
"org.opencontainers.image.title=Hello-World",
"org.opencontainers.image.description=This your first repo!",
"org.opencontainers.image.url=https://github.com/octocat/Hello-World",
"org.opencontainers.image.source=https://github.com/octocat/Hello-World.git",
"org.opencontainers.image.version=20200110-003000",
"org.opencontainers.image.created=2020-01-10T00:30:00.000Z",
"org.opencontainers.image.revision=90dd6032fac8bda1b6c4436a2e65de27961ed071",
"org.opencontainers.image.licenses=MIT"
]
],
[
'event_tag.env',
{
images: ['user/app'],
},
} as Inputs,
'release1',
[
'user/app:release1'
Expand All @@ -195,7 +242,7 @@ describe('tags and labels', () => {
'event_tag_semver.env',
{
images: ['user/app'],
},
} as Inputs,
'1.1.1',
[
'user/app:1.1.1',
Expand All @@ -216,7 +263,7 @@ describe('tags and labels', () => {
'event_workflow_dispatch.env',
{
images: ['user/app'],
},
} as Inputs,
'edge',
[
'user/app:edge'
Expand All @@ -236,7 +283,7 @@ describe('tags and labels', () => {
'event_pull_request.env',
{
images: ['org/app', 'ghcr.io/user/app'],
},
} as Inputs,
'pr-2',
[
'org/app:pr-2',
Expand All @@ -257,7 +304,7 @@ describe('tags and labels', () => {
'event_push.env',
{
images: ['org/app', 'ghcr.io/user/app'],
},
} as Inputs,
'dev',
[
'org/app:dev',
Expand All @@ -278,7 +325,7 @@ describe('tags and labels', () => {
'event_push_defbranch.env',
{
images: ['org/app', 'ghcr.io/user/app'],
},
} as Inputs,
'edge',
[
'org/app:edge',
Expand All @@ -299,7 +346,7 @@ describe('tags and labels', () => {
'event_schedule.env',
{
images: ['org/app', 'ghcr.io/user/app'],
},
} as Inputs,
'nightly',
[
'org/app:nightly',
Expand All @@ -320,7 +367,7 @@ describe('tags and labels', () => {
'event_tag_semver.env',
{
images: ['org/app', 'ghcr.io/user/app'],
},
} as Inputs,
'1.1.1',
[
'org/app:1.1.1',
Expand All @@ -344,7 +391,7 @@ describe('tags and labels', () => {
{
images: ['org/app', 'ghcr.io/user/app'],
tagSha: true,
},
} as Inputs,
'pr-2',
[
'org/app:pr-2',
Expand All @@ -368,7 +415,7 @@ describe('tags and labels', () => {
{
images: ['org/app', 'ghcr.io/user/app'],
tagSha: true,
},
} as Inputs,
'dev',
[
'org/app:dev',
Expand All @@ -392,7 +439,7 @@ describe('tags and labels', () => {
{
images: ['org/app', 'ghcr.io/user/app'],
tagSha: true,
},
} as Inputs,
'edge',
[
'org/app:edge',
Expand All @@ -416,7 +463,7 @@ describe('tags and labels', () => {
{
images: ['org/app', 'ghcr.io/user/app'],
tagSha: true,
},
} as Inputs,
'nightly',
[
'org/app:nightly',
Expand All @@ -440,7 +487,7 @@ describe('tags and labels', () => {
{
images: ['org/app', 'ghcr.io/user/app'],
tagSha: true,
},
} as Inputs,
'1.1.1',
[
'org/app:1.1.1',
Expand All @@ -467,7 +514,7 @@ describe('tags and labels', () => {
images: ['org/app', 'ghcr.io/user/app'],
tagSha: true,
tagEdge: 'dev'
},
} as Inputs,
'edge',
[
'org/app:edge',
Expand All @@ -492,7 +539,7 @@ describe('tags and labels', () => {
images: ['org/app', 'ghcr.io/user/app'],
tagSha: true,
tagEdge: 'dev'
},
} as Inputs,
'master',
[
'org/app:master',
Expand All @@ -513,24 +560,22 @@ describe('tags and labels', () => {
],
])('given %p event ', async (envFile, inputs, exVersion, exTags, exLabels) => {
process.env = dotenv.parse(fs.readFileSync(path.join(__dirname, 'fixtures', envFile)));
console.log(process.env);

const context = github.context();
console.log(context);
console.log(process.env, context);

const repo = await github.repo(process.env.GITHUB_TOKEN || '');
const meta = new Meta(inputs as Inputs, context, repo);
const meta = new Meta({...getInputs(), ...inputs}, context, repo);

const version = meta.version();
console.log(version)
console.log('version', version);
expect(version).toEqual(exVersion);

const tags = meta.tags();
console.log(tags)
console.log('tags', tags);
expect(tags).toEqual(exTags);

const labels = meta.labels();
console.log(labels)
console.log('labels', labels);
expect(labels).toEqual(exLabels);
});
});
Loading