Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify action to build and consume container #124

Merged
merged 6 commits into from
Mar 29, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
20 changes: 20 additions & 0 deletions .github/actions/automatic-releases/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Definition of the github action
# as per https://docs.github.com/en/actions/creating-actions/creating-a-docker-container-action

name: 'laminas/automatic-releases'
description: 'Automates automatic releases for semver-compliant repositories'

inputs:
command-name:
description: |
Command to execute: one of
* `laminas:automatic-releases:release`
* `laminas:automatic-releases:create-merge-up-pull-request`
* `laminas:automatic-releases:switch-default-branch-to-next-minor`
required: true

runs:
using: 'docker'
image: '../../../Dockerfile'
args:
- ${{ inputs.command-name }}
Comment on lines +16 to +20
Copy link
Member Author

@weierophinney weierophinney Mar 3, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I discovered is that while the image is in a directory relative to this one, the actual build happens relative to $GITHUB_WORKSPACE, so everything works correctly. By doing this, we get the benefits of whatever changes we've made for this release... when creating the release.

10 changes: 5 additions & 5 deletions .github/workflows/automatic-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
uses: "actions/checkout@v2"

- name: "Release"
uses: "./"
uses: "./.github/actions/automatic-releases/"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And this part ensures we use that local action, which builds from the Dockerfile directly instead of using the image.

with:
command-name: "laminas:automatic-releases:release"
env:
Expand All @@ -27,7 +27,7 @@ jobs:
"GIT_AUTHOR_EMAIL": ${{ secrets.GIT_AUTHOR_EMAIL }}

- name: "Create Merge-Up Pull Request"
uses: "./"
uses: "./.github/actions/automatic-releases/"
with:
command-name: "laminas:automatic-releases:create-merge-up-pull-request"
env:
Expand All @@ -37,7 +37,7 @@ jobs:
"GIT_AUTHOR_EMAIL": ${{ secrets.GIT_AUTHOR_EMAIL }}

- name: "Create and/or Switch to new Release Branch"
uses: "./"
uses: "./.github/actions/automatic-releases/"
with:
command-name: "laminas:automatic-releases:switch-default-branch-to-next-minor"
env:
Expand All @@ -47,7 +47,7 @@ jobs:
"GIT_AUTHOR_EMAIL": ${{ secrets.GIT_AUTHOR_EMAIL }}

- name: "Bump Changelog Version On Originating Release Branch"
uses: "./"
uses: "./.github/actions/automatic-releases/"
with:
command-name: "laminas:automatic-releases:bump-changelog"
env:
Expand All @@ -57,7 +57,7 @@ jobs:
"GIT_AUTHOR_EMAIL": ${{ secrets.GIT_AUTHOR_EMAIL }}

- name: "Create new milestones"
uses: "./"
uses: "./.github/actions/automatic-releases/"
with:
command-name: "laminas:automatic-releases:create-milestones"
env:
Expand Down
47 changes: 47 additions & 0 deletions .github/workflows/build-and-push-containers.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: Build and push containers

on:
release:
types: [published]

jobs:
tags:
runs-on: ubuntu-latest
outputs:
tags: ${{ steps.tags.outputs.tags }}
steps:
- name: Compile tag list
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True - but this is fast and doesn't require any additional overhead as it's using tooling already in the environment.

Thanks for the link, though - I'd not seen that one!

id: tags
run: |
TAG=${GITHUB_REF/refs\/tags\//}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
TAG=${GITHUB_REF/refs\/tags\//}
TAG=${GITHUB_REF#refs/tags/}

I'm sure I've already suggested this elsewhere.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

PREFIX=ghcr.io/laminas/automatic-releases
MAJOR="${PREFIX}:$(echo ${TAG} | cut -d. -f1)"
MINOR="${MAJOR}.$(echo ${TAG} | cut -d. -f2)"
PATCH="${PREFIX}:${TAG}"
echo "::set-output name=tags::${MAJOR}%0A${MINOR}%0A${PATCH}"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GHA replaces %0A with \n, making these line-delimited tags. This creates a list that looks like:

ghcr.io/laminas/automatic-releases:1
ghcr.io/laminas/automatic-releases:1.11
ghcr.io/laminas/automatic-releases:1.11.0

which is then passed as the variable "tags" to the next step.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is probably not the best idea to rely on undocumented behavior. Seems like an implementation detail that may behave differently in the future.

However, they have documented to pass as json, if you need something like this:

name: build
on: push
jobs:
  job1:
    runs-on: ubuntu-latest
    outputs:
      matrix: ${{ steps.set-matrix.outputs.matrix }}
    steps:
    - id: set-matrix
      run: echo "::set-output name=matrix::{\"include\":[{\"project\":\"foo\",\"config\":\"Debug\"},{\"project\":\"bar\",\"config\":\"Release\"}]}"
  job2:
    needs: job1
    runs-on: ubuntu-latest
    strategy:
      matrix: ${{fromJSON(needs.job1.outputs.matrix)}}
    steps:
    - run: build

Copy link
Member Author

@weierophinney weierophinney Mar 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@glensc This is actually the approach recommended by the GHA team, and I discovered it here: https://git.luolix.topmunity/t/set-output-truncates-multiline-strings/16852/3 (they are indicating that they transform ANSI escape sequences emitted in outputs).

A later note in that thread indicates that it's undocumented, but that they will be updating the docs to reflect this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've done some thinking on this, and decided to go with the JSON approach; I'm now doing:

with:
  tags: ${{ join(fromJSON(steps.tags.outputs.tags), "\n") }}

(as they "tags" value is expected to be multiline).

This should address any concerns about ANSI escape sequences, and ensure we are forwards-compatible with any changes GHA makes.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update: using "," as the separator, as CSV is supported for that value, and the "\n" value was causing issues with the GHA expressions.


release-container:
runs-on: ubuntu-latest
needs: [tags]
steps:
- name: Checkout
uses: actions/checkout@v2
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not super-important, but some spacing (blank lines around blocks) would go a long way with improving readability

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

- name: Setup QEMU
uses: docker/setup-qemu-action@v1
- name: Setup Docker Buildx
uses: docker/setup-buildx-action@v1
- name: Login to GitHub Container Registry
uses: docker/login-action@v1
with:
registry: ghcr.io
username: ${{ secrets.CONTAINER_USERNAME }}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These secrets should be prefixed, to make it clear they are local to our build

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done; used AUTOMATIC_RELEASES_ as the prefix.

password: ${{ secrets.CONTAINER_PAT }}
- name: Build and push
uses: docker/build-push-action@v2
with:
context: .
file: ./Dockerfile
platforms: linux/amd64
push: true
tags: ${{ needs.tags.outputs.tags }}

2 changes: 1 addition & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ inputs:

runs:
using: 'docker'
image: 'Dockerfile'
image: 'docker://ghcr.io/laminas/automatic-releases:1'
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the magic: the action now uses an image on ghcr.io, which means only a pull operation is required, and no build operations!

args:
- ${{ inputs.command-name }}