|
| 1 | +name: Builder |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_call: |
| 5 | + inputs: |
| 6 | + context: |
| 7 | + description: 'Effective Working Directory' |
| 8 | + required: true |
| 9 | + type: string |
| 10 | + default: "./" |
| 11 | + image_name: |
| 12 | + description: 'Image Name' |
| 13 | + required: true |
| 14 | + type: string |
| 15 | + github_username: |
| 16 | + description: 'Github Container Registry Username' |
| 17 | + required: true |
| 18 | + type: string |
| 19 | + dockerhub_username: |
| 20 | + description: 'Dockerhub Container Registry Username' |
| 21 | + required: false |
| 22 | + type: string |
| 23 | + dockerhub_organization: |
| 24 | + description: 'Dockerhub Container Registry Organization' |
| 25 | + required: false |
| 26 | + type: string |
| 27 | + default: bcgovimages |
| 28 | + secrets: |
| 29 | + ghcr_token: |
| 30 | + description: 'Github Container Registry Authorization Token' |
| 31 | + required: true |
| 32 | + dockerhub_token: |
| 33 | + description: 'Dockerhub Container Registry Authorization Token' |
| 34 | + required: false |
| 35 | + outputs: |
| 36 | + image_tag: |
| 37 | + description: 'Docker image tag' |
| 38 | + value: ${{ jobs.builder.outputs.image_tag }} |
| 39 | + |
| 40 | +jobs: |
| 41 | + builder: |
| 42 | + name: Builder |
| 43 | + permissions: |
| 44 | + packages: write # for docker/build-push-action to push images to GitHub Container Registry |
| 45 | + runs-on: ubuntu-latest |
| 46 | + timeout-minutes: 10 |
| 47 | + outputs: |
| 48 | + image_tag: ${{ steps.meta.outputs.version }} |
| 49 | + steps: |
| 50 | + - name: Checkout repository |
| 51 | + uses: actions/checkout@v6 |
| 52 | + |
| 53 | + - name: Parse Input Values |
| 54 | + shell: bash |
| 55 | + run: | |
| 56 | + echo "GH_USERNAME=$(tr '[:upper:]' '[:lower:]' <<< '${{ inputs.github_username }}')" >> $GITHUB_ENV |
| 57 | + echo "HAS_DOCKERHUB=${{ inputs.dockerhub_username != '' && secrets.dockerhub_token != '' }}" >> $GITHUB_ENV |
| 58 | +
|
| 59 | + - name: Login to Github Container Registry |
| 60 | + uses: docker/login-action@v4 |
| 61 | + with: |
| 62 | + registry: ghcr.io |
| 63 | + username: ${{ env.GH_USERNAME }} |
| 64 | + password: ${{ secrets.ghcr_token }} |
| 65 | + |
| 66 | + - name: Login to Dockerhub Container Registry |
| 67 | + if: env.HAS_DOCKERHUB == 'true' |
| 68 | + uses: docker/login-action@v4 |
| 69 | + with: |
| 70 | + registry: docker.io |
| 71 | + username: ${{ inputs.dockerhub_username }} |
| 72 | + password: ${{ secrets.dockerhub_token }} |
| 73 | + |
| 74 | + - name: Prepare Container Metadata tags |
| 75 | + id: meta |
| 76 | + uses: docker/metadata-action@v6 |
| 77 | + with: |
| 78 | + images: | |
| 79 | + ghcr.io/${{ env.GH_USERNAME }}/${{ inputs.image_name }} |
| 80 | + docker.io/${{ inputs.dockerhub_organization }}/${{ inputs.image_name }},enable=${{ env.HAS_DOCKERHUB }} |
| 81 | + # Always updates the 'latest' tag |
| 82 | + flavor: | |
| 83 | + latest=true |
| 84 | + # Creates tags based off of branch names and semver tags |
| 85 | + tags: | |
| 86 | + type=ref,event=branch |
| 87 | + type=ref,event=pr |
| 88 | + type=semver,pattern={{version}} |
| 89 | + type=semver,pattern={{major}}.{{minor}} |
| 90 | + type=semver,pattern={{major}} |
| 91 | + type=sha |
| 92 | +
|
| 93 | + - name: Build and Push to Container Registry |
| 94 | + id: builder |
| 95 | + uses: docker/build-push-action@v7 |
| 96 | + with: |
| 97 | + context: ${{ inputs.context }} |
| 98 | + push: true |
| 99 | + tags: ${{ steps.meta.outputs.tags }} |
| 100 | + labels: ${{ steps.meta.outputs.labels }} |
| 101 | + annotations: ${{ steps.meta.outputs.annotations }} |
| 102 | + build-args: | |
| 103 | + GIT_COMMIT=${{ github.sha }} |
| 104 | +
|
| 105 | + - name: Inspect Docker Image |
| 106 | + shell: bash |
| 107 | + run: | |
| 108 | + docker image inspect ghcr.io/${{ env.GH_USERNAME }}/${{ inputs.image_name }}:latest |
0 commit comments