Last active
July 1, 2021 17:27
-
-
Save aviflax/357cc3b97317d97d2a8cc250f85abe0c to your computer and use it in GitHub Desktop.
An example of a GitHub Actions job that builds a Docker container image and then pushes it to to the GitHub Packages container registry associated with the same repo, using regular `docker` commands
This file contains hidden or 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
# An example of a GitHub Actions job that builds a Docker container image and then pushes | |
# it to to the GitHub Packages container registry associated with the same repo, using | |
# regular `docker` commands. | |
# Change these to whatever you want, of course. For example, you might want to only run this workflow for | |
# PRs. Personally I like to check the build on every push. That said, it’s probably not necessary to publish | |
# an artifact (container image) for every single push; I’m personally planning to add an `if` to the push | |
# step that skips the `docker push` if the GitHub event is `push`. | |
name: Push | |
on: | |
- push | |
# un-comment this line if you also want the workflow triggered for PRs. | |
# - pull_request | |
jobs: | |
build-and-push-container-image: | |
# Relevant docs: https://docs.github.com/en/actions/guides/publishing-docker-images#publishing-images-to-github-packages | |
runs-on: ubuntu-20.04 | |
env: | |
REGISTRY: ghcr.io | |
steps: | |
- uses: actions/checkout@v2 | |
- name: Set environment variables | |
run: | | |
echo "IMAGE_NAME=$(echo "$REGISTRY/$GITHUB_REPOSITORY" | tr '[:upper:]' '[:lower:]')" >> $GITHUB_ENV | |
echo "GIT_SHA=$(echo ${{ github.sha }} | cut -c1-7)" >> $GITHUB_ENV | |
echo "GIT_REF=$(git symbolic-ref -q --short HEAD || git describe --tags --exact-match)" >> $GITHUB_ENV | |
echo "IMAGE_TAG=${GIT_REF}-${GIT_SHA}" >> $GITHUB_ENV | |
- name: docker build | |
run: docker build --tag ${IMAGE_NAME}:${IMAGE_TAG} . | |
- name: docker login | |
run: echo ${{ secrets.GITHUB_TOKEN }} | docker login $REGISTRY -u "${{ github.actor }}" --password-stdin | |
- name: docker push | |
run: docker push ${IMAGE_NAME}:${IMAGE_TAG} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment