Skip to content

Commit

Permalink
Add new addTimestamp input (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-smithers-excellent authored Sep 1, 2021
1 parent b74f266 commit a7fe665
Show file tree
Hide file tree
Showing 10 changed files with 10,770 additions and 37,074 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,6 @@ typings/
# Allure
allure-report
allure-results

# husky
.husky
115 changes: 59 additions & 56 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Docker Build & Push Action

[![Unit Tests](https://github.com/mr-smithers-excellent/docker-build-push/actions/workflows/ci.yml/badge.svg)](https://github.com/mr-smithers-excellent/docker-build-push/actions/workflows/ci.yml)
[![e2e Tests](https://github.com/mr-smithers-excellent/docker-build-push/actions/workflows/e2e.yml/badge.svg)](https://github.com/mr-smithers-excellent/docker-build-push/actions/workflows/e2e.yml)
[![Maintainability](https://api.codeclimate.com/v1/badges/ac0bf06dc93ba3110cd3/maintainability)](https://codeclimate.com/github/mr-smithers-excellent/docker-build-push/maintainability)
Expand All @@ -8,19 +9,20 @@ Builds a Docker image and pushes it to the private registry of your choosing.

## Supported Docker registries

* Docker Hub
* Google Container Registry (GCR)
* AWS Elastic Container Registry (ECR)
* GitHub Docker Registry
- Docker Hub
- Google Container Registry (GCR)
- AWS Elastic Container Registry (ECR)
- GitHub Docker Registry

## Breaking changes

If you're experiencing issues, be sure you are using the [latest stable release](https://github.com/mr-smithers-excellent/docker-build-push/releases/latest) (currently v5). The AWS ECR login command became deprecated between v4 and v5. Additionally, support for multiple tags was added between v4 and v5.

## Basic usage

* Ensure you run the [checkout action](https://github.com/actions/checkout) before using this action
* Add the following to a workflow `.yml` file in the `/.github` directory of your repo
- Ensure you run the [checkout action](https://github.com/actions/checkout) before using this action
- Add the following to a workflow `.yml` file in the `/.github` directory of your repo

```yaml
steps:
- uses: actions/checkout@v2
Expand All @@ -39,35 +41,36 @@ steps:
## Inputs
| Name | Description | Required | Type |
|------------|----------------------------------------------------------------------------------------------------------|----------|---------|
| image | Docker image name | Yes | String |
| tags | Comma separated docker image tags (see [Tagging the image with GitOps](#tagging-the-image-using-gitops)) | No | List |
| addLatest | Adds the `latest` tag to the GitOps-generated tags | No | Boolean |
| registry | Docker registry host | Yes | String |
| dockerfile | Location of Dockerfile (defaults to `Dockerfile`) | No | String |
| directory | Directory to pass to `docker build` command, if not project root | No | String |
| buildArgs | Docker build arguments passed via `--build-arg` | No | List |
| labels | Docker build labels passed via `--label` | No | List |
| target | Docker build target passed via `--target` | No | String |
| username | Docker registry username | No | String |
| password | Docker registry password or token | No | String |
| githubOrg | GitHub organization to push image to (if not current) | No | String |
| Name | Description | Required | Type |
| ------------ | -------------------------------------------------------------------------------------------------------- | -------- | ------- |
| image | Docker image name | Yes | String |
| tags | Comma separated docker image tags (see [Tagging the image with GitOps](#tagging-the-image-using-gitops)) | No | List |
| addLatest | Adds the `latest` tag to the GitOps-generated tags | No | Boolean |
| addTimestamp | Suffixes a build timestamp to the branch-based Docker tag | No | Boolean |
| registry | Docker registry host | Yes | String |
| dockerfile | Location of Dockerfile (defaults to `Dockerfile`) | No | String |
| directory | Directory to pass to `docker build` command, if not project root | No | String |
| buildArgs | Docker build arguments passed via `--build-arg` | No | List |
| labels | Docker build labels passed via `--label` | No | List |
| target | Docker build target passed via `--target` | No | String |
| username | Docker registry username | No | String |
| password | Docker registry password or token | No | String |
| githubOrg | GitHub organization to push image to (if not current) | No | String |

## Outputs

| Name | Description | Format |
|---------------|-------------------------------------------------------------------|----------------------------|
| imageFullName | Full name of the Docker image with registry prefix | `registry/owner/image` |
| imageName | Name of the Docker image with owner prefix | `owner/image` |
| tags | Tags for the Docker image | `v1,latest` |
| Name | Description | Format |
| ------------- | -------------------------------------------------- | ---------------------- |
| imageFullName | Full name of the Docker image with registry prefix | `registry/owner/image` |
| imageName | Name of the Docker image with owner prefix | `owner/image` |
| tags | Tags for the Docker image | `v1,latest` |

## Examples

### Docker Hub

* Save your Docker Hub username (`DOCKER_USERNAME`) and password (`DOCKER_PASSWORD`) as secrets in your GitHub repo
* Modify sample below and include in your workflow `.github/workflows/*.yml` file
- Save your Docker Hub username (`DOCKER_USERNAME`) and password (`DOCKER_PASSWORD`) as secrets in your GitHub repo
- Modify sample below and include in your workflow `.github/workflows/*.yml` file

```yaml
uses: mr-smithers-excellent/docker-build-push@v5
Expand All @@ -80,28 +83,28 @@ with:

### Google Container Registry (GCR)

* Create a service account with the ability to push to GCR (see [configuring access control](https://cloud.google.com/container-registry/docs/access-control))
* Create and download JSON key for new service account
* Save content of `.json` file as a secret called `DOCKER_PASSWORD` in your GitHub repo
* Modify sample below and include in your workflow `.github/workflows/*.yml` file
* Ensure you set the username to `_json_key`
- Create a service account with the ability to push to GCR (see [configuring access control](https://cloud.google.com/container-registry/docs/access-control))
- Create and download JSON key for new service account
- Save content of `.json` file as a secret called `DOCKER_PASSWORD` in your GitHub repo
- Modify sample below and include in your workflow `.github/workflows/*.yml` file
- Ensure you set the username to `_json_key`

```yaml
uses: mr-smithers-excellent/docker-build-push@v5
with:
image: gcp-project/image-name
registry: gcr.io
username: _json_key
password: ${{ secrets.DOCKER_PASSWORD }}
username: _json_key
password: ${{ secrets.DOCKER_PASSWORD }}
```

### AWS Elastic Container Registry (ECR)

* Create an IAM user with the ability to push to ECR (see [example policies](https://docs.aws.amazon.com/AmazonECR/latest/userguide/ecr_managed_policies.html))
* Create and download access keys
* Save `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` as secrets in your GitHub repo
* Ensure the repo you are trying to push to already exists, if not create with `aws ecr create-repository` before pushing
* Modify sample below and include in your workflow `.github/workflows/*.yml` file
- Create an IAM user with the ability to push to ECR (see [example policies](https://docs.aws.amazon.com/AmazonECR/latest/userguide/ecr_managed_policies.html))
- Create and download access keys
- Save `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` as secrets in your GitHub repo
- Ensure the repo you are trying to push to already exists, if not create with `aws ecr create-repository` before pushing
- Modify sample below and include in your workflow `.github/workflows/*.yml` file

```yaml
uses: mr-smithers-excellent/docker-build-push@v5
Expand All @@ -115,12 +118,12 @@ env:

### GitHub Container Registry

* GitHub recently [migrated their container registry](https://docs.github.com/en/packages/guides/migrating-to-github-container-registry-for-docker-images) from docker.pkg.github.com to ghcr.io
* It is assumed you'll be pushing the image to a repo inside your GitHub organization, unless you set `githubOrg`
* If using ghcr.io, provide the image name in `ghcr.io/OWNER/IMAGE_NAME` format
* If using docker.pkg.github.com, provide the image name in `docker.pkg.github.com/OWNER/REPOSITORY/IMAGE_NAME` format
* Provide either the `${{ github.actor }}` or an alternate username for Docker login (with associated token below)
* Pass the default GitHub Actions token or custom secret with [proper push permissions](https://docs.github.com/en/packages/guides/pushing-and-pulling-docker-images#authenticating-to-github-container-registry)
- GitHub recently [migrated their container registry](https://docs.github.com/en/packages/guides/migrating-to-github-container-registry-for-docker-images) from docker.pkg.github.com to ghcr.io
- It is assumed you'll be pushing the image to a repo inside your GitHub organization, unless you set `githubOrg`
- If using ghcr.io, provide the image name in `ghcr.io/OWNER/IMAGE_NAME` format
- If using docker.pkg.github.com, provide the image name in `docker.pkg.github.com/OWNER/REPOSITORY/IMAGE_NAME` format
- Provide either the `${{ github.actor }}` or an alternate username for Docker login (with associated token below)
- Pass the default GitHub Actions token or custom secret with [proper push permissions](https://docs.github.com/en/packages/guides/pushing-and-pulling-docker-images#authenticating-to-github-container-registry)

#### New ghcr.io

Expand All @@ -131,7 +134,7 @@ with:
registry: ghcr.io
githubOrg: override-org # optional
username: ${{ secrets.GHCR_USERNAME }}
password: ${{ secrets.GHCR_TOKEN }}
password: ${{ secrets.GHCR_TOKEN }}
```

#### Legacy docker.pkg.github.com
Expand All @@ -149,13 +152,13 @@ with:

By default, if you do not pass a `tags` input this action will use an algorithm based on the state of your git repo to determine the Docker image tag(s). This is designed to enable developers to more easily use [GitOps](https://www.weave.works/technologies/gitops/) in their CI/CD pipelines. Below is a table detailing how the GitHub trigger (branch or tag) determines the Docker tag(s).

| Trigger | Commit SHA | addLatest | Docker Tag(s) |
|--------------------------|------------|-----------|-----------------------------|
| /refs/tags/v1.0 | N/A | false | v1.0 |
| /refs/tags/v1.0 | N/A | true | v1.0,latest |
| /refs/heads/dev | 1234567 | false | dev-1234567 |
| /refs/heads/dev | 1234567 | true | dev-1234567,latest |
| /refs/heads/master | 1234567 | false | master-1234567 |
| /refs/heads/master | 1234567 | true | master-1234567,latest |
| /refs/heads/SOME-feature | 1234567 | false | some-feature-1234567 |
| /refs/heads/SOME-feature | 1234567 | true | some-feature-1234567,latest |
| Trigger | Commit SHA | addLatest | addTimestamp | Docker Tag(s) |
| ------------------------ | ---------- | --------- | ------------ | -------------------------------------- |
| /refs/tags/v1.0 | N/A | false | N/A | v1.0 |
| /refs/tags/v1.0 | N/A | true | N/A | v1.0,latest |
| /refs/heads/dev | 1234567 | false | true | dev-1234567-2021-09-01.195027 |
| /refs/heads/dev | 1234567 | true | false | dev-1234567,latest |
| /refs/heads/main | 1234567 | false | true | main-1234567-2021-09-01.195027 |
| /refs/heads/main | 1234567 | true | false | main-1234567,latest |
| /refs/heads/SOME-feature | 1234567 | false | true | some-feature-1234567-2021-09-01.195027 |
| /refs/heads/SOME-feature | 1234567 | true | false | some-feature-1234567,latest |
52 changes: 28 additions & 24 deletions action.yml
Original file line number Diff line number Diff line change
@@ -1,55 +1,59 @@
name: 'Docker Build & Push Action'
description: 'Builds a Docker image and pushes to a private registry with support for multiple tags'
author: 'Sean Smith'
name: "Docker Build & Push Action"
description: "Builds a Docker image and pushes to a private registry with support for multiple tags"
author: "Sean Smith"
inputs:
image:
description: 'Name of the Docker image'
description: "Name of the Docker image"
required: true
tags:
description: 'Tags override for Docker image'
description: "Tags override for Docker image"
required: false
registry:
description: 'Target Docker registry'
description: "Target Docker registry"
required: true
dockerfile:
description: 'Location of Dockerfile, if not Dockerfile in root directory'
description: "Location of Dockerfile, if not Dockerfile in root directory"
required: false
default: 'Dockerfile'
default: "Dockerfile"
directory:
description: 'Directory to run `docker build` from, if not project root'
description: "Directory to run `docker build` from, if not project root"
required: false
buildArgs:
description: 'Docker build arguments passed via --build-arg'
description: "Docker build arguments passed via --build-arg"
required: false
labels:
description: 'Docker build labels passed via --label'
description: "Docker build labels passed via --label"
required: false
target:
description: 'Docker build target passed via --target'
description: "Docker build target passed via --target"
required: false
username:
description: 'Docker registry username'
description: "Docker registry username"
required: false
password:
description: 'Docker registry password'
description: "Docker registry password"
required: false
gitHubOrg:
description: 'GitHub organization to push image to (if not current)'
description: "GitHub organization to push image to (if not current)"
required: false
addLatest:
description: 'Adds latest tag to auto-generated GitOps tag'
description: "Adds latest tag to auto-generated GitOps tag"
required: false
default: 'false'
default: "false"
addTimestamp:
description: "Suffixes a build timestamp to the branch-based Docker tag"
required: false
default: "false"
outputs:
imageFullName:
description: 'Full name of the Docker image with registry prefix and tag suffix'
description: "Full name of the Docker image with registry prefix and tag suffix"
imageName:
description: 'Name of the Docker image with owner prefix'
description: "Name of the Docker image with owner prefix"
tags:
description: 'Tags for the Docker image'
description: "Tags for the Docker image"
runs:
using: 'node12'
main: 'dist/index.js'
using: "node12"
main: "dist/index.js"
branding:
icon: 'anchor'
color: 'blue'
icon: "anchor"
color: "blue"
Loading

0 comments on commit a7fe665

Please sign in to comment.