Skip to content
This repository has been archived by the owner on Nov 29, 2024. It is now read-only.

Feature/support ecr docker #12

Merged
merged 4 commits into from
Oct 15, 2023
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
78 changes: 78 additions & 0 deletions .github/workflows/test-action-on-pr-and-schedule.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
name: Test github-actions-build-push-containers on PR and schedule

on:
pull_request:
types: [opened, synchronize, reopened]
schedule:
- cron: '45 12 * * 4'

jobs:
test_action:
runs-on: ubuntu-22.04

steps:
- name: set variables
run: |
echo "TEST_IMAGE_NAME=glueops/github-actions-build-push-containers/test-github-actions-build-push-containers" >> $GITHUB_ENV
echo "DOCKERHUB_TEST_IMAGE_NAME=glueopsrocksv2/github-actions-build-push-containers_test-github-actions-build-push-containers" >> $GITHUB_ENV
echo "ECR_REGISTRY=616531474007.dkr.ecr.us-west-2.amazonaws.com" >> $GITHUB_ENV
echo "AWS_REGION=us-west-2" >> $GITHUB_ENV

- name: Checkout code
uses: actions/checkout@v3


- name: run ghcr.io
uses: ./
with:
image_name: ${{ env.TEST_IMAGE_NAME }}
registry: "ghcr.io"
context: "./test-directory/tests/"
target_directory: test-directory

- name: test ghcr.io
run: |
echo "::group::pull from ghcr.io"
docker pull ghcr.io/$TEST_IMAGE_NAME:${{ github.sha }}
echo "::endgroup::"
docker run -e REGISTRY=ghcr.io ghcr.io/$TEST_IMAGE_NAME:${{ github.sha }}


- name: run ecr
uses: ./
with:
aws_access_key_id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws_secret_access_key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws_default_region: ${{ env.AWS_REGION }}
image_name: ${{ env.TEST_IMAGE_NAME }}
registry: ${{ env.ECR_REGISTRY }}
context: "./test-directory/tests/"
target_directory: test-directory

- name: test ecr
run: |
echo "::group::log in to ecr and pull"
echo $(aws ecr get-login-password --region $AWS_REGION) \
| docker login --username AWS --password-stdin $ECR_REGISTRY
docker pull $ECR_REGISTRY/$TEST_IMAGE_NAME:${{ github.sha }}
echo "::endgroup::"
docker run -e REGISTRY=dkr.ecr $ECR_REGISTRY/$TEST_IMAGE_NAME:${{ github.sha }}


- name: run docker hub
uses: ./
with:
dockerhub_username: ${{ secrets.DOCKERHUB_USERNAME }}
dockerhub_password: ${{ secrets.DOCKERHUB_PASSWORD }}
image_name: ${{ env.DOCKERHUB_TEST_IMAGE_NAME }}
registry: "docker.io"
context: "./test-directory/tests/"
target_directory: test-directory

- name: test docker hub
run: |
echo "::group::log in to docker.io and pull"
echo "${{ secrets.DOCKERHUB_PASSWORD }}" | docker login -u "${{ secrets.DOCKERHUB_USERNAME }}" --password-stdin
docker pull $DOCKERHUB_TEST_IMAGE_NAME:${{ github.sha }}
echo "::endgroup::"
docker run -e REGISTRY=docker.io $DOCKERHUB_TEST_IMAGE_NAME:${{ github.sha }}
82 changes: 77 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,85 @@
# Custom Action to push Docker images to GitHub Container Registry
Automate your Docker image deployments effortlessly with this custom GitHub Action! 🚀💪 Whenever there's a push event or a release event in your repository (public and private), this action automatically pushes the Docker image to ghcr.io, GitHub's Container Registry.
# Custom Action to build and push Docker images to GitHub Container Registry (ghcr.io), Docker Hub (docker.io), and AWS ECR

Automate your Docker image deployments effortlessly with this custom GitHub Action! 🚀💪
Configure the event using the GitHub Actions `on:` clause to determine what triggers builds.
This Action supports both public and private repositories for ghcr, docker, and ecr.
The default registry is ghcr.io.

## 💡 Benefits

## 💡 Benefits:
✅ Streamlined workflow: Say goodbye to tedious configuration and manual image deployments.

✅ Increased efficiency: Focus on developing and let the CI/CD pipeline handle image distribution.

✅ Seamless integration: GitHub Container Registry simplifies container image management.
✅ Seamless integration: simplifies container image management.

✅ Default Image Tagging: Out-of-the-box tagging with the below elements.

* `Target Reference:` Either Branch Name or Tag, depending upon the trigger context.
* `Short SHA`
* `SHA`

## 🛠️ How to Use

For usage instructions, refer to the [GlueOps Documentation](https://glueops.dev/docs/deploy-applications/deploy-hello-world-to-glueops#add-ci-to-publish-a-docker-image-to-github-container-registry).
For detailed usage instructions, refer to the [GlueOps Documentation](https://glueops.dev/docs/deploy-applications/deploy-hello-world-to-glueops#add-ci-to-publish-a-docker-image-to-github-container-registry).

### Example Configurations

#### **GitHub Container Registry (ghcr.io)**

```yaml
name: Build and Push Container to GitHub Container Registry

on:
pull_request:
types: [opened, synchronize, reopened]

jobs:
build_and_push:
runs-on: ubuntu-latest
steps:
- name: Build and Push Container to ghcr.io
uses: GlueOps/github-actions-build-push-containers@v0.2.0
```

#### **Docker Hub (docker.io)**

```yaml
name: Build and Push Container to Docker Hub

on:
pull_request:
types: [opened, synchronize, reopened]

jobs:
build_and_push:
runs-on: ubuntu-latest
steps:
- name: Build and Push Container to docker.io
uses: GlueOps/github-actions-build-push-containers@v0.2.0
with:
registry: "docker.io"
dockerhub_username: ${{ secrets.DOCKERHUB_USERNAME }}
dockerhub_password: ${{ secrets.DOCKERHUB_PASSWORD }}
```

#### **AWS Elastic Container Registry (.dkr.ecr.)**

```yaml
name: Build and Push Container to ECR

on:
pull_request:
types: [opened, synchronize, reopened]

jobs:
build_and_push:
runs-on: ubuntu-latest
steps:
- name: Build and Push Container to ECR
uses: GlueOps/github-actions-build-push-containers@v0.2.0
with:
registry: "<aws-account-id>.dkr.ecr.<aws-region>.amazonaws.com"
aws_access_key_id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws_secret_access_key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
```
162 changes: 120 additions & 42 deletions action.yml
Original file line number Diff line number Diff line change
@@ -1,77 +1,155 @@
name: "Build Docker Image and Push to GHCR"
author: "@GlueOps"
description: "Abstracts defining actions to push Docker images to ghcr.io"
description: "Abstracts defining actions to push Docker images to desired registry, defaults to ghcr.io"
branding:
icon: 'box'
color: 'yellow'



inputs:
github_token:
description: "Personal Access Token (PAT) used to authenticate with the GitHub Container Registry."
required: true
default: ${{ github.token }}
# common inputs
registry:
description: 'The container registry to push the image to'
required: true
default: "ghcr.io"

registry-username:
description: 'The username for authentication to the container registry (defaults to the github.actor)'
required: true
default: ${{ github.actor }}

image_name:
description: 'Docker image is named after repository'
required: true
default: ${{ github.repository }}

dockerfile-path:
description: 'path to the Dockerfile'
required: true
default: "Dockerfile"

context:
description: "A path to the context in which the build will happen, see https://docs.docker.com/engine/reference/commandline/build/"
required: false
default: "."

registry:
description: 'The container registry to push the image to'
required: true
default: "ghcr.io"

registry-username:
description: 'The username for authentication to the container registry (defaults to the github.actor)'
target_directory:
description: 'Directory to clone the repository into.'
required: false
default: "."


# ghcr
github_token:
description: "Personal Access Token (PAT) used to authenticate with the GitHub Container Registry."
required: false
default: ${{ github.token }}


# ecr
aws_access_key_id:
description: 'AWS Access Key ID'
required: false

aws_secret_access_key:
description: 'AWS Secret Access Key'
required: false

tags:
description: 'The tags to assign to the Docker image'

aws_default_region:
description: 'AWS Default Region'
required: false
default: "us-west-2"


# docker hub
dockerhub_username:
description: 'Docker Hub Username'
required: false

dockerhub_password:
description: 'Docker Hub Password or Token'
required: false



runs:
using: "composite"
steps:
# https://docs.github.com/en/packages/working-with-a-github-packages-registry/working-with-the-container-registry#authenticating-to-the-container-registry
- name: Log in to the Container registry
- name: Configure for AWS if using ECR
shell: bash
env:
DOCKER_BUILDKIT: '1'
DOCKER_IO_USER: ${{ inputs.registry-username }}
IMAGE_NAME: ${{ inputs.image_name }}

if: contains(inputs.registry, '.dkr.ecr.')
run: |
echo "::group::Cloning target ref..."
# initial ref is branch
export TARGET_REF="${GITHUB_REF#refs/heads/}"
# if tag, replace branch with tag
export TARGET_REF="${TARGET_REF#refs/tags/}"
echo "Current ref is: $TARGET_REF"

git clone --depth=1 --branch="$TARGET_REF" "https://${{ github.actor }}:${{ github.token }}@github.com/${{ github.repository }}" .
echo "Using $(docker -v)"
echo "::group::Installing AWS CLI..."
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install --update
aws --version
echo "::endgroup::"

if [ -z "${DOCKER_IO_USER}" ]; then
export DOCKER_IO_USER="${{ github.actor }}"
echo "::group::Setting AWS Credentials to Environment Variables"
# set aws credentials as env vars
if [[ -n "${{ inputs.aws_access_key_id }}" ]]; then
echo "AWS_ACCESS_KEY_ID=${{ inputs.aws_access_key_id }}" >> $GITHUB_ENV
fi
if [[ -n "${{ inputs.aws_secret_access_key }}" ]]; then
echo "AWS_SECRET_ACCESS_KEY=${{ inputs.aws_secret_access_key }}" >> $GITHUB_ENV
fi
if [[ -n "${{ inputs.aws_default_region }}" ]]; then
echo "AWS_DEFAULT_REGION=${{ inputs.aws_default_region }}" >> $GITHUB_ENV
fi
echo "Your username is ${DOCKER_IO_USER}"
echo "::endgroup::"

echo "::group::Logging into the GitHub Container registry ..."
echo "${{ github.token }}" | docker login ${{ inputs.registry }} -u ${{ github.actor }} --password-stdin
# https://docs.github.com/en/packages/working-with-a-github-packages-registry/working-with-the-container-registry#authenticating-to-the-container-registry
- name: Registry Authentication
shell: bash
run: |
case "${{ inputs.registry }}" in
"ghcr.io")
echo "${{ inputs.github_token }}" | docker login ${{ inputs.registry }} -u ${{ github.actor }} --password-stdin
;;
*".dkr.ecr."*)
echo $(aws ecr get-login-password --region ${{ inputs.aws_default_region }}) \
| docker login --username AWS --password-stdin ${{ inputs.registry }}
;;
"docker.io")
echo "${{ inputs.dockerhub_password }}" | docker login -u "${{ inputs.dockerhub_username }}" --password-stdin
;;
*)
echo "Unsupported registry"
exit 1
;;
esac

- name: Determine ref
id: determine_ref
shell: bash
run: |
echo "::group::determine ref"
if [[ $GITHUB_REF == refs/heads/* ]]; then
echo "REF_TYPE=branch" >> $GITHUB_ENV
echo "TARGET_REF=${GITHUB_REF#refs/heads/}" >> $GITHUB_ENV
elif [[ $GITHUB_REF == refs/tags/* ]]; then
echo "REF_TYPE=tag" >> $GITHUB_ENV
echo "TARGET_REF=${GITHUB_REF#refs/tags/}" >> $GITHUB_ENV
elif [[ $GITHUB_REF == refs/pull/* ]]; then
echo "REF_TYPE=pull_request" >> $GITHUB_ENV
echo "TARGET_REF=$GITHUB_SHA" >> $GITHUB_ENV
else
echo "REF_TYPE=unknown" >> $GITHUB_ENV
fi
echo "::endgroup::"

echo "::group::Set commit tags"

- name: Checkout
uses: actions/checkout@v3
with:
ref: ${{ steps.determine_ref.outputs.ref_name }}
path: ${{ inputs.target_directory }}

- name: Build Container
shell: bash
env:
DOCKER_BUILDKIT: '1'
IMAGE_NAME: ${{ inputs.image_name }}

run: |
echo "::group::Set Tags"
echo "Event payload: ${{ toJson(github.event_name) }}"

# Clean up TARGET_REF for invalid characters
Expand Down
3 changes: 3 additions & 0 deletions tests/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FROM debian:bullseye-slim

CMD echo "\e[42m\e[30m github-actions-build-push-containers works from \e[1m$REGISTRY \e[0m"