Last active
August 2, 2024 23:36
-
-
Save nbeck415/5182b3597f889da740e57f8707311219 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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
Superb! thanks a lot!