Skip to content

Commit

Permalink
gpavelar/c05-actions02
Browse files Browse the repository at this point in the history
  • Loading branch information
gpavelar committed Sep 1, 2020
1 parent 1c79b1a commit 9ccd52a
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 0 deletions.
35 changes: 35 additions & 0 deletions .github/workflows/gpavelar-gh-actions02-workflow.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: GPAVELAR/C05-ACTIONS01
on:
pull_request:
branches:
- master
paths: 'gpavelar/c05-actions02/**'

jobs:
comment:
name: Deploy
runs-on: ubuntu-18.04

steps:
- name: Docker build and tag
working-directory: 'gpavelar/c05-actions02/'
run: make docker-build

- name: Docker Push Image
working-directory: 'gpavelar/c05-actions02/'
env:
GPAVELAR_DH_TOKEN: ${{ secrets.GPAVELAR_DH_TOKEN }}
run: make docker-push

uses: mshick/add-pr-comment@v1
with:
message: "${{ env.IMAGE }}:${{ env.SHORT_SHA }}"


# uses: mshick/add-pr-comment@v1
# env:
# GITHUB_TOKEN: ${{ secrets.GPAVELAR_PAT }}
# with:
# message: "Well done ${{ secrets.GPAVELAR_USERNAME }} ! This is a nice PR"
# repo-token-user-login: 'github-actions[bot]'
# allow-repeats: true
2 changes: 2 additions & 0 deletions gpavelar/c05-actions02/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
FROM httpd:2.4.41
RUN echo "This is my GH actions exercise" > /usr/local/apache2/htdocs/index.html
46 changes: 46 additions & 0 deletions gpavelar/c05-actions02/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
COMPOSE_PATH = docker-compose.yml
COMPOSE_ENV = docker-compose -f ${COMPOSE_PATH}
COMPOSE_RUN = ${COMPOSE_ENV} run --rm
SHORT_SHA=$(shell git rev-parse --short HEAD)
IMAGE ?= actions02
DH_USERNAME ?= gpavelar

##+MAKEFILE HELPER
##+
##+GENERAL COMMANDS
help: ##+ Show Available commands.
@fgrep -h "##+" $(MAKEFILE_LIST) | fgrep -v fgrep | sed -e 's/\\$$//' | sed -e 's/##+//'

##+
##+COMPOSE CMD
up: ##+ Builds the compose containers.
${COMPOSE_ENV} up -d
down: ##+ Downs the compose containers.
${COMPOSE_ENV} down
build: ##+ Builds the compose containers.
${COMPOSE_ENV} build

##+
##+ Docker build
##+
.PHONY: docker-build
docker-build:
@echo "Building Docker Image ${IMAGE}:${SHORT_SHA}"
@docker build -t ${IMAGE}:${SHORT_SHA} .

##+
##+ Docker push image
##+
.PHONY: docker-push
docker-push: dh-login
@echo "Publishing the container as ${DH_USERNAME}/${IMAGE}:${SHORT_SHA}"
@docker tag ${IMAGE}:${SHORT_SHA} ${DH_USERNAME}/${IMAGE}:${SHORT_SHA}
@docker push ${DH_USERNAME}/${IMAGE}:${SHORT_SHA}

##+
##+ DockerHub login
##+
.PHONY: dh-login
dh-login:
@echo "Log in to a Docker registry"
@docker login --username ${DH_USERNAME} --password ${GPAVELAR_DH_TOKEN}
81 changes: 81 additions & 0 deletions gpavelar/c05-actions02/README-actions02.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# Github Action to push a container image to Dockerhub (c05-actions02)

Using 3Musketeers (don't use marketplace actions), create a workflow that will:

- build a simple container image. Something like this is enough:
```
FROM httpd:2.4.41
RUN echo "This is my GH actions exercise" > /usr/local/apache2/htdocs/index.html
```
- tag the container image with the commit `SHORT_SHA` (SHORT_SHA's are usually the last 4-6 digits of the commit SHA) i.e `my-image:123456`
- push to dockerhub
- comment to the PR the new image name with tag pushed to Dockerhub

Please follow the below steps to complete this task:

- Save all your files on the [PLAYGROUND](https://github.com/devopsacademyau/playground) repository under `/yourgithub-username/c05-actions02/***`
- Generate any GitHub or DockerHub tokens required
- Reference tokens as SECRETS - **DON'T HARDCODE YOUR GH TOKEN ON YOUR CODE.**
- Raise a PR with some `Dockerfile` changes
- Check if image gets deployed to Dockerhub
- Check if PR gets commented with new image name/tag

- Create a PR to add a new folder with your github username on the PLAYGROUND repository root `/`.
- You should be able to merge your own PR without an approval. (let us know if not)
- You will need to generate a GH token to be able to interact with GH api. https://help.github.com/en/github/authenticating-to-github/creating-a-personal-access-token-for-the-command-line
- Please use SECRETS to refer your GH token when creating your workflow. https://help.github.com/en/actions/configuring-and-managing-workflows/creating-and-storing-encrypted-secrets

**DON'T HARDCODE YOUR GH TOKEN ON YOUR CODE.**

- Your workflow should be triggered every time a PR to change a file under `/<your-github-username>/*` is created
- Once the workflow is triggered, a job should comment the PR with:

`Well done $MY_NAME ! This is a nice PR`

- `$MY_NAME` need to be replaced with **YOUR** name using **SECRETS**.


## Submit a PR with the following files:
- README.md based on the [ANSWER.md file](ANSWER.md) with a link to the following files from your answer:
- workflow yaml file used


## Actions

```yml
name: GPAVELAR/C05-ACTIONS02
on:
pull_request:
branches:
- master
paths: 'gpavelar/c05-actions02/**'

jobs:
comment:
name: Deploy
runs-on: ubuntu-18.04

steps:
- name: Docker build and tag
working-directory: 'gpavelar/c05-actions02/'
run: make docker-build

- name: Docker Push Image
working-directory: 'gpavelar/c05-actions02/'
env:
GPAVELAR_DH_TOKEN: ${{ secrets.GPAVELAR_DH_TOKEN }}
run: make docker-push

uses: mshick/add-pr-comment@v1
with:
message: "${{ env.IMAGE }}:${{ env.SHORT_SHA }}"


# uses: mshick/add-pr-comment@v1
# env:
# GITHUB_TOKEN: ${{ secrets.GPAVELAR_PAT }}
# with:
# message: "Well done ${{ secrets.GPAVELAR_USERNAME }} ! This is a nice PR"
# repo-token-user-login: 'github-actions[bot]'
# allow-repeats: true
```
10 changes: 10 additions & 0 deletions gpavelar/c05-actions02/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
version: "3"
services:
web:
build:
context: .
dockerfile: Dockerfile
working_dir: /app
volumes:
- .:/app

0 comments on commit 9ccd52a

Please sign in to comment.