puppetlabs / puppet-dev-tools

Puppet development tools in a Docker image
Apache License 2.0
12 stars 25 forks source link

Adapt GH Action to also push to Docker Hub #22

Closed genebean closed 4 years ago

genebean commented 4 years ago

Part of this commit includes switching from two Dockerfiles to a single one that does a multi-stage build. All the same images are built but now the gosu image is guaranteed to be built off of the main image that was also just built. Part of the need for the change to what gosu uses as a FROM is an additional change that removes the user from the images being built so that when they are build by the GH Action the username can be configurable. This allows for anyone who has forked the repo to also build Docker images and push them to their own account on Docker Hub.

You may also notice that the end of the Dockerfile effectively takes base and renames it to main. This is done so that two things can be simultaneously true:

  1. the gosu image is built off of the base image
  2. if --target is not specified the result is the "main" image... which happens to be the exact same as base.

The following additional changes are also included:

  • A minor edit was made to the Dockerfile to cleanup the bundler config set at build time so as to be sure not to bust any use of bundler by the end user.
  • A couple of minor edits were made to the Gemfile to improve consistency in light of the fact that Dependabot is now keeping this file up to date.
  • The README was updated to document what tags are pushed to Docker Hub
  • Microbadger badges were added to show latest version and commit on Docker Hub
  • A couple of pieces of metadata were added into the Dockerfile to be included in the images as labels. These labels power the Microbadger badges.

Config changes needed to make this all work

genebean commented 4 years ago

Probably shouldn’t push to Docker Hub unless it’s a push event to master either...

genebean commented 4 years ago

Probably shouldn’t push to Docker Hub unless it’s a push event to master either...

This is resolved too by switching to multiple workflow files.

genebean commented 4 years ago

CI won't pass on this until, at a minimum, the username secret mentioned in the top message is added.

genebean commented 4 years ago

For reference, here is a comparison of the two workflow files:

--- .github/workflows/build-test-push.yml   2020-02-26 14:16:12 +0000
+++ .github/workflows/build-test.yml    2020-02-26 14:22:36 +0000
@@ -1,10 +1,10 @@
-name: Build-Test-Push
+name: Build-Test
 on:
-  push:
-      branches:
-        - master
+  - pull_request
 jobs:
-  Build-Test-Push:
+  Build-Test:
+    # Everything below here should be identical to build-test-push.yml with one exception:
+    # the "Login to Docker Hub" and "Push Docker images" should have been removed.
     runs-on: ubuntu-latest
     steps:
       - uses: actions/checkout@v1
@@ -36,12 +36,3 @@
         run: |
           docker inspect --format='{{json .Config.Labels}}' ${{ secrets.DOCKERHUB_USERNAME }}/puppet-dev-tools:latest
           docker inspect --format='{{json .Config.Labels}}' ${{ secrets.DOCKERHUB_USERNAME }}/puppet-dev-tools:latest
-      - name: Login to Docker Hub
-        run: echo ${{ secrets.DOCKERHUB_PASSWORD }} | docker login -u ${{ secrets.DOCKERHUB_USERNAME }} --password-stdin
-      - name: Push Docker images
-        run: |
-          docker push ${{ secrets.DOCKERHUB_USERNAME }}/puppet-dev-tools:$(date +"%F")-$(git rev-parse --short HEAD)
-          docker push ${{ secrets.DOCKERHUB_USERNAME }}/puppet-dev-tools:latest
-          docker push ${{ secrets.DOCKERHUB_USERNAME }}/puppet-dev-tools:gosu-$(date +"%F")-$(git rev-parse --short HEAD)
-          docker push ${{ secrets.DOCKERHUB_USERNAME }}/puppet-dev-tools:gosu