Build and Push go-ethereum Docker image to ECR #1
Workflow file for this run
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 go-ethereum Docker image to ECR | |
on: | |
# keep for manual trigger if needed | |
workflow_dispatch: | |
inputs: | |
create_tag: | |
description: "Create and push a new tag?" | |
required: false | |
type: boolean | |
pull_request: | |
branches: [master] | |
types: | |
- closed | |
env: | |
AWS_ACCOUNT_ID: "861276097334" | |
AWS_REGION: "eu-central-1" | |
ECR_REPO_NAME: "limechain-devops-task/go-ethereum" | |
IAM_OIDC_ROLE_NAME: "go-ethereum-github-actions-role" | |
GH_TOKEN: ${{ github.token }} | |
permissions: | |
id-token: write | |
contents: write | |
pull-requests: write | |
jobs: | |
build-and-push: | |
if: > | |
github.event_name == 'workflow_dispatch' || | |
(github.event.pull_request.merged == true && contains(github.event.pull_request.labels.*.name, 'CI:Build')) | |
runs-on: ubuntu-22.04 | |
steps: | |
- name: Checkout Code | |
uses: actions/checkout@v4 | |
with: | |
fetch-depth: 0 | |
ref: ${{ github.ref_name }} | |
### Need to be careful with dependency caches, since using GitHub-hosted runners. More info at the URL below: | |
### https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/caching-dependencies-to-speed-up-workflows#about-caching-workflow-dependencies | |
- name: Cache go.mod packages | |
uses: actions/setup-go@v5 | |
with: | |
go-version-file: "go.mod" | |
- name: Configure AWS Credentials | |
uses: aws-actions/configure-aws-credentials@v4 | |
with: | |
aws-region: ${{ env.AWS_REGION }} | |
role-to-assume: arn:aws:iam::${{ env.AWS_ACCOUNT_ID }}:role/${{ env.IAM_OIDC_ROLE_NAME }} | |
role-session-name: go-ethereum-github-actions | |
- name: Login to Amazon ECR | |
id: login-ecr | |
run: | | |
FULL_ECR_URL=${{ env.AWS_ACCOUNT_ID }}.dkr.ecr.${{ env.AWS_REGION }}.amazonaws.com | |
aws ecr get-login-password --region ${{ env.AWS_REGION }} | docker login --username AWS --password-stdin ${FULL_ECR_URL} | |
- name: Get latest image tag | |
id: get-latest-tag | |
run: | | |
LATEST_TAG=$(aws ecr describe-images \ | |
--repository-name ${{ env.ECR_REPO_NAME }} \ | |
--region ${{ env.AWS_REGION }} \ | |
--query 'sort_by(imageDetails,&imagePushedAt)[-1].imageTags[0]' \ | |
--output text) | |
if [[ "$LATEST_TAG" == "None" ]]; then | |
NEW_TAG=1 | |
else | |
NEW_TAG=$((LATEST_TAG + 1)) | |
fi | |
echo "new-tag=${NEW_TAG}" >> $GITHUB_OUTPUT | |
- name: Build and push Docker Image | |
env: | |
COMMIT: ${{ github.sha }} | |
run: | | |
FULL_ECR_URL="${{ env.AWS_ACCOUNT_ID }}.dkr.ecr.${{ env.AWS_REGION }}.amazonaws.com/${{ env.ECR_REPO_NAME }}" | |
NEW_TAG="${{ steps.get-latest-tag.outputs.new-tag }}" | |
# define build args for the image | |
COMMIT=${COMMIT:0:7} | |
VERSION=$NEW_TAG | |
BUILDNUM=$(git rev-list --count HEAD) | |
docker build \ | |
--build-arg COMMIT="${COMMIT}" \ | |
--build-arg VERSION="${VERSION}" \ | |
--build-arg BUILDNUM="${BUILDNUM}" \ | |
-t "${FULL_ECR_URL}:${NEW_TAG}" \ | |
-f Dockerfile . | |
docker push "${FULL_ECR_URL}:${NEW_TAG}" | |
- name: Create a new tag | |
if: > | |
inputs.create_tag == true || | |
(github.event.pull_request.merged == true && contains(github.event.pull_request.labels.*.name, 'CI:Build')) | |
run: | | |
git config user.name "github-actions[bot]" | |
git config user.email "github-actions[bot]@users.noreply.github.com" | |
NEW_TAG=v${{ steps.get-latest-tag.outputs.new-tag }} | |
git tag -a $NEW_TAG -m "New tag: $NEW_TAG" | |
git push origin $NEW_TAG | |
- name: Add a release comment in PR | |
if: > | |
inputs.create_tag == true || | |
(github.event.pull_request.merged == true && contains(github.event.pull_request.labels.*.name, 'CI:Build')) | |
uses: actions/github-script@v7 | |
id: my-script | |
with: | |
script: | | |
github.rest.issues.createComment({ | |
issue_number: context.issue.number, | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
body: ':rocket: A new tag v${{ steps.get-latest-tag.outputs.new-tag }} has been created and pushed!' | |
}) |