Skip to content

Commit cfcd693

Browse files
committed
Push image ghcr
1 parent 4e3fb92 commit cfcd693

File tree

4 files changed

+152
-14
lines changed

4 files changed

+152
-14
lines changed

.github/actions/build/action.yml

+41-14
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ inputs:
1515
required: false
1616
description: "Node environment"
1717
default: "production"
18+
latest:
19+
required: false
20+
description: "Tag latest version"
21+
default: "false"
1822

1923
outputs:
2024
tags:
@@ -56,30 +60,53 @@ runs:
5660
id: image
5761
shell: bash
5862
run: |
59-
echo "image=ghcr.io/mozilla/test-github-features" >> $GITHUB_OUTPUT
63+
registry="ghcr.io"
64+
repository="${{ github.repository }}"
65+
image="$registry/$repository"
66+
67+
echo "registry=$registry" >> $GITHUB_OUTPUT
68+
echo "repository=$repository" >> $GITHUB_OUTPUT
69+
echo "image=$image" >> $GITHUB_OUTPUT
70+
71+
cat $GITHUB_OUTPUT
6072
6173
- name: Docker meta
6274
id: meta
6375
uses: docker/metadata-action@v5
6476
with:
6577
images: ${{ steps.image.outputs.image }}
78+
flavor: |
79+
suffix=-next,onlatest=true
80+
latest=${{ inputs.latest == 'true' }}
6681
tags: |
67-
type=raw,value=latest,enable={{is_default_branch}}
68-
type=raw,value=staging,enable=${{ github.event_name == 'merge_group' }}
6982
type=ref,event=pr
70-
type=sha
83+
type=ref,event=branch
84+
type=ref,event=tag
85+
86+
- name: Docker tag
87+
id: tag
88+
shell: bash
89+
run: |
90+
tag=$(echo "${{ steps.meta.outputs.json }}" | jq -r '.tags[0]')
91+
echo "tag: $tag"
92+
tag="${{ steps.image.outputs.image }}:${{ steps.meta.outputs.version }}"
93+
echo "tag: $tag"
94+
tag_cache="$image:$version-cache"
95+
96+
echo "tag=$tag" >> $GITHUB_OUTPUT
97+
echo "tag_cache=$tag_cache" >> $GITHUB_OUTPUT
98+
99+
cat $GITHUB_OUTPUT
71100
72101
- name: Build Image
73-
uses: docker/build-push-action@v5
102+
id: build
103+
uses: docker/bake-action@v4
104+
env:
105+
DOCKER_TAG: ${{ steps.tag.outputs.tag }}
74106
with:
75-
context: .
76-
platforms: linux/amd64
77-
pull: true
107+
targets: app
78108
push: ${{ inputs.push }}
79109
load: ${{ inputs.push == 'false' }}
80-
tags: ${{ steps.meta.outputs.tags }}
81-
cache-from: type=gha
82-
cache-to: type=gha,mode=max
83-
build-args: |
84-
VERSION=${{ steps.meta.outputs.tags }}
85-
NODE_ENV=${{ inputs.node_env }}
110+
set: |
111+
*.cache-from=type=registry,ref=${{ steps.tag.outputs.tag_cache }}
112+
*.cache-to=type=registry,ref=${{ steps.tag.outputs.tag_cache }},mode=max,compression-level=9,force-compression=true,ignore-error=true

.github/actions/context/action.yml

+80
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,30 @@
11
name: 'Dump Context'
22
description: 'Display context for action run'
33

4+
outputs:
5+
# All github action outputs are strings, even if set to "true"
6+
# so when using these values always assert against strings or convert from json
7+
# \$\{{ needs.context.outputs.is_fork == 'true' }} // true
8+
# \$\{{ fromJson(needs.context.outputs.is_fork) == false }} // true
9+
# \$\{{ needs.context.outputs.is_fork == true }} // false
10+
# \$\{{ needs.context.outputs.is_fork }} // false
11+
is_fork:
12+
description: ""
13+
value: ${{ steps.context.outputs.is_fork }}
14+
is_default_branch:
15+
description: ""
16+
value: ${{ steps.context.outputs.is_default_branch }}
17+
is_release_master:
18+
description: ""
19+
value: ${{ steps.context.outputs.is_release_master }}
20+
is_release_tag:
21+
description: ""
22+
value: ${{ steps.context.outputs.is_release_tag }}
23+
# Hardcode image name
24+
image_name:
25+
description: ""
26+
value: mozilla/addons-server
27+
428
runs:
529
using: 'composite'
630
steps:
@@ -36,3 +60,59 @@ runs:
3660
INPUTS_CONTEXT: ${{ toJson(inputs) }}
3761
run: |
3862
echo "$INPUTS_CONTEXT"
63+
64+
- name: Set context
65+
id: context
66+
env:
67+
# The default branch of the repository, in this case "master"
68+
default_branch: ${{ github.event.repository.default_branch }}
69+
shell: bash
70+
run: |
71+
event_name="${{ github.event_name }}"
72+
event_action="${{ github.event.action }}"
73+
74+
# Stable check for if the workflow is running on the default branch
75+
# https://stackoverflow.com/questions/64781462/github-actions-default-branch-variable
76+
is_default_branch="${{ format('refs/heads/{0}', env.default_branch) == github.ref }}"
77+
78+
# In most events, the epository refers to the head which would be the fork
79+
is_fork="${{ github.event.repository.fork }}"
80+
81+
# This is different in a pull_request where we need to check the head explicitly
82+
if [[ "${{ github.event_name }}" == 'pull_request' ]]; then
83+
# repository on a pull request refers to the base which is always mozilla/addons-server
84+
is_head_fork="${{ github.event.pull_request.head.repo.fork }}"
85+
# https://docs.github.com/en/code-security/dependabot/working-with-dependabot/automating-dependabot-with-github-actions
86+
is_dependabot="${{ github.actor == 'dependabot[bot]' }}"
87+
88+
# If the head repository is a fork or if the PR is opened by dependabot
89+
# we consider the run to be a fork. Dependabot and proper forks are treated
90+
# the same in terms of limited read only github token scope
91+
if [[ "$is_head_fork" == 'true' || "$is_dependabot" == 'true' ]]; then
92+
is_fork="true"
93+
fi
94+
fi
95+
96+
is_release_master="false"
97+
is_release_tag="false"
98+
99+
# Releases can only happen if we are NOT on a fork
100+
if [[ "$is_fork" == 'false' ]]; then
101+
# A master release occurs on a push to the default branch of the origin repository
102+
if [[ "$event_name" == 'push' && "$is_default_branch" == 'true' ]]; then
103+
is_release_master="true"
104+
fi
105+
106+
# A tag release occurs when a release is published
107+
if [[ "$event_name" == 'release' && "$event_action" == 'publish' ]]; then
108+
is_release_tag="true"
109+
fi
110+
fi
111+
112+
echo "is_default_branch=$is_default_branch" >> $GITHUB_OUTPUT
113+
echo "is_fork=$is_fork" >> $GITHUB_OUTPUT
114+
echo "is_release_master=$is_release_master" >> $GITHUB_OUTPUT
115+
echo "is_release_tag=$is_release_tag" >> $GITHUB_OUTPUT
116+
117+
echo "event_name: $event_name"
118+
cat $GITHUB_OUTPUT

.github/workflows/push.yml

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: Push
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
9+
permissions:
10+
packages: write
11+
12+
jobs:
13+
build:
14+
runs-on: ubuntu-latest
15+
16+
steps:
17+
- uses: actions/checkout@v4
18+
19+
- id: context
20+
uses: ./.github/actions/context
21+
22+
- uses: ./.github/actions/build
23+
with:
24+
push: true
25+
username: ${{ github.actor }}
26+
password: ${{ secrets.GITHUB_TOKEN }}
27+
node_env: production
28+
latest: ${{ steps.context.outputs.is_release_master }}
29+
30+

docker-compose.yml

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
version: '3.8'
22
services:
33
app:
4+
image: ${DOCKER_TAG:-}
45
build:
56
context: .
67
args:

0 commit comments

Comments
 (0)