Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save yousecjoe/366d526f46ee66d98f466fff8c9ae3de to your computer and use it in GitHub Desktop.
Save yousecjoe/366d526f46ee66d98f466fff8c9ae3de to your computer and use it in GitHub Desktop.
This GitHub Actions workflow builds a Docker image and pushes it a container registry every time someone commits to main.
name: Build and push to Docker Hub
on:
push:
branches:
- main
jobs:
push_to_docker_hub:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Log in to Docker Hub
run: |
echo ${{ secrets.DOCKERHUB_PASSWORD }} | docker login -u ${{ secrets.DOCKERHUB_USERNAME }} --password-stdin
- name: Build and tag image
run: |
COMMIT_SHA=$(echo $GITHUB_SHA | cut -c1-7)
docker build -t ${{ secrets.DOCKERHUB_USERNAME }}/${{ github.repository }}:$COMMIT_SHA -f path/to/Dockerfile .
- name: Push image to Docker Hub
run: docker push ${{ secrets.DOCKERHUB_USERNAME }}/${{ github.repository }}:$COMMIT_SHA
name: Build and push to GHCR
on:
push:
branches:
- main
jobs:
push_to_ghcr:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Log in to ghcr.io
run: echo "${{ secrets.GHCR_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin
- name: Build and tag image
run: |
COMMIT_SHA=$(echo $GITHUB_SHA | cut -c1-7)
docker build -t ghcr.io/${{ github.repository_owner }}/${{ github.repository }}:$COMMIT_SHA -f path/to/Dockerfile .
- name: Push image to GHCR
run: docker push ghcr.io/${{ github.repository_owner }}/${{ github.repository }}:$COMMIT_SHA
name: Build and push to Harbor
on:
push:
branches:
- main
jobs:
push_to_harbor:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Log in to Harbor
run: |
echo ${{ secrets.HARBOR_CREDENTIALS }} | base64 --decode | docker login -u $(cut -d ':' -f1 <<< "${{ secrets.HARBOR_CREDENTIALS }}") --password-stdin ${{ secrets.HARBOR_REGISTRY_URL }}
- name: Build and tag image
run: |
COMMIT_SHA=$(echo $GITHUB_SHA | cut -c1-7)
docker build -t ${{ secrets.HARBOR_REGISTRY_URL }}/project-name/${{ github.repository }}:$COMMIT_SHA -f path/to/Dockerfile .
- name: Push image to Harbor
run: docker push ${{ secrets.HARBOR_REGISTRY_URL }}/project-name/${{ github.repository }}:$COMMIT_SHA
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment